From f7fbfa09f6b4d4b9a073358c31287d191a4e9bb5 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 25 Mar 2013 18:52:29 +0100 Subject: [PATCH 01/43] XML Flash part 1 --- neo/swf/SWF.h | 11 + neo/swf/SWF_Load.cpp | 450 ++++++++++++++++++++++++++++++++++++++++ neo/swf/SWF_Main.cpp | 16 ++ neo/swf/SWF_Names.cpp | 26 +++ neo/swf/SWF_Sprites.cpp | 53 +++++ neo/swf/SWF_Sprites.h | 5 + 6 files changed, 561 insertions(+) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index 926f8e9d..1fb63773 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -389,9 +390,16 @@ private: bool LoadSWF( const char* fullpath ); bool LoadBinary( const char* bfilename, ID_TIME_T sourceTime ); void WriteBinary( const char* bfilename ); +<<<<<<< HEAD void FileAttributes( idSWFBitStream& bitstream ); void Metadata( idSWFBitStream& bitstream ); void SetBackgroundColor( idSWFBitStream& bitstream ); +======= + // RB begin + void LoadXML( const char* filename ); + void WriteXML( const char* filename ); + // RB end +>>>>>>> c4098bc... XML Flash part 1 //---------------------------------- // SWF_Shapes.cpp @@ -495,6 +503,9 @@ public: //---------------------------------- // SWF_Names.cpp //---------------------------------- + // RB begin + static const char* GetDictTypeName( swfDictType_t type ); + // RB end static const char* GetTagName( swfTag_t tag ); static const char* GetActionName( swfAction_t action ); diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 457b5a27..d36cde33 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -34,6 +35,10 @@ If you have questions concerning this license or the applicable additional terms #define BSWF_VERSION 16 // bumped to 16 for storing atlas image dimensions for unbuffered loads #define BSWF_MAGIC ( ( 'B' << 24 ) | ( 'S' << 16 ) | ( 'W' << 8 ) | BSWF_VERSION ) +// RB begin +#define XSWF_VERSION 16 +// RB end + /* =================== idSWF::LoadSWF @@ -529,6 +534,7 @@ void idSWF::WriteBinary( const char* bfilename ) } } +<<<<<<< HEAD /* =================== idSWF::FileAttributes @@ -559,3 +565,447 @@ void idSWF::SetBackgroundColor( idSWFBitStream& bitstream ) { bitstream.Seek( 4 ); // int } +======= + +// RB begin + +/* +=================== +idSWF::WriteXML +=================== +*/ +void idSWF::WriteXML( const char* filename ) +{ + idFileLocal file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); + if( file == NULL ) + { + return; + } + + file->WriteFloatString( "\n" ); + file->WriteFloatString( "\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); + + //file->WriteBig( BSWF_MAGIC ); + //file->WriteBig( timestamp ); + + + //file->WriteBig( frameWidth ); + //file->WriteBig( frameHeight ); + //file->WriteBig( frameRate ); + + mainsprite->WriteXML( file ); + + file->WriteFloatString( "\t\n" ); + for( int i = 0; i < dictionary.Num(); i++ ) + { + const idSWFDictionaryEntry& entry = dictionary[i]; + + //file->WriteFloatString( "\t\n", idSWF::GetDictTypeName( dictionary[i].type ) ); + switch( dictionary[i].type ) + { + case SWF_DICT_IMAGE: + { + file->WriteFloatString( "\t\tWriteFloatString( "%s\"", dictionary[i].material->GetName() ); + } + else + { + file->WriteFloatString( ".\"" ); + } + + file->WriteFloatString( " width=\"%i\" height=\"%i\" atlasOffsetX=\"%i\" atlasOffsetY=\"%i\">\n", + entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); + + file->WriteFloatString( "\t\t\t\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); + + file->WriteFloatString( "\t\t\n" ); + //for( int j = 0 ; j < 2 ; j++ ) + //{ + // file->WriteBig( dictionary[i].imageSize[j] ); + // file->WriteBig( dictionary[i].imageAtlasOffset[j] ); + //} + + //for( int j = 0 ; j < 4 ; j++ ) + //{ + // file->WriteBig( dictionary[i].channelScale[j] ); + //} + break; + } + + case SWF_DICT_MORPH: + case SWF_DICT_SHAPE: + { + idSWFShape* shape = dictionary[i].shape; + + file->WriteFloatString( "\t\t\n" ); + + float x = shape->startBounds.tl.y; + float y = shape->startBounds.tl.x; + float width = fabs( shape->startBounds.br.y - shape->startBounds.tl.y ); + float height = fabs( shape->startBounds.br.x - shape->startBounds.tl.x ); + + file->WriteFloatString( "\t\t\t\n", x, y, width, height ); + + x = shape->endBounds.tl.y; + y = shape->endBounds.tl.x; + width = fabs( shape->endBounds.br.y - shape->endBounds.tl.y ); + height = fabs( shape->endBounds.br.x - shape->endBounds.tl.x ); + + file->WriteFloatString( "\t\t\t\n", x, y, width, height ); + + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + file->WriteFloatString( "\t\t\t\n" ); + + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + + file->WriteFloatString( "\t\t\t\tWriteFloatString( "\"gradient\"" ); + } + else if( fillDraw.style.type == 4 ) + { + file->WriteFloatString( "\"bitmap\"" ); + } + else + { + file->WriteFloatString( "\"%i\"", fillDraw.style.type ); + } + + // 0 = linear, 2 = radial, 3 = focal; 0 = repeat, 1 = clamp, 2 = near repeat, 3 = near clamp + file->WriteFloatString( " subType=" ); + if( fillDraw.style.subType == 0 ) + { + file->WriteFloatString( "\"linear\"" ); + } + else if( fillDraw.style.subType == 1 ) + { + file->WriteFloatString( "\"radial\"" ); + } + else if( fillDraw.style.subType == 2 ) + { + file->WriteFloatString( "\"focal\"" ); + } + else if( fillDraw.style.subType == 3 ) + { + file->WriteFloatString( "\"near clamp\"" ); + } + else + { + file->WriteFloatString( "\"%i\"", fillDraw.style.subType ); + } + + if( fillDraw.style.type == 1 && fillDraw.style.subType == 3 ) + { + file->WriteFloatString( " focalPoint=\"%f\"", fillDraw.style.focalPoint ); + } + + if( fillDraw.style.type == 4 ) + { + file->WriteFloatString( " bitmapID=\"%i\"", fillDraw.style.bitmapID ); + } + + file->WriteFloatString( ">\n", fillDraw.style.subType ); + + if( fillDraw.style.type == 0 ) + { + idVec4 color = fillDraw.style.startColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + color = fillDraw.style.endColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + } + + if( fillDraw.style.type > 0 ) + { + swfMatrix_t m = fillDraw.style.startMatrix; + file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", + m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + + m = fillDraw.style.endMatrix; + file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", + m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + } + + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + + file->WriteFloatString( "\t\t\t\t\t\n", gr.startRatio, gr.endRatio ); + + idVec4 color = gr.startColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + color = gr.endColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + } + + file->WriteFloatString( "\t\t\t\t\n" ); + + /*file->WriteBig( fillDraw.startVerts.Num() ); + file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); + file->WriteBig( fillDraw.endVerts.Num() ); + file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); + file->WriteBig( fillDraw.indices.Num() ); + file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() );*/ + + //file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); + for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + { + const idVec2& vert = fillDraw.startVerts[v]; + + file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + } + + for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + { + const idVec2& vert = fillDraw.endVerts[v]; + + file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + } + + file->WriteFloatString( "\t\t\t\t", fillDraw.indices.Num() ); + for( int v = 0; v < fillDraw.indices.Num(); v++ ) + { + const uint16& vert = fillDraw.indices[v]; + + file->WriteFloatString( "%i ", vert ); + } + file->WriteFloatString( "\n" ); + + file->WriteFloatString( "\t\t\t\n" ); + } + + // TODO line draws + + /* + file->WriteBig( shape->startBounds.tl ); + file->WriteBig( shape->startBounds.br ); + file->WriteBig( shape->endBounds.tl ); + file->WriteBig( shape->endBounds.br ); + file->WriteBig( shape->fillDraws.Num() ); + + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + file->WriteBig( fillDraw.style.type ); + file->WriteBig( fillDraw.style.subType ); + file->Write( &fillDraw.style.startColor, 4 ); + file->Write( &fillDraw.style.endColor, 4 ); + file->WriteBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); + file->WriteBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); + file->WriteBig( fillDraw.style.gradient.numGradients ); + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + file->WriteBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); + file->WriteBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); + file->Write( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); + file->Write( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); + } + file->WriteBig( fillDraw.style.focalPoint ); + file->WriteBig( fillDraw.style.bitmapID ); + file->WriteBig( fillDraw.startVerts.Num() ); + file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); + file->WriteBig( fillDraw.endVerts.Num() ); + file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); + file->WriteBig( fillDraw.indices.Num() ); + file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() ); + } + file->WriteBig( shape->lineDraws.Num() ); + for( int d = 0; d < shape->lineDraws.Num(); d++ ) + { + idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + file->WriteBig( lineDraw.style.startWidth ); + file->WriteBig( lineDraw.style.endWidth ); + file->Write( &lineDraw.style.startColor, 4 ); + file->Write( &lineDraw.style.endColor, 4 ); + file->WriteBig( lineDraw.startVerts.Num() ); + file->WriteBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); + file->WriteBig( lineDraw.endVerts.Num() ); + file->WriteBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); + file->WriteBig( lineDraw.indices.Num() ); + file->WriteBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); + } + */ + + file->WriteFloatString( "\t\t\n" ); + break; + } + } + + //file->WriteFloatString( "\t\n" ); + } + + file->WriteFloatString( "\t" ); + +#if 0 + file->WriteBig( dictionary.Num() ); + for( int i = 0; i < dictionary.Num(); i++ ) + { + file->WriteBig( dictionary[i].type ); + switch( dictionary[i].type ) + { + case SWF_DICT_IMAGE: + { + if( dictionary[i].material ) + { + file->WriteString( dictionary[i].material->GetName() ); + } + else + { + file->WriteString( "." ); + } + for( int j = 0 ; j < 2 ; j++ ) + { + file->WriteBig( dictionary[i].imageSize[j] ); + file->WriteBig( dictionary[i].imageAtlasOffset[j] ); + } + for( int j = 0 ; j < 4 ; j++ ) + { + file->WriteBig( dictionary[i].channelScale[j] ); + } + break; + } + case SWF_DICT_MORPH: + case SWF_DICT_SHAPE: + { + idSWFShape* shape = dictionary[i].shape; + file->WriteBig( shape->startBounds.tl ); + file->WriteBig( shape->startBounds.br ); + file->WriteBig( shape->endBounds.tl ); + file->WriteBig( shape->endBounds.br ); + file->WriteBig( shape->fillDraws.Num() ); + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + file->WriteBig( fillDraw.style.type ); + file->WriteBig( fillDraw.style.subType ); + file->Write( &fillDraw.style.startColor, 4 ); + file->Write( &fillDraw.style.endColor, 4 ); + file->WriteBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); + file->WriteBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); + file->WriteBig( fillDraw.style.gradient.numGradients ); + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + file->WriteBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); + file->WriteBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); + file->Write( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); + file->Write( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); + } + file->WriteBig( fillDraw.style.focalPoint ); + file->WriteBig( fillDraw.style.bitmapID ); + file->WriteBig( fillDraw.startVerts.Num() ); + file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); + file->WriteBig( fillDraw.endVerts.Num() ); + file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); + file->WriteBig( fillDraw.indices.Num() ); + file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() ); + } + file->WriteBig( shape->lineDraws.Num() ); + for( int d = 0; d < shape->lineDraws.Num(); d++ ) + { + idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + file->WriteBig( lineDraw.style.startWidth ); + file->WriteBig( lineDraw.style.endWidth ); + file->Write( &lineDraw.style.startColor, 4 ); + file->Write( &lineDraw.style.endColor, 4 ); + file->WriteBig( lineDraw.startVerts.Num() ); + file->WriteBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); + file->WriteBig( lineDraw.endVerts.Num() ); + file->WriteBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); + file->WriteBig( lineDraw.indices.Num() ); + file->WriteBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); + } + break; + } + case SWF_DICT_SPRITE: + { + dictionary[i].sprite->Write( file ); + break; + } + case SWF_DICT_FONT: + { + idSWFFont* font = dictionary[i].font; + file->WriteString( font->fontID->GetName() ); + file->WriteBig( font->ascent ); + file->WriteBig( font->descent ); + file->WriteBig( font->leading ); + file->WriteBig( font->glyphs.Num() ); + for( int g = 0; g < font->glyphs.Num(); g++ ) + { + file->WriteBig( font->glyphs[g].code ); + file->WriteBig( font->glyphs[g].advance ); + file->WriteBig( font->glyphs[g].verts.Num() ); + file->WriteBigArray( font->glyphs[g].verts.Ptr(), font->glyphs[g].verts.Num() ); + file->WriteBig( font->glyphs[g].indices.Num() ); + file->WriteBigArray( font->glyphs[g].indices.Ptr(), font->glyphs[g].indices.Num() ); + } + break; + } + case SWF_DICT_TEXT: + { + idSWFText* text = dictionary[i].text; + file->WriteBig( text->bounds.tl ); + file->WriteBig( text->bounds.br ); + file->WriteBigArray( ( float* )&text->matrix, 6 ); + file->WriteBig( text->textRecords.Num() ); + for( int t = 0; t < text->textRecords.Num(); t++ ) + { + idSWFTextRecord& textRecord = text->textRecords[t]; + file->WriteBig( textRecord.fontID ); + file->Write( &textRecord.color, 4 ); + file->WriteBig( textRecord.xOffset ); + file->WriteBig( textRecord.yOffset ); + file->WriteBig( textRecord.textHeight ); + file->WriteBig( textRecord.firstGlyph ); + file->WriteBig( textRecord.numGlyphs ); + } + file->WriteBig( text->glyphs.Num() ); + for( int g = 0; g < text->glyphs.Num(); g++ ) + { + file->WriteBig( text->glyphs[g].index ); + file->WriteBig( text->glyphs[g].advance ); + } + break; + } + case SWF_DICT_EDITTEXT: + { + idSWFEditText* edittext = dictionary[i].edittext; + file->WriteBig( edittext->bounds.tl ); + file->WriteBig( edittext->bounds.br ); + file->WriteBig( edittext->flags ); + file->WriteBig( edittext->fontID ); + file->WriteBig( edittext->fontHeight ); + file->Write( &edittext->color, 4 ); + file->WriteBig( edittext->maxLength ); + file->WriteBig( edittext->align ); + file->WriteBig( edittext->leftMargin ); + file->WriteBig( edittext->rightMargin ); + file->WriteBig( edittext->indent ); + file->WriteBig( edittext->leading ); + file->WriteString( edittext->variable ); + file->WriteString( edittext->initialText ); + break; + } + } + } +#endif + + file->WriteFloatString( "\n" ); +} + +// RB end +>>>>>>> c4098bc... XML Flash part 1 diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index e56be060..29c84671 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -32,6 +33,9 @@ If you have questions concerning this license or the applicable additional terms #pragma warning(disable: 4355) // 'this' : used in base member initializer list idCVar swf_loadBinary( "swf_loadBinary", "1", CVAR_INTEGER, "used to set whether to load binary swf from generated" ); +// RB begin +idCVar swf_exportXML( "swf_exportXML", "1", CVAR_INTEGER, "" ); +// RB end int idSWF::mouseX = -1; int idSWF::mouseY = -1; @@ -143,6 +147,18 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) { LoadSWF( filename ); } + + // RB begin + if( swf_exportXML.GetBool() ) + { + idStr xmlFileName = "generated/"; + xmlFileName += filename; + xmlFileName.SetFileExtension( ".xml" ); + + WriteXML( xmlFileName ); + } + // RB end + idStr atlasFileName = binaryFileName; atlasFileName.SetFileExtension( ".tga" ); atlasMaterial = declManager->FindMaterial( atlasFileName ); diff --git a/neo/swf/SWF_Names.cpp b/neo/swf/SWF_Names.cpp index 39f26301..f5d575f3 100644 --- a/neo/swf/SWF_Names.cpp +++ b/neo/swf/SWF_Names.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -28,6 +29,31 @@ If you have questions concerning this license or the applicable additional terms #pragma hdrstop #include "precompiled.h" +// RB begin +/* +======================== +idSWF::GetDictTypeName +======================== +*/ +const char* idSWF::GetDictTypeName( swfDictType_t type ) +{ +#define SWF_DICT_NAME( x ) case SWF_DICT_##x: return #x; + switch( type ) + { + SWF_DICT_NAME( NULL ); + SWF_DICT_NAME( IMAGE ); + SWF_DICT_NAME( SHAPE ); + SWF_DICT_NAME( MORPH ); + SWF_DICT_NAME( SPRITE ); + SWF_DICT_NAME( FONT ); + SWF_DICT_NAME( TEXT ); + SWF_DICT_NAME( EDITTEXT ); + default: + return "????"; + } +} +// RB end + /* ======================== idSWF::GetTagName diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index b5f4bb9d..bcb49572 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -287,3 +288,55 @@ void idSWFSprite::Write( idFile* f ) f->WriteBig( doInitActions[i].Length() ); } } + + +// RB begin +/* +======================== +idSWFSprite::WriteXML +======================== +*/ +void idSWFSprite::WriteXML( idFile* f ) +{ + f->WriteFloatString( "\t\n", frameCount, frameOffsets.Num() ); + + f->WriteFloatString( "\t\t" ); + for( int i = 0; i < frameOffsets.Num(); i++ ) + { + f->WriteFloatString( "%i ", frameOffsets[i] ); + } + f->WriteFloatString( "\n" ); + + //f->WriteFloatString( "\t\t", frameLabels.Num() ); + for( int i = 0; i < frameLabels.Num(); i++ ) + { + f->WriteFloatString( "\t\t\n", frameLabels[i].frameNum, frameLabels[i].frameLabel ); + } + + + idBase64 base64; + + for( int i = 0; i < commands.Num(); i++ ) + { + base64.Encode( commands[i].stream.Ptr(), commands[i].stream.Length() ); + //base64.Decode( src ); + + //f->WriteFloatString( "\t\t%s\n", idSWF::GetTagName( commands[i].tag ), src.Length(), src.c_str() ); + + f->WriteFloatString( "\t\t%s\n", idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), base64.c_str() ); + + //f->WriteFloatString( "\t\t%s\n", idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), commands[i].stream.Ptr() ); + } + + for( int i = 0; i < doInitActions.Num(); i++ ) + { + base64.Encode( doInitActions[i].Ptr(), doInitActions[i].Length() ); + + f->WriteFloatString( "\t\t%s\n", doInitActions[i].Length(), base64.c_str() ); + + //f->WriteFloatString( "\t\t%s\n", doInitActions[i].Length(), doInitActions[i].Ptr() ); + } + + f->WriteFloatString( "\t\n" ); +} +// RB end diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index 7b86e9c9..828a3463 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -46,6 +47,10 @@ public: void Read( idFile* f ); void Write( idFile* f ); + // RB begin + void WriteXML( idFile* f ); + // RB end + class idSWF* GetSWF() { return swf; From 87283f7a7047cc5db9a0c5e6c36f1be8bb2d486c Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 25 Mar 2013 21:17:11 +0100 Subject: [PATCH 02/43] XML Flash part 2 --- neo/swf/SWF_Load.cpp | 122 +++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 74 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index d36cde33..37787615 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -585,14 +585,6 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\n" ); file->WriteFloatString( "\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); - //file->WriteBig( BSWF_MAGIC ); - //file->WriteBig( timestamp ); - - - //file->WriteBig( frameWidth ); - //file->WriteBig( frameHeight ); - //file->WriteBig( frameRate ); - mainsprite->WriteXML( file ); file->WriteFloatString( "\t\n" ); @@ -621,16 +613,6 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); file->WriteFloatString( "\t\t\n" ); - //for( int j = 0 ; j < 2 ; j++ ) - //{ - // file->WriteBig( dictionary[i].imageSize[j] ); - // file->WriteBig( dictionary[i].imageAtlasOffset[j] ); - //} - - //for( int j = 0 ; j < 4 ; j++ ) - //{ - // file->WriteBig( dictionary[i].channelScale[j] ); - //} break; } @@ -655,6 +637,8 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\n", x, y, width, height ); + // export fill draws + for( int d = 0; d < shape->fillDraws.Num(); d++ ) { file->WriteFloatString( "\t\t\t\n" ); @@ -755,14 +739,6 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\t\n" ); - /*file->WriteBig( fillDraw.startVerts.Num() ); - file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); - file->WriteBig( fillDraw.endVerts.Num() ); - file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); - file->WriteBig( fillDraw.indices.Num() ); - file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() );*/ - - //file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) { const idVec2& vert = fillDraw.startVerts[v]; @@ -789,67 +765,65 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\n" ); } - // TODO line draws + // export line draws - /* - file->WriteBig( shape->startBounds.tl ); - file->WriteBig( shape->startBounds.br ); - file->WriteBig( shape->endBounds.tl ); - file->WriteBig( shape->endBounds.br ); - file->WriteBig( shape->fillDraws.Num() ); - - for( int d = 0; d < shape->fillDraws.Num(); d++ ) - { - idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - file->WriteBig( fillDraw.style.type ); - file->WriteBig( fillDraw.style.subType ); - file->Write( &fillDraw.style.startColor, 4 ); - file->Write( &fillDraw.style.endColor, 4 ); - file->WriteBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); - file->WriteBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); - file->WriteBig( fillDraw.style.gradient.numGradients ); - for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) - { - file->WriteBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); - file->WriteBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); - file->Write( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); - file->Write( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); - } - file->WriteBig( fillDraw.style.focalPoint ); - file->WriteBig( fillDraw.style.bitmapID ); - file->WriteBig( fillDraw.startVerts.Num() ); - file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); - file->WriteBig( fillDraw.endVerts.Num() ); - file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); - file->WriteBig( fillDraw.indices.Num() ); - file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() ); - } - file->WriteBig( shape->lineDraws.Num() ); for( int d = 0; d < shape->lineDraws.Num(); d++ ) { - idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; - file->WriteBig( lineDraw.style.startWidth ); - file->WriteBig( lineDraw.style.endWidth ); - file->Write( &lineDraw.style.startColor, 4 ); - file->Write( &lineDraw.style.endColor, 4 ); - file->WriteBig( lineDraw.startVerts.Num() ); - file->WriteBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); - file->WriteBig( lineDraw.endVerts.Num() ); - file->WriteBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); - file->WriteBig( lineDraw.indices.Num() ); - file->WriteBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); + const idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + + file->WriteFloatString( "\t\t\t\n" ); + + file->WriteFloatString( "\t\t\t\t\n", lineDraw.style.startWidth, lineDraw.style.endWidth ); + + idVec4 color = lineDraw.style.startColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + color = lineDraw.style.endColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + file->WriteFloatString( "\t\t\t\t\n" ); + + for( int v = 0; v < lineDraw.startVerts.Num(); v++ ) + { + const idVec2& vert = lineDraw.startVerts[v]; + + file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + } + + for( int v = 0; v < lineDraw.endVerts.Num(); v++ ) + { + const idVec2& vert = lineDraw.endVerts[v]; + + file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + } + + file->WriteFloatString( "\t\t\t\t", lineDraw.indices.Num() ); + for( int v = 0; v < lineDraw.indices.Num(); v++ ) + { + const uint16& vert = lineDraw.indices[v]; + + file->WriteFloatString( "%i ", vert ); + } + file->WriteFloatString( "\n" ); } - */ file->WriteFloatString( "\t\t\n" ); break; } + + case SWF_DICT_SPRITE: + { + dictionary[i].sprite->Write( file ); + break; + } } //file->WriteFloatString( "\t\n" ); } - file->WriteFloatString( "\t" ); + file->WriteFloatString( "\t\n" ); #if 0 file->WriteBig( dictionary.Num() ); From e4c0d05b7eb59e45993dc1f1d4ab9943bc3280ab Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 26 Mar 2013 00:06:38 +0100 Subject: [PATCH 03/43] XML Flash almost done --- neo/swf/SWF.h | 1 + neo/swf/SWF_Load.cpp | 291 +++++++++++++++++++--------------------- neo/swf/SWF_Names.cpp | 19 ++- neo/swf/SWF_Sprites.cpp | 20 +-- neo/swf/SWF_Sprites.h | 2 +- neo/swf/SWF_Types.h | 1 + 6 files changed, 163 insertions(+), 171 deletions(-) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index 1fb63773..4832ba6d 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -505,6 +505,7 @@ public: //---------------------------------- // RB begin static const char* GetDictTypeName( swfDictType_t type ); + static const char* GetEditTextAlignName( swfEditTextAlign_t align ); // RB end static const char* GetTagName( swfTag_t tag ); static const char* GetActionName( swfAction_t action ); diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 37787615..abaa227d 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -585,7 +585,7 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\n" ); file->WriteFloatString( "\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); - mainsprite->WriteXML( file ); + mainsprite->WriteXML( file, "\t" ); file->WriteFloatString( "\t\n" ); for( int i = 0; i < dictionary.Num(); i++ ) @@ -815,7 +815,141 @@ void idSWF::WriteXML( const char* filename ) case SWF_DICT_SPRITE: { - dictionary[i].sprite->Write( file ); + dictionary[i].sprite->WriteXML( file, "\t\t" ); + break; + } + + case SWF_DICT_FONT: + { + const idSWFFont* font = dictionary[i].font; + + file->WriteFloatString( "\t\t\n", + font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); + + for( int g = 0; g < font->glyphs.Num(); g++ ) + { + file->WriteFloatString( "\t\t\t\n", font->glyphs[g].code, font->glyphs[g].advance ); + +#if 0 + for( int v = 0; v < font->glyphs[g].verts.Num(); v++ ) + { + const idVec2& vert = font->glyphs[g].verts[v]; + + file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + } + + file->WriteFloatString( "\t\t\t\t", font->glyphs[g].indices.Num() ); + for( int v = 0; v < font->glyphs[g].indices.Num(); v++ ) + { + const uint16& vert = font->glyphs[g].indices[v]; + + file->WriteFloatString( "%i ", vert ); + } + file->WriteFloatString( "\n" ); + + file->WriteFloatString( "\t\t\t\n" ); +#endif + } + break; + } + + case SWF_DICT_TEXT: + { + const idSWFText* text = dictionary[i].text; + + file->WriteFloatString( "\t\t\t\n" ); + + float x = text->bounds.tl.y; + float y = text->bounds.tl.x; + float width = fabs( text->bounds.br.y - text->bounds.tl.y ); + float height = fabs( text->bounds.br.x - text->bounds.tl.x ); + + file->WriteFloatString( "\t\t\t\t\n", x, y, width, height ); + + //file->WriteBig( text->bounds.tl ); + //file->WriteBig( text->bounds.br ); + + //file->WriteBigArray( ( float* )&text->matrix, 6 ); + + swfMatrix_t m = text->matrix; + file->WriteFloatString( "\t\t\t\t%f %f %f %f %f %f\n", + m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + + //file->WriteBig( text->textRecords.Num() ); + for( int t = 0; t < text->textRecords.Num(); t++ ) + { + const idSWFTextRecord& textRecord = text->textRecords[t]; + + file->WriteFloatString( "\t\t\t\t\n", + textRecord.fontID, textRecord.xOffset, textRecord.yOffset, textRecord.textHeight, textRecord.firstGlyph, textRecord.numGlyphs ); + + idVec4 color = textRecord.color.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + file->WriteFloatString( "\t\t\t\t\n" ); + + /*file->WriteBig( textRecord.fontID ); + file->Write( &textRecord.color, 4 ); + file->WriteBig( textRecord.xOffset ); + file->WriteBig( textRecord.yOffset ); + file->WriteBig( textRecord.textHeight ); + file->WriteBig( textRecord.firstGlyph ); + file->WriteBig( textRecord.numGlyphs );*/ + } + + for( int g = 0; g < text->glyphs.Num(); g++ ) + { + file->WriteFloatString( "\t\t\t\t\n", text->glyphs[g].index, text->glyphs[g].advance ); + } + + /* + file->WriteBig( text->glyphs.Num() ); + for( int g = 0; g < text->glyphs.Num(); g++ ) + { + file->WriteBig( text->glyphs[g].index ); + file->WriteBig( text->glyphs[g].advance ); + } + */ + break; + } + + case SWF_DICT_EDITTEXT: + { + const idSWFEditText* et = dictionary[i].edittext; + + file->WriteFloatString( "\t\t\t\n", + et->flags, et->fontID, et->fontHeight, et->maxLength, idSWF::GetEditTextAlignName( et->align ), + et->leftMargin, et->rightMargin, et->indent, et->leading, + et->variable.c_str(), et->initialText.c_str() ); + + float x = et->bounds.tl.y; + float y = et->bounds.tl.x; + float width = fabs( et->bounds.br.y - et->bounds.tl.y ); + float height = fabs( et->bounds.br.x - et->bounds.tl.x ); + + file->WriteFloatString( "\t\t\t\t\n", x, y, width, height ); + + idVec4 color = et->color.ToVec4(); + file->WriteFloatString( "\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + file->WriteFloatString( "\t\t\t\n" ); + + //file->WriteBig( et->bounds.tl ); + //file->WriteBig( et->bounds.br ); + //file->WriteBig( et->flags ); + //file->WriteBig( et->fontID ); + //file->WriteBig( et->fontHeight ); + //file->Write( &et->color, 4 ); + //file->WriteBig( et->maxLength ); + //file->WriteBig( et->align ); + //file->WriteBig( et->leftMargin ); + //file->WriteBig( et->rightMargin ); + //file->WriteBig( et->indent ); + //file->WriteBig( et->leading ); + //file->WriteString( et->variable ); + //file->WriteString( et->initialText ); break; } } @@ -825,159 +959,6 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\n" ); -#if 0 - file->WriteBig( dictionary.Num() ); - for( int i = 0; i < dictionary.Num(); i++ ) - { - file->WriteBig( dictionary[i].type ); - switch( dictionary[i].type ) - { - case SWF_DICT_IMAGE: - { - if( dictionary[i].material ) - { - file->WriteString( dictionary[i].material->GetName() ); - } - else - { - file->WriteString( "." ); - } - for( int j = 0 ; j < 2 ; j++ ) - { - file->WriteBig( dictionary[i].imageSize[j] ); - file->WriteBig( dictionary[i].imageAtlasOffset[j] ); - } - for( int j = 0 ; j < 4 ; j++ ) - { - file->WriteBig( dictionary[i].channelScale[j] ); - } - break; - } - case SWF_DICT_MORPH: - case SWF_DICT_SHAPE: - { - idSWFShape* shape = dictionary[i].shape; - file->WriteBig( shape->startBounds.tl ); - file->WriteBig( shape->startBounds.br ); - file->WriteBig( shape->endBounds.tl ); - file->WriteBig( shape->endBounds.br ); - file->WriteBig( shape->fillDraws.Num() ); - for( int d = 0; d < shape->fillDraws.Num(); d++ ) - { - idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - file->WriteBig( fillDraw.style.type ); - file->WriteBig( fillDraw.style.subType ); - file->Write( &fillDraw.style.startColor, 4 ); - file->Write( &fillDraw.style.endColor, 4 ); - file->WriteBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); - file->WriteBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); - file->WriteBig( fillDraw.style.gradient.numGradients ); - for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) - { - file->WriteBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); - file->WriteBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); - file->Write( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); - file->Write( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); - } - file->WriteBig( fillDraw.style.focalPoint ); - file->WriteBig( fillDraw.style.bitmapID ); - file->WriteBig( fillDraw.startVerts.Num() ); - file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); - file->WriteBig( fillDraw.endVerts.Num() ); - file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); - file->WriteBig( fillDraw.indices.Num() ); - file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() ); - } - file->WriteBig( shape->lineDraws.Num() ); - for( int d = 0; d < shape->lineDraws.Num(); d++ ) - { - idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; - file->WriteBig( lineDraw.style.startWidth ); - file->WriteBig( lineDraw.style.endWidth ); - file->Write( &lineDraw.style.startColor, 4 ); - file->Write( &lineDraw.style.endColor, 4 ); - file->WriteBig( lineDraw.startVerts.Num() ); - file->WriteBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); - file->WriteBig( lineDraw.endVerts.Num() ); - file->WriteBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); - file->WriteBig( lineDraw.indices.Num() ); - file->WriteBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); - } - break; - } - case SWF_DICT_SPRITE: - { - dictionary[i].sprite->Write( file ); - break; - } - case SWF_DICT_FONT: - { - idSWFFont* font = dictionary[i].font; - file->WriteString( font->fontID->GetName() ); - file->WriteBig( font->ascent ); - file->WriteBig( font->descent ); - file->WriteBig( font->leading ); - file->WriteBig( font->glyphs.Num() ); - for( int g = 0; g < font->glyphs.Num(); g++ ) - { - file->WriteBig( font->glyphs[g].code ); - file->WriteBig( font->glyphs[g].advance ); - file->WriteBig( font->glyphs[g].verts.Num() ); - file->WriteBigArray( font->glyphs[g].verts.Ptr(), font->glyphs[g].verts.Num() ); - file->WriteBig( font->glyphs[g].indices.Num() ); - file->WriteBigArray( font->glyphs[g].indices.Ptr(), font->glyphs[g].indices.Num() ); - } - break; - } - case SWF_DICT_TEXT: - { - idSWFText* text = dictionary[i].text; - file->WriteBig( text->bounds.tl ); - file->WriteBig( text->bounds.br ); - file->WriteBigArray( ( float* )&text->matrix, 6 ); - file->WriteBig( text->textRecords.Num() ); - for( int t = 0; t < text->textRecords.Num(); t++ ) - { - idSWFTextRecord& textRecord = text->textRecords[t]; - file->WriteBig( textRecord.fontID ); - file->Write( &textRecord.color, 4 ); - file->WriteBig( textRecord.xOffset ); - file->WriteBig( textRecord.yOffset ); - file->WriteBig( textRecord.textHeight ); - file->WriteBig( textRecord.firstGlyph ); - file->WriteBig( textRecord.numGlyphs ); - } - file->WriteBig( text->glyphs.Num() ); - for( int g = 0; g < text->glyphs.Num(); g++ ) - { - file->WriteBig( text->glyphs[g].index ); - file->WriteBig( text->glyphs[g].advance ); - } - break; - } - case SWF_DICT_EDITTEXT: - { - idSWFEditText* edittext = dictionary[i].edittext; - file->WriteBig( edittext->bounds.tl ); - file->WriteBig( edittext->bounds.br ); - file->WriteBig( edittext->flags ); - file->WriteBig( edittext->fontID ); - file->WriteBig( edittext->fontHeight ); - file->Write( &edittext->color, 4 ); - file->WriteBig( edittext->maxLength ); - file->WriteBig( edittext->align ); - file->WriteBig( edittext->leftMargin ); - file->WriteBig( edittext->rightMargin ); - file->WriteBig( edittext->indent ); - file->WriteBig( edittext->leading ); - file->WriteString( edittext->variable ); - file->WriteString( edittext->initialText ); - break; - } - } - } -#endif - file->WriteFloatString( "\n" ); } diff --git a/neo/swf/SWF_Names.cpp b/neo/swf/SWF_Names.cpp index f5d575f3..6594022d 100644 --- a/neo/swf/SWF_Names.cpp +++ b/neo/swf/SWF_Names.cpp @@ -30,11 +30,6 @@ If you have questions concerning this license or the applicable additional terms #include "precompiled.h" // RB begin -/* -======================== -idSWF::GetDictTypeName -======================== -*/ const char* idSWF::GetDictTypeName( swfDictType_t type ) { #define SWF_DICT_NAME( x ) case SWF_DICT_##x: return #x; @@ -52,6 +47,20 @@ const char* idSWF::GetDictTypeName( swfDictType_t type ) return "????"; } } + +const char* idSWF::GetEditTextAlignName( swfEditTextAlign_t align ) +{ +#define SWF_ET_ALIGN_NAME( x ) case SWF_ET_ALIGN_##x: return #x; + switch( align ) + { + SWF_ET_ALIGN_NAME( LEFT ); + SWF_ET_ALIGN_NAME( RIGHT ); + SWF_ET_ALIGN_NAME( CENTER ); + SWF_ET_ALIGN_NAME( JUSTIFY ); + default: + return "????"; + } +} // RB end /* diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index bcb49572..def76d3e 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -296,11 +296,11 @@ void idSWFSprite::Write( idFile* f ) idSWFSprite::WriteXML ======================== */ -void idSWFSprite::WriteXML( idFile* f ) +void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix ) { - f->WriteFloatString( "\t\n", frameCount, frameOffsets.Num() ); + f->WriteFloatString( "%s\n", indentPrefix, frameCount, frameOffsets.Num() ); - f->WriteFloatString( "\t\t" ); + f->WriteFloatString( "%s\t", indentPrefix ); for( int i = 0; i < frameOffsets.Num(); i++ ) { f->WriteFloatString( "%i ", frameOffsets[i] ); @@ -310,7 +310,7 @@ void idSWFSprite::WriteXML( idFile* f ) //f->WriteFloatString( "\t\t", frameLabels.Num() ); for( int i = 0; i < frameLabels.Num(); i++ ) { - f->WriteFloatString( "\t\t\n", frameLabels[i].frameNum, frameLabels[i].frameLabel ); + f->WriteFloatString( "%s\t\n", indentPrefix, frameLabels[i].frameNum, frameLabels[i].frameLabel ); } @@ -321,22 +321,22 @@ void idSWFSprite::WriteXML( idFile* f ) base64.Encode( commands[i].stream.Ptr(), commands[i].stream.Length() ); //base64.Decode( src ); - //f->WriteFloatString( "\t\t%s\n", idSWF::GetTagName( commands[i].tag ), src.Length(), src.c_str() ); + //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), src.Length(), src.c_str() ); - f->WriteFloatString( "\t\t%s\n", idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), base64.c_str() ); + f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), base64.c_str() ); - //f->WriteFloatString( "\t\t%s\n", idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), commands[i].stream.Ptr() ); + //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), commands[i].stream.Ptr() ); } for( int i = 0; i < doInitActions.Num(); i++ ) { base64.Encode( doInitActions[i].Ptr(), doInitActions[i].Length() ); - f->WriteFloatString( "\t\t%s\n", doInitActions[i].Length(), base64.c_str() ); + f->WriteFloatString( "%s\t%s\n", indentPrefix, doInitActions[i].Length(), base64.c_str() ); - //f->WriteFloatString( "\t\t%s\n", doInitActions[i].Length(), doInitActions[i].Ptr() ); + //f->WriteFloatString( "%s\t%s\n", indentPrefix, doInitActions[i].Length(), doInitActions[i].Ptr() ); } - f->WriteFloatString( "\t\n" ); + f->WriteFloatString( "%s\n", indentPrefix ); } // RB end diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index 828a3463..e10361ef 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -48,7 +48,7 @@ public: void Write( idFile* f ); // RB begin - void WriteXML( idFile* f ); + void WriteXML( idFile* f, const char* indentPrefix = "" ); // RB end class idSWF* GetSWF() diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 228bcd10..7158d7cc 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -158,6 +158,7 @@ public: idSWFFontGlyph(); uint16 code; int16 advance; + // RB: verts and indices are not used by the renderer idList< idVec2, TAG_SWF > verts; idList< uint16, TAG_SWF > indices; }; From 83c76f576d102ad7037aefb5acb931223a1e68ab Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 26 Mar 2013 00:27:58 +0100 Subject: [PATCH 04/43] XML Flash bugfixes --- neo/swf/SWF_Load.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index abaa227d..64625bff 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -850,6 +850,7 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\n" ); #endif } + file->WriteFloatString( "\t\t\n" ); break; } @@ -857,14 +858,14 @@ void idSWF::WriteXML( const char* filename ) { const idSWFText* text = dictionary[i].text; - file->WriteFloatString( "\t\t\t\n" ); + file->WriteFloatString( "\t\t\n" ); float x = text->bounds.tl.y; float y = text->bounds.tl.x; float width = fabs( text->bounds.br.y - text->bounds.tl.y ); float height = fabs( text->bounds.br.x - text->bounds.tl.x ); - file->WriteFloatString( "\t\t\t\t\n", x, y, width, height ); + file->WriteFloatString( "\t\t\t\n", x, y, width, height ); //file->WriteBig( text->bounds.tl ); //file->WriteBig( text->bounds.br ); @@ -872,7 +873,7 @@ void idSWF::WriteXML( const char* filename ) //file->WriteBigArray( ( float* )&text->matrix, 6 ); swfMatrix_t m = text->matrix; - file->WriteFloatString( "\t\t\t\t%f %f %f %f %f %f\n", + file->WriteFloatString( "\t\t\t%f %f %f %f %f %f\n", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); //file->WriteBig( text->textRecords.Num() ); @@ -911,6 +912,8 @@ void idSWF::WriteXML( const char* filename ) file->WriteBig( text->glyphs[g].advance ); } */ + + file->WriteFloatString( "\t\t\n" ); break; } @@ -918,7 +921,7 @@ void idSWF::WriteXML( const char* filename ) { const idSWFEditText* et = dictionary[i].edittext; - file->WriteFloatString( "\t\t\t\n", + file->WriteFloatString( "\t\t\n", et->flags, et->fontID, et->fontHeight, et->maxLength, idSWF::GetEditTextAlignName( et->align ), et->leftMargin, et->rightMargin, et->indent, et->leading, et->variable.c_str(), et->initialText.c_str() ); @@ -928,13 +931,13 @@ void idSWF::WriteXML( const char* filename ) float width = fabs( et->bounds.br.y - et->bounds.tl.y ); float height = fabs( et->bounds.br.x - et->bounds.tl.x ); - file->WriteFloatString( "\t\t\t\t\n", x, y, width, height ); + file->WriteFloatString( "\t\t\t\n", x, y, width, height ); idVec4 color = et->color.ToVec4(); - file->WriteFloatString( "\t\t\t\t\n", + file->WriteFloatString( "\t\t\t\n", color.x, color.y, color.z, color.w ); - file->WriteFloatString( "\t\t\t\n" ); + file->WriteFloatString( "\t\t\n" ); //file->WriteBig( et->bounds.tl ); //file->WriteBig( et->bounds.br ); From ebd9df1202725b39e05258942d36ad3ba240b5e2 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 26 Mar 2013 18:07:01 +0100 Subject: [PATCH 05/43] Extended Shape-Command exports --- neo/swf/SWF_Load.cpp | 15 +++-- neo/swf/SWF_PlaceObject.cpp | 16 ----- neo/swf/SWF_SpriteInstance.h | 20 ++++++ neo/swf/SWF_Sprites.cpp | 121 +++++++++++++++++++++++++++++++++-- neo/swf/SWF_Sprites.h | 3 +- 5 files changed, 147 insertions(+), 28 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 64625bff..56930e4b 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -597,7 +597,7 @@ void idSWF::WriteXML( const char* filename ) { case SWF_DICT_IMAGE: { - file->WriteFloatString( "\t\tWriteFloatString( "\t\tWriteFloatString( "%s\"", dictionary[i].material->GetName() ); @@ -621,7 +621,7 @@ void idSWF::WriteXML( const char* filename ) { idSWFShape* shape = dictionary[i].shape; - file->WriteFloatString( "\t\t\n" ); + file->WriteFloatString( "\t\t\n", i ); float x = shape->startBounds.tl.y; float y = shape->startBounds.tl.x; @@ -815,7 +815,7 @@ void idSWF::WriteXML( const char* filename ) case SWF_DICT_SPRITE: { - dictionary[i].sprite->WriteXML( file, "\t\t" ); + dictionary[i].sprite->WriteXML( file, "\t\t", i ); break; } @@ -823,8 +823,8 @@ void idSWF::WriteXML( const char* filename ) { const idSWFFont* font = dictionary[i].font; - file->WriteFloatString( "\t\t\n", - font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); + file->WriteFloatString( "\t\t\n", + i, font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); for( int g = 0; g < font->glyphs.Num(); g++ ) { @@ -858,7 +858,7 @@ void idSWF::WriteXML( const char* filename ) { const idSWFText* text = dictionary[i].text; - file->WriteFloatString( "\t\t\n" ); + file->WriteFloatString( "\t\t\n", i ); float x = text->bounds.tl.y; float y = text->bounds.tl.x; @@ -921,7 +921,8 @@ void idSWF::WriteXML( const char* filename ) { const idSWFEditText* et = dictionary[i].edittext; - file->WriteFloatString( "\t\t\n", + file->WriteFloatString( "\t\t\n", + i, et->flags, et->fontID, et->fontHeight, et->maxLength, idSWF::GetEditTextAlignName( et->align ), et->leftMargin, et->rightMargin, et->indent, et->leading, et->variable.c_str(), et->initialText.c_str() ); diff --git a/neo/swf/SWF_PlaceObject.cpp b/neo/swf/SWF_PlaceObject.cpp index 063b2dfe..91f77de1 100644 --- a/neo/swf/SWF_PlaceObject.cpp +++ b/neo/swf/SWF_PlaceObject.cpp @@ -31,23 +31,7 @@ If you have questions concerning this license or the applicable additional terms int c_PlaceObject2; int c_PlaceObject3; -#define PlaceFlagHasClipActions BIT( 7 ) -#define PlaceFlagHasClipDepth BIT( 6 ) -#define PlaceFlagHasName BIT( 5 ) -#define PlaceFlagHasRatio BIT( 4 ) -#define PlaceFlagHasColorTransform BIT( 3 ) -#define PlaceFlagHasMatrix BIT( 2 ) -#define PlaceFlagHasCharacter BIT( 1 ) -#define PlaceFlagMove BIT( 0 ) -#define PlaceFlagPad0 BIT( 7 ) -#define PlaceFlagPad1 BIT( 6 ) -#define PlaceFlagPad2 BIT( 5 ) -#define PlaceFlagHasImage BIT( 4 ) -#define PlaceFlagHasClassName BIT( 3 ) -#define PlaceFlagCacheAsBitmap BIT( 2 ) -#define PlaceFlagHasBlendMode BIT( 1 ) -#define PlaceFlagHasFilterList BIT( 0 ) /* ======================== diff --git a/neo/swf/SWF_SpriteInstance.h b/neo/swf/SWF_SpriteInstance.h index 38c38461..51acca96 100644 --- a/neo/swf/SWF_SpriteInstance.h +++ b/neo/swf/SWF_SpriteInstance.h @@ -28,6 +28,26 @@ If you have questions concerning this license or the applicable additional terms #ifndef __SWF_SPRITEINSTANCE_H__ #define __SWF_SPRITEINSTANCE_H__ +// RB: moved here +#define PlaceFlagHasClipActions BIT( 7 ) +#define PlaceFlagHasClipDepth BIT( 6 ) +#define PlaceFlagHasName BIT( 5 ) +#define PlaceFlagHasRatio BIT( 4 ) +#define PlaceFlagHasColorTransform BIT( 3 ) +#define PlaceFlagHasMatrix BIT( 2 ) +#define PlaceFlagHasCharacter BIT( 1 ) +#define PlaceFlagMove BIT( 0 ) + +#define PlaceFlagPad0 BIT( 7 ) +#define PlaceFlagPad1 BIT( 6 ) +#define PlaceFlagPad2 BIT( 5 ) +#define PlaceFlagHasImage BIT( 4 ) +#define PlaceFlagHasClassName BIT( 3 ) +#define PlaceFlagCacheAsBitmap BIT( 2 ) +#define PlaceFlagHasBlendMode BIT( 1 ) +#define PlaceFlagHasFilterList BIT( 0 ) +// RB end + /* ================================================ There can be multiple instances of a single sprite running diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index def76d3e..d5e8a749 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -296,9 +296,16 @@ void idSWFSprite::Write( idFile* f ) idSWFSprite::WriteXML ======================== */ -void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix ) +void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID ) { - f->WriteFloatString( "%s\n", indentPrefix, frameCount, frameOffsets.Num() ); + if( characterID >= 0 ) + { + f->WriteFloatString( "%s\n", indentPrefix, characterID, frameCount, frameOffsets.Num() ); + } + else + { + f->WriteFloatString( "%s\n", indentPrefix, frameCount, frameOffsets.Num() ); + } f->WriteFloatString( "%s\t", indentPrefix ); for( int i = 0; i < frameOffsets.Num(); i++ ) @@ -318,14 +325,40 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix ) for( int i = 0; i < commands.Num(); i++ ) { - base64.Encode( commands[i].stream.Ptr(), commands[i].stream.Length() ); + idSWFSprite::swfSpriteCommand_t& command = commands[i]; + + base64.Encode( command.stream.Ptr(), command.stream.Length() ); //base64.Decode( src ); //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), src.Length(), src.c_str() ); - f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), base64.c_str() ); + //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), base64.c_str() ); + + f->WriteFloatString( "%s\t\n", indentPrefix, idSWF::GetTagName( command.tag ), command.stream.Length(), base64.c_str() ); + f->WriteFloatString( "%s\t\t%s\n", indentPrefix, base64.c_str() ); //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), commands[i].stream.Ptr() ); + + command.stream.Rewind(); + switch( command.tag ) + { + //case Tag_PlaceObject2: + // WriteXML_PlaceObject2( command.stream, indentPrefix ); + // break; + +#define HANDLE_SWF_TAG( x ) case Tag_##x: WriteXML_##x( f, command.stream, indentPrefix ); break; + HANDLE_SWF_TAG( PlaceObject2 ); + //HANDLE_SWF_TAG( PlaceObject3 ); + //HANDLE_SWF_TAG( RemoveObject2 ); + //HANDLE_SWF_TAG( StartSound ); + //HANDLE_SWF_TAG( DoAction ); +#undef HANDLE_SWF_TAG + default: + break; + //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); + } + + f->WriteFloatString( "%s\t\n", indentPrefix ); } for( int i = 0; i < doInitActions.Num(); i++ ) @@ -339,4 +372,84 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix ) f->WriteFloatString( "%s\n", indentPrefix ); } +<<<<<<< HEAD // RB end +======= + +void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +{ + uint64 flags = bitstream.ReadU8(); + int depth = bitstream.ReadU16(); + + file->WriteFloatString( "%s\t\tWriteFloatString( " characterID=\"%i\"", characterID ); + } + + file->WriteFloatString( ">\n" ); + + if( ( flags & PlaceFlagHasMatrix ) != 0 ) + { + swfMatrix_t m; + + bitstream.ReadMatrix( m ); + + file->WriteFloatString( "%s\t\t\t%f %f %f %f %f %f\n", indentPrefix, m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + } + + if( ( flags & PlaceFlagHasColorTransform ) != 0 ) + { + swfColorXform_t cxf; + bitstream.ReadColorXFormRGBA( cxf ); + + idVec4 color = cxf.mul; + file->WriteFloatString( "%s\t\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + + color = cxf.add; + file->WriteFloatString( "%s\t\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + } + + if( ( flags & PlaceFlagHasRatio ) != 0 ) + { + float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); + + file->WriteFloatString( "%s\t\t\t%f\n", indentPrefix, ratio ); + } + + if( ( flags & PlaceFlagHasName ) != 0 ) + { + idStr name = bitstream.ReadString(); + + file->WriteFloatString( "%s\t\t\t%s\n", indentPrefix, name.c_str() ); + + /*if( display->spriteInstance ) + { + display->spriteInstance->name = name; + scriptObject->Set( name, display->spriteInstance->GetScriptObject() ); + } + else if( display->textInstance ) + { + scriptObject->Set( name, display->textInstance->GetScriptObject() ); + }*/ + } + + if( ( flags & PlaceFlagHasClipDepth ) != 0 ) + { + uint16 clipDepth = bitstream.ReadU16(); + file->WriteFloatString( "%s\t\t\t%i\n", indentPrefix, clipDepth ); + } + + if( ( flags & PlaceFlagHasClipActions ) != 0 ) + { + // FIXME: clip actions + } + + file->WriteFloatString( "%s\t\t\n", indentPrefix, flags, depth ); +} + +// RB end +>>>>>>> 2f86bde... Extended Shape-Command exports diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index e10361ef..ed2704c5 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -48,7 +48,8 @@ public: void Write( idFile* f ); // RB begin - void WriteXML( idFile* f, const char* indentPrefix = "" ); + void WriteXML( idFile* f, const char* indentPrefix = "", int characterID = -1 ); + void WriteXML_PlaceObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); // RB end class idSWF* GetSWF() From 15fe2a2ae689cb93252477eb779af9240b5450ce Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Wed, 27 Mar 2013 13:15:09 +0100 Subject: [PATCH 06/43] Added swf_skip* cvars --- neo/swf/SWF_Load.cpp | 14 ++++-- neo/swf/SWF_Render.cpp | 104 +++++++++++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 39 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 56930e4b..56b5ee89 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -641,10 +641,15 @@ void idSWF::WriteXML( const char* filename ) for( int d = 0; d < shape->fillDraws.Num(); d++ ) { - file->WriteFloatString( "\t\t\t\n" ); - idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + if( fillDraw.style.type != 4 ) + { + continue; + } + + file->WriteFloatString( "\t\t\t\n" ); + file->WriteFloatString( "\t\t\t\tWriteFloatString( ">\n", fillDraw.style.subType ); + file->WriteFloatString( ">\n" ); if( fillDraw.style.type == 0 ) { @@ -766,7 +771,7 @@ void idSWF::WriteXML( const char* filename ) } // export line draws - +#if 0 for( int d = 0; d < shape->lineDraws.Num(); d++ ) { const idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; @@ -808,6 +813,7 @@ void idSWF::WriteXML( const char* filename ) } file->WriteFloatString( "\n" ); } +#endif file->WriteFloatString( "\t\t\n" ); break; diff --git a/neo/swf/SWF_Render.cpp b/neo/swf/SWF_Render.cpp index 45b2fcae..aa99543b 100644 --- a/neo/swf/SWF_Render.cpp +++ b/neo/swf/SWF_Render.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -36,6 +37,13 @@ idCVar swf_titleSafe( "swf_titleSafe", "0.005", CVAR_FLOAT, "space between UI el idCVar swf_forceAlpha( "swf_forceAlpha", "0", CVAR_FLOAT, "force an alpha value on all elements, useful to show invisible animating elements", 0.0f, 1.0f ); +// RB begin +idCVar swf_skipSolids( "swf_skipSolids", "0", CVAR_BOOL, "" ); +idCVar swf_skipGradients( "swf_skipGradients", "0", CVAR_BOOL, "" ); +idCVar swf_skipLineDraws( "swf_skipLineDraws", "0", CVAR_BOOL, "" ); +idCVar swf_skipBitmaps( "swf_skipBitmaps", "0", CVAR_BOOL, "" ); +// RB end + extern idCVar swf_textStrokeSize; extern idCVar swf_textStrokeSizeGlyphSpacer; extern idCVar in_useJoystick; @@ -650,11 +658,25 @@ void idSWF::RenderShape( idRenderSystem* gui, const idSWFShape* shape, const swf } else if( fill.style.type == 0 ) { + // RB begin + if( swf_skipSolids.GetBool() ) + { + continue; + } + // RB end + material = guiSolid; color.mul = fill.style.startColor.ToVec4(); } else if( fill.style.type == 4 && fill.style.bitmapID != 65535 ) { + // RB begin + if( swf_skipBitmaps.GetBool() ) + { + continue; + } + // RB end + // everything in a single image atlas idSWFDictionaryEntry* entry = &dictionary[ fill.style.bitmapID ]; material = atlasMaterial; @@ -674,6 +696,13 @@ void idSWF::RenderShape( idRenderSystem* gui, const idSWFShape* shape, const swf } else { + // RB begin + if( fill.style.type == 1 && swf_skipGradients.GetBool() ) + { + continue; + } + // RB end + material = guiSolid; } color = color.Multiply( renderState.cxf ); @@ -755,48 +784,53 @@ void idSWF::RenderShape( idRenderSystem* gui, const idSWFShape* shape, const swf WriteDrawVerts16( & verts[fill.startVerts.Num() & ~3], tempVerts, fill.startVerts.Num() & 3 ); } - for( int i = 0; i < shape->lineDraws.Num(); i++ ) + // RB begin + if( !swf_skipLineDraws.GetBool() ) { - const idSWFShapeDrawLine& line = shape->lineDraws[i]; - swfColorXform_t color; - color.mul = line.style.startColor.ToVec4(); - color = color.Multiply( renderState.cxf ); - if( swf_forceAlpha.GetFloat() > 0.0f ) + for( int i = 0; i < shape->lineDraws.Num(); i++ ) { - color.mul.w = swf_forceAlpha.GetFloat(); - color.add.w = 0.0f; - } - if( ( color.mul.w + color.add.w ) <= ALPHA_EPSILON ) - { - continue; - } - uint32 packedColorM = LittleLong( PackColor( color.mul ) ); - uint32 packedColorA = LittleLong( PackColor( ( color.add * 0.5f ) + idVec4( 0.5f ) ) ); // Compress from -1..1 to 0..1 - - gui->SetGLState( GLStateForRenderState( renderState ) | GLS_POLYMODE_LINE ); - - idDrawVert* verts = gui->AllocTris( line.startVerts.Num(), line.indices.Ptr(), line.indices.Num(), white, renderState.stereoDepth ); - if( verts == NULL ) - { - continue; - } - - for( int j = 0; j < line.startVerts.Num(); j++ ) - { - const idVec2& xy = line.startVerts[j]; + const idSWFShapeDrawLine& line = shape->lineDraws[i]; + swfColorXform_t color; + color.mul = line.style.startColor.ToVec4(); + color = color.Multiply( renderState.cxf ); + if( swf_forceAlpha.GetFloat() > 0.0f ) + { + color.mul.w = swf_forceAlpha.GetFloat(); + color.add.w = 0.0f; + } + if( ( color.mul.w + color.add.w ) <= ALPHA_EPSILON ) + { + continue; + } + uint32 packedColorM = LittleLong( PackColor( color.mul ) ); + uint32 packedColorA = LittleLong( PackColor( ( color.add * 0.5f ) + idVec4( 0.5f ) ) ); // Compress from -1..1 to 0..1 - ALIGNTYPE16 idDrawVert tempVert; + gui->SetGLState( GLStateForRenderState( renderState ) | GLS_POLYMODE_LINE ); - tempVert.Clear(); - tempVert.xyz.ToVec2() = renderState.matrix.Transform( xy ).Scale( scaleToVirtual ); - tempVert.xyz.z = 0.0f; - tempVert.SetTexCoord( 0.0f, 0.0f ); - tempVert.SetNativeOrderColor( packedColorM ); - tempVert.SetNativeOrderColor2( packedColorA ); + idDrawVert* verts = gui->AllocTris( line.startVerts.Num(), line.indices.Ptr(), line.indices.Num(), white, renderState.stereoDepth ); + if( verts == NULL ) + { + continue; + } - WriteDrawVerts16( & verts[j], & tempVert, 1 ); + for( int j = 0; j < line.startVerts.Num(); j++ ) + { + const idVec2& xy = line.startVerts[j]; + + ALIGNTYPE16 idDrawVert tempVert; + + tempVert.Clear(); + tempVert.xyz.ToVec2() = renderState.matrix.Transform( xy ).Scale( scaleToVirtual ); + tempVert.xyz.z = 0.0f; + tempVert.SetTexCoord( 0.0f, 0.0f ); + tempVert.SetNativeOrderColor( packedColorM ); + tempVert.SetNativeOrderColor2( packedColorA ); + + WriteDrawVerts16( & verts[j], & tempVert, 1 ); + } } } + // RB end } /* From d4a53e6b49ba2797cc2a5b23731c93469223d4e2 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 24 May 2013 12:10:52 +0200 Subject: [PATCH 07/43] Fixed crash --- neo/swf/SWF_Load.cpp | 4 ++-- neo/swf/SWF_Sprites.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 56b5ee89..8154fc30 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -585,8 +585,6 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\n" ); file->WriteFloatString( "\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); - mainsprite->WriteXML( file, "\t" ); - file->WriteFloatString( "\t\n" ); for( int i = 0; i < dictionary.Num(); i++ ) { @@ -969,6 +967,8 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\n" ); + mainsprite->WriteXML( file, "\t" ); + file->WriteFloatString( "\n" ); } diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index d5e8a749..f1693c6f 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -317,7 +317,7 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID //f->WriteFloatString( "\t\t", frameLabels.Num() ); for( int i = 0; i < frameLabels.Num(); i++ ) { - f->WriteFloatString( "%s\t\n", indentPrefix, frameLabels[i].frameNum, frameLabels[i].frameLabel ); + f->WriteFloatString( "%s\t\n", indentPrefix, frameLabels[i].frameNum, frameLabels[i].frameLabel.c_str() ); } @@ -448,7 +448,7 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream // FIXME: clip actions } - file->WriteFloatString( "%s\t\t\n", indentPrefix, flags, depth ); + file->WriteFloatString( "%s\t\t\n", indentPrefix ); } // RB end From 45025883389db0d8bd814bf66e7953f469d4d795 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sat, 10 Aug 2013 13:51:49 +0200 Subject: [PATCH 08/43] Improved XSWF output --- neo/swf/SWF_Sprites.cpp | 141 +++++++++++++++++++++++++++++++++++----- neo/swf/SWF_Sprites.h | 3 + 2 files changed, 126 insertions(+), 18 deletions(-) diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index f1693c6f..f622bcbd 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -327,17 +327,14 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID { idSWFSprite::swfSpriteCommand_t& command = commands[i]; - base64.Encode( command.stream.Ptr(), command.stream.Length() ); + //base64.Encode( command.stream.Ptr(), command.stream.Length() ); //base64.Decode( src ); //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), src.Length(), src.c_str() ); - //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), base64.c_str() ); - f->WriteFloatString( "%s\t\n", indentPrefix, idSWF::GetTagName( command.tag ), command.stream.Length(), base64.c_str() ); - f->WriteFloatString( "%s\t\t%s\n", indentPrefix, base64.c_str() ); - - //f->WriteFloatString( "%s\t%s\n", indentPrefix, idSWF::GetTagName( commands[i].tag ), commands[i].stream.Length(), commands[i].stream.Ptr() ); + //f->WriteFloatString( "%s\t\n", indentPrefix, idSWF::GetTagName( command.tag ), command.stream.Length(), base64.c_str() ); + //f->WriteFloatString( "%s\t\t%s\n", indentPrefix, base64.c_str() ); command.stream.Rewind(); switch( command.tag ) @@ -348,17 +345,17 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID #define HANDLE_SWF_TAG( x ) case Tag_##x: WriteXML_##x( f, command.stream, indentPrefix ); break; HANDLE_SWF_TAG( PlaceObject2 ); - //HANDLE_SWF_TAG( PlaceObject3 ); - //HANDLE_SWF_TAG( RemoveObject2 ); + HANDLE_SWF_TAG( PlaceObject3 ); + HANDLE_SWF_TAG( RemoveObject2 ); //HANDLE_SWF_TAG( StartSound ); - //HANDLE_SWF_TAG( DoAction ); + HANDLE_SWF_TAG( DoAction ); #undef HANDLE_SWF_TAG default: break; //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); } - f->WriteFloatString( "%s\t\n", indentPrefix ); + //f->WriteFloatString( "%s\t\n", indentPrefix ); } for( int i = 0; i < doInitActions.Num(); i++ ) @@ -381,7 +378,7 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream uint64 flags = bitstream.ReadU8(); int depth = bitstream.ReadU16(); - file->WriteFloatString( "%s\t\tWriteFloatString( "%s\tWriteFloatString( "%s\t\t\t%f %f %f %f %f %f\n", indentPrefix, m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + file->WriteFloatString( "%s\t\t%f %f %f %f %f %f\n", indentPrefix, m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); } if( ( flags & PlaceFlagHasColorTransform ) != 0 ) @@ -407,24 +404,24 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream bitstream.ReadColorXFormRGBA( cxf ); idVec4 color = cxf.mul; - file->WriteFloatString( "%s\t\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); color = cxf.add; - file->WriteFloatString( "%s\t\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); } if( ( flags & PlaceFlagHasRatio ) != 0 ) { float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); - file->WriteFloatString( "%s\t\t\t%f\n", indentPrefix, ratio ); + file->WriteFloatString( "%s\t\t%f\n", indentPrefix, ratio ); } if( ( flags & PlaceFlagHasName ) != 0 ) { idStr name = bitstream.ReadString(); - file->WriteFloatString( "%s\t\t\t%s\n", indentPrefix, name.c_str() ); + file->WriteFloatString( "%s\t\t%s\n", indentPrefix, name.c_str() ); /*if( display->spriteInstance ) { @@ -440,7 +437,7 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream if( ( flags & PlaceFlagHasClipDepth ) != 0 ) { uint16 clipDepth = bitstream.ReadU16(); - file->WriteFloatString( "%s\t\t\t%i\n", indentPrefix, clipDepth ); + file->WriteFloatString( "%s\t\t%i\n", indentPrefix, clipDepth ); } if( ( flags & PlaceFlagHasClipActions ) != 0 ) @@ -448,7 +445,115 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream // FIXME: clip actions } - file->WriteFloatString( "%s\t\t\n", indentPrefix ); + file->WriteFloatString( "%s\t\n", indentPrefix ); +} + + +void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +{ + uint64 flags1 = bitstream.ReadU8(); + uint64 flags2 = bitstream.ReadU8(); + int depth = bitstream.ReadU16(); + + file->WriteFloatString( "%s\tWriteFloatString( " characterID=\"%i\"", characterID ); + } + + file->WriteFloatString( ">\n" ); + + if( ( flags1 & PlaceFlagHasMatrix ) != 0 ) + { + swfMatrix_t m; + + bitstream.ReadMatrix( m ); + + file->WriteFloatString( "%s\t\t%f %f %f %f %f %f\n", indentPrefix, m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + } + + if( ( flags1 & PlaceFlagHasColorTransform ) != 0 ) + { + swfColorXform_t cxf; + bitstream.ReadColorXFormRGBA( cxf ); + + idVec4 color = cxf.mul; + file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + + color = cxf.add; + file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + } + + if( ( flags1 & PlaceFlagHasRatio ) != 0 ) + { + float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); + + file->WriteFloatString( "%s\t\t%f\n", indentPrefix, ratio ); + } + + if( ( flags1 & PlaceFlagHasName ) != 0 ) + { + idStr name = bitstream.ReadString(); + + file->WriteFloatString( "%s\t\t%s\n", indentPrefix, name.c_str() ); + + /*if( display->spriteInstance ) + { + display->spriteInstance->name = name; + scriptObject->Set( name, display->spriteInstance->GetScriptObject() ); + } + else if( display->textInstance ) + { + scriptObject->Set( name, display->textInstance->GetScriptObject() ); + }*/ + } + + if( ( flags1 & PlaceFlagHasClipDepth ) != 0 ) + { + uint16 clipDepth = bitstream.ReadU16(); + file->WriteFloatString( "%s\t\t%i\n", indentPrefix, clipDepth ); + } + + if( ( flags2 & PlaceFlagHasFilterList ) != 0 ) + { + // we don't support filters and because the filter list is variable length we + // can't support anything after the filter list either (blend modes and clip actions) + //idLib::Warning( "PlaceObject3: has filters" ); + file->WriteFloatString( "%s\t\t\n", indentPrefix ); + return; + } + + if( ( flags2 & PlaceFlagHasBlendMode ) != 0 ) + { + uint8 blendMode = bitstream.ReadU8(); + file->WriteFloatString( "%s\t\t%i\n", indentPrefix, blendMode ); + } + + if( ( flags1 & PlaceFlagHasClipActions ) != 0 ) + { + // FIXME: clip actions + } + + file->WriteFloatString( "%s\t\n", indentPrefix ); +} + +void idSWFSprite::WriteXML_RemoveObject2( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +{ + int depth = bitstream.ReadU16(); + + file->WriteFloatString( "%s\t", indentPrefix, depth ); +} + +void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +{ + idBase64 base64; + + base64.Encode( bitstream.Ptr(), bitstream.Length() ); + + file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); } // RB end diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index ed2704c5..d40d11fc 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -50,6 +50,9 @@ public: // RB begin void WriteXML( idFile* f, const char* indentPrefix = "", int characterID = -1 ); void WriteXML_PlaceObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); + void WriteXML_PlaceObject3( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); + void WriteXML_RemoveObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); + void WriteXML_DoAction( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); // RB end class idSWF* GetSWF() From 9c58fdf6f9446540f42d973361ec4a8e1cbacea5 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sat, 10 Aug 2013 18:51:22 +0200 Subject: [PATCH 09/43] Added swf_exportAtlas --- neo/swf/SWF_Main.cpp | 106 +++++++++++++++++++++++++++++++++++++++- neo/swf/SWF_Sprites.cpp | 2 + 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 29c84671..d38694f8 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -29,12 +29,14 @@ If you have questions concerning this license or the applicable additional terms #pragma hdrstop #include "precompiled.h" #include "../renderer/Image.h" +#include "../renderer/DXT//DXTCodec.h" #pragma warning(disable: 4355) // 'this' : used in base member initializer list idCVar swf_loadBinary( "swf_loadBinary", "1", CVAR_INTEGER, "used to set whether to load binary swf from generated" ); // RB begin idCVar swf_exportXML( "swf_exportXML", "1", CVAR_INTEGER, "" ); +idCVar swf_exportAtlas( "swf_exportAtlas", "1", CVAR_INTEGER, "" ); // RB end int idSWF::mouseX = -1; @@ -157,12 +159,114 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) WriteXML( xmlFileName ); } - // RB end idStr atlasFileName = binaryFileName; atlasFileName.SetFileExtension( ".tga" ); atlasMaterial = declManager->FindMaterial( atlasFileName ); + if( swf_exportAtlas.GetBool() ) + { + idStrStatic< MAX_OSPATH > generatedName = atlasFileName; + generatedName.StripFileExtension(); + idImage::GetGeneratedName( generatedName, TD_DEFAULT, CF_2D ); + + idBinaryImage im( generatedName ); + ID_TIME_T binaryFileTime = im.LoadFromGeneratedFile( FILE_NOT_FOUND_TIMESTAMP ); + + if( binaryFileTime != FILE_NOT_FOUND_TIMESTAMP ) + { + const bimageFile_t& imgHeader = im.GetFileHeader(); + const bimageImage_t& img = im.GetImageHeader( 0 ); + + const byte* data = im.GetImageData( 0 ); + + //( img.level, 0, 0, img.destZ, img.width, img.height, data ); + + idTempArray rgba( img.width * img.height * 4 ); + memset( rgba.Ptr(), 255, rgba.Size() ); + + if( imgHeader.format == FMT_DXT1 ) + { + idDxtDecoder dxt; + dxt.DecompressImageDXT1( data, rgba.Ptr(), img.width, img.height ); + } + else if( imgHeader.format == FMT_DXT5 ) + { + idDxtDecoder dxt; + + if( imgHeader.colorFormat == CFM_NORMAL_DXT5 ) + { + dxt.DecompressNormalMapDXT5( data, rgba.Ptr(), img.width, img.height ); + } + else if( imgHeader.colorFormat == CFM_YCOCG_DXT5 ) + { + dxt.DecompressYCoCgDXT5( data, rgba.Ptr(), img.width, img.height ); + } + else + { + + dxt.DecompressImageDXT5( data, rgba.Ptr(), img.width, img.height ); + } + } + else if( imgHeader.format == FMT_LUM8 || imgHeader.format == FMT_INT8 ) + { + // LUM8 and INT8 just read the red channel + byte* pic = rgba.Ptr(); + for( int i = 0; i < img.dataSize; i++ ) + { + pic[ i * 4 ] = data[ i ]; + } + } + else if( imgHeader.format == FMT_ALPHA ) + { + // ALPHA reads the alpha channel + byte* pic = rgba.Ptr(); + for( int i = 0; i < img.dataSize; i++ ) + { + pic[ i * 4 + 3 ] = data[ i ]; + } + } + else if( imgHeader.format == FMT_L8A8 ) + { + // L8A8 reads the alpha and red channels + byte* pic = rgba.Ptr(); + for( int i = 0; i < img.dataSize / 2; i++ ) + { + pic[ i * 4 + 0 ] = data[ i * 2 + 0 ]; + pic[ i * 4 + 3 ] = data[ i * 2 + 1 ]; + } + } + else if( imgHeader.format == FMT_RGB565 ) + { + // FIXME + /* + byte* pic = rgba.Ptr(); + for( int i = 0; i < img.dataSize / 2; i++ ) + { + unsigned short color = ( ( pic[ i * 4 + 0 ] >> 3 ) << 11 ) | ( ( pic[ i * 4 + 1 ] >> 2 ) << 5 ) | ( pic[ i * 4 + 2 ] >> 3 ); + img.data[ i * 2 + 0 ] = ( color >> 8 ) & 0xFF; + img.data[ i * 2 + 1 ] = color & 0xFF; + } + */ + } + else + { + byte* pic = rgba.Ptr(); + for( int i = 0; i < img.dataSize; i++ ) + { + pic[ i ] = data[ i ]; + } + } + + idStr atlasFileNameExport = atlasFileName; + atlasFileNameExport.SetFileExtension( ".png" ); + + R_WritePNG( atlasFileNameExport, rgba.Ptr(), 4, img.width, img.height, true, "fs_basepath" ); + } + + } + // RB end + globals = idSWFScriptObject::Alloc(); globals->Set( "_global", globals ); diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index f622bcbd..86655efa 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -321,6 +321,7 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID } +#if 1 idBase64 base64; for( int i = 0; i < commands.Num(); i++ ) @@ -366,6 +367,7 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID //f->WriteFloatString( "%s\t%s\n", indentPrefix, doInitActions[i].Length(), doInitActions[i].Ptr() ); } +#endif f->WriteFloatString( "%s\n", indentPrefix ); } From b952f3c5949bf40f11beaff2174bd6f492749655 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Wed, 30 Oct 2013 11:44:24 +0100 Subject: [PATCH 10/43] SWF export --- neo/swf/SWF.h | 7 +++++ neo/swf/SWF_Load.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index 4832ba6d..ef068621 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -387,15 +387,22 @@ private: friend class idSWFSprite; friend class idSWFSpriteInstance; + // RB begin bool LoadSWF( const char* fullpath ); + void WriteSWF( const char* filename ); + bool LoadBinary( const char* bfilename, ID_TIME_T sourceTime ); void WriteBinary( const char* bfilename ); +<<<<<<< HEAD <<<<<<< HEAD void FileAttributes( idSWFBitStream& bitstream ); void Metadata( idSWFBitStream& bitstream ); void SetBackgroundColor( idSWFBitStream& bitstream ); ======= // RB begin +======= + +>>>>>>> 534d415... SWF export void LoadXML( const char* filename ); void WriteXML( const char* filename ); // RB end diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 8154fc30..7f9cdd60 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -139,6 +139,68 @@ bool idSWF::LoadSWF( const char* fullpath ) return true; } +/* +=================== +idSWF::WriteSWF + +RB: bring .bswf back to .swf +=================== +*/ +void idSWF::WriteSWF( const char* fullpath ) +{ + idFileLocal file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); + if( file == NULL ) + { + return; + } + + swfHeader_t header; + header.W = 'W'; + header.S = 'S'; + header.version = 9; + header.compression == 'F'; + + file->Write( &header, sizeof( header ) ); + + + // TODO + /* + swfRect_t frameSize; + bitstream.ReadRect( frameSize ); + + if( !frameSize.tl.Compare( vec2_zero ) ) + { + idLib::Warning( "Invalid frameSize top left" ); + Mem_Free( fileData ); + return false; + } + + frameWidth = frameSize.br.x; + frameHeight = frameSize.br.y; + frameRate = bitstream.ReadU16(); + + // parse everything + mainsprite->Load( bitstream, true ); + + // now that all images have been loaded, write out the combined image + idStr atlasFileName = "generated/"; + atlasFileName += fullpath; + atlasFileName.SetFileExtension( ".tga" ); + + WriteSwfImageAtlas( atlasFileName ); + + Mem_Free( fileData ); + */ + + // add Tag_End + + // go back and write filesize into header + uint32 fileSize = file->Length(); + + file->Seek( offsetof( swfHeader_t, fileLength ), FS_SEEK_SET ); + file->WriteBig( fileSize ); +} + /* =================== idSWF::LoadBinary From 54b27530251f3938525ef18cbc60fa2e5d62ad63 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Wed, 6 Nov 2013 21:46:35 +0100 Subject: [PATCH 11/43] SWF export empty .swf --- neo/swf/SWF.h | 3 ++ neo/swf/SWF_File.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++ neo/swf/SWF_File.h | 92 +++++++++++++++++++++++++++++++++ neo/swf/SWF_Load.cpp | 38 ++++++-------- neo/swf/SWF_Main.cpp | 44 ++++++++++++++++ neo/swf/SWF_Types.h | 10 ++++ 6 files changed, 283 insertions(+), 22 deletions(-) create mode 100644 neo/swf/SWF_File.cpp create mode 100644 neo/swf/SWF_File.h diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index ef068621..dea7ff52 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -32,6 +32,9 @@ If you have questions concerning this license or the applicable additional terms #include "SWF_Enums.h" #include "SWF_Types.h" #include "SWF_Bitstream.h" +// RB begin +#include "SWF_File.h" +// RB end #include "SWF_ScriptVar.h" #include "SWF_Sprites.h" #include "SWF_ScriptObject.h" diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp new file mode 100644 index 00000000..e7a61360 --- /dev/null +++ b/neo/swf/SWF_File.cpp @@ -0,0 +1,118 @@ +/* +=========================================================================== + +Doom 3 BFG Edition GPL Source Code +Copyright (C) 2013 Robert Beckebans + +This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). + +Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Doom 3 BFG Edition Source Code. If not, see . + +In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ +#pragma hdrstop +#include "precompiled.h" + + + + + + +idFile_SWF::~idFile_SWF() +{ + if( file != NULL ) + { + delete file; + file = NULL; + } +} + + + +//uint idFile_SWF::GetNumBits( int64 value, bool isSigned ) +//{ +// //return idMath::BitCount( +//} + +uint idFile_SWF::GetNumBitsInt( const int value ) +{ + return idMath::BitCount( value ); +} + +int idFile_SWF::Write( const void* buffer, int len ) +{ + return file->Write( buffer, len ); +} + +void idFile_SWF::WriteByte( byte bits ) +{ + file->WriteUnsignedChar( bits ); +} + +void idFile_SWF::WriteUBits( int value, int numBits ) +{ + for( int bit = 0; bit < numBits; bit++ ) + { + int nb = ( int )( ( value >> ( numBits - 1 - bit ) ) & 1 ); + + NBits += nb * ( 1 << ( 7 - bitPos ) ); + bitPos++; + + if( bitPos == 8 ) + { + WriteByte( NBits ); + + bitPos = 0; + NBits = 0; + } + } +} + +void idFile_SWF::WriteSBits( int value, int numBits ) +{ + WriteUBits( value, numBits ); +} + +void idFile_SWF::WriteU16( uint16 value ) +{ + ByteAlign(); + + WriteByte( value & 0xFF ); + WriteByte( value >> 8 ); +} + +void idFile_SWF::WriteRect( const swfRect_t& rect ) +{ + uint64 regCurrentBit = 0; + uint64 regCurrentByte = 0; + + int nBits = rect.BitCount(); + + int tl_x = PIXEL2SWFTWIP( rect.tl.x ); + int br_x = PIXEL2SWFTWIP( rect.br.x ); + int tl_y = PIXEL2SWFTWIP( rect.tl.y ); + int br_y = PIXEL2SWFTWIP( rect.br.y ); + + WriteUBits( nBits, 5 ); + WriteSBits( tl_x, nBits ); + WriteSBits( br_x, nBits ); + WriteSBits( tl_y, nBits ); + WriteSBits( br_y, nBits ); +} + + diff --git a/neo/swf/SWF_File.h b/neo/swf/SWF_File.h new file mode 100644 index 00000000..2191810e --- /dev/null +++ b/neo/swf/SWF_File.h @@ -0,0 +1,92 @@ +/* +=========================================================================== + +Doom 3 BFG Edition GPL Source Code +Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans + +This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). + +Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Doom 3 BFG Edition Source Code. If not, see . + +In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. + +If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. + +=========================================================================== +*/ +#ifndef __SWF_FILE_H__ +#define __SWF_FILE_H__ + +class idFile_SWF// : public idFile +{ +public: + // Constructor that accepts and stores the file pointer. + idFile_SWF( idFile* _file ) : file( _file ) + { + bitPos = 0; + NBits = 0; + } + + // Destructor that will destroy (close) the file when this wrapper class goes out of scope. + ~idFile_SWF(); + + // Cast to a file pointer. + operator idFile* () const + { + return file; + } + + // Member access operator for treating the wrapper as if it were the file, itself. + idFile* operator -> () const + { + return file; + } + + void ByteAlign() + { + if( bitPos > 0 ) + { + WriteByte( NBits ); + } + } + + + uint GetNumBits( int64 value, bool isSigned ); + uint GetNumBitsInt( const int value ); + uint GetNumBitsFloat( const int value ); + + virtual int Write( const void* buffer, int len ); + + void WriteUBits( int value, int numBits ); + void WriteSBits( int value, int numBits ); + + void WriteU16( uint16 value ); + + void WriteRect( const swfRect_t& rect ); + +private: + + void WriteByte( byte bits ); + + idFile* file; // The managed file pointer. + + byte bitPos; + byte NBits; + +}; + + + +#endif // !__SWF_FILE_H__ diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 7f9cdd60..083e33bb 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -146,9 +146,9 @@ idSWF::WriteSWF RB: bring .bswf back to .swf =================== */ -void idSWF::WriteSWF( const char* fullpath ) +void idSWF::WriteSWF( const char* filename ) { - idFileLocal file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); + idFile_SWF file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); if( file == NULL ) { return; @@ -158,41 +158,35 @@ void idSWF::WriteSWF( const char* fullpath ) header.W = 'W'; header.S = 'S'; header.version = 9; - header.compression == 'F'; + header.compression = 'F'; - file->Write( &header, sizeof( header ) ); + file.Write( &header, sizeof( header ) ); // TODO - /* + swfRect_t frameSize; - bitstream.ReadRect( frameSize ); + frameSize.br.x = frameWidth; + frameSize.br.y = frameHeight; - if( !frameSize.tl.Compare( vec2_zero ) ) - { - idLib::Warning( "Invalid frameSize top left" ); - Mem_Free( fileData ); - return false; - } + file.WriteRect( frameSize ); - frameWidth = frameSize.br.x; - frameHeight = frameSize.br.y; - frameRate = bitstream.ReadU16(); + file.WriteU16( frameRate ); // parse everything - mainsprite->Load( bitstream, true ); + //mainsprite->Load( bitstream, true ); // now that all images have been loaded, write out the combined image - idStr atlasFileName = "generated/"; - atlasFileName += fullpath; - atlasFileName.SetFileExtension( ".tga" ); + //idStr atlasFileName = "generated/"; + //atlasFileName += fullpath; + //atlasFileName.SetFileExtension( ".tga" ); - WriteSwfImageAtlas( atlasFileName ); + //WriteSwfImageAtlas( atlasFileName ); - Mem_Free( fileData ); - */ + //Mem_Free( fileData ); // add Tag_End + file.WriteU16( Tag_End ); // go back and write filesize into header uint32 fileSize = file->Length(); diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index d38694f8..29db3244 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -35,6 +35,7 @@ If you have questions concerning this license or the applicable additional terms idCVar swf_loadBinary( "swf_loadBinary", "1", CVAR_INTEGER, "used to set whether to load binary swf from generated" ); // RB begin +idCVar swf_exportSWF( "swf_exportSWF", "1", CVAR_INTEGER, "" ); idCVar swf_exportXML( "swf_exportXML", "1", CVAR_INTEGER, "" ); idCVar swf_exportAtlas( "swf_exportAtlas", "1", CVAR_INTEGER, "" ); // RB end @@ -45,6 +46,40 @@ bool idSWF::isMouseInClientArea = false; extern idCVar in_useJoystick; +// RB begin +int swfRect_t::BitCount() const +{ + int c = 0; + + int num = idMath::BitCount( ( int ) tl.x ); + if( num > c ) + { + c = num; + } + + num = idMath::BitCount( ( int ) tl.y ); + if( num > c ) + { + c = num; + } + + num = idMath::BitCount( ( int ) br.x ); + if( num > c ) + { + c = num; + } + + num = idMath::BitCount( ( int ) br.y ); + if( num > c ) + { + c = num; + } + + return c; +} + +// RB end + /* =================== idSWF::idSWF @@ -160,6 +195,15 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) WriteXML( xmlFileName ); } + if( swf_exportSWF.GetBool() ) + { + idStr swfFileName = "generated/"; + swfFileName += filename; + swfFileName.SetFileExtension( ".swf" ); + + WriteSWF( swfFileName ); + } + idStr atlasFileName = binaryFileName; atlasFileName.SetFileExtension( ".tga" ); atlasMaterial = declManager->FindMaterial( atlasFileName ); diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 7158d7cc..1df85caa 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2013 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -41,6 +42,11 @@ ID_INLINE float SWFFIXED8( int fixed ) return fixed * ( 1.0f / 256.0f ); } +ID_INLINE int PIXEL2SWFTWIP( float pixel ) +{ + return ( int )( pixel * 20 ); +} + struct swfHeader_t { byte compression; @@ -54,6 +60,10 @@ struct swfRect_t swfRect_t(); idVec2 tl; idVec2 br; + + // RB begin + int BitCount() const; + // RB end }; struct swfMatrix_t { From edd5ccb9b9c5438da481cc5dcda69d2d1fe69563 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 8 Nov 2013 01:06:01 +0100 Subject: [PATCH 12/43] Prepaired SWF images export --- neo/swf/SWF_Main.cpp | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 29db3244..ae945e47 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -195,20 +195,15 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) WriteXML( xmlFileName ); } - if( swf_exportSWF.GetBool() ) - { - idStr swfFileName = "generated/"; - swfFileName += filename; - swfFileName.SetFileExtension( ".swf" ); - - WriteSWF( swfFileName ); - } - idStr atlasFileName = binaryFileName; atlasFileName.SetFileExtension( ".tga" ); atlasMaterial = declManager->FindMaterial( atlasFileName ); - if( swf_exportAtlas.GetBool() ) + byte* atlasExportImageRGBA = NULL; + int atlasExportImageWidth = 0; + int atlasExportImageHeight = 0; + + if( swf_exportAtlas.GetBool() || swf_exportSWF.GetBool() ) { idStrStatic< MAX_OSPATH > generatedName = atlasFileName; generatedName.StripFileExtension(); @@ -306,8 +301,30 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) atlasFileNameExport.SetFileExtension( ".png" ); R_WritePNG( atlasFileNameExport, rgba.Ptr(), 4, img.width, img.height, true, "fs_basepath" ); + + if( swf_exportSWF.GetBool() ) + { + atlasExportImageWidth = img.width; + atlasExportImageHeight = img.height; + atlasExportImageRGBA = ( byte* ) Mem_Alloc( rgba.Size(), TAG_TEMP ); + memcpy( atlasExportImageRGBA, rgba.Ptr(), rgba.Size() ); + } } + } + + if( swf_exportSWF.GetBool() ) + { + idStr swfFileName = "generated/"; + swfFileName += filename; + swfFileName.SetFileExtension( ".swf" ); + WriteSWF( swfFileName ); + } + + if( atlasExportImageRGBA != NULL ) + { + Mem_Free( atlasExportImageRGBA ); + atlasExportImageRGBA = NULL; } // RB end From f8f1264eb405a0f8ad7007beba358fe5914c9d8d Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 10 Nov 2013 00:34:51 +0100 Subject: [PATCH 13/43] More work on SWF images export --- neo/swf/SWF.h | 5 +- neo/swf/SWF_File.cpp | 37 +++++++++++++-- neo/swf/SWF_File.h | 6 +++ neo/swf/SWF_Load.cpp | 103 ++++++++++++++++++++++++++++++++++++++++-- neo/swf/SWF_Main.cpp | 2 +- neo/swf/SWF_Sprites.h | 5 ++ neo/swf/SWF_Zlib.cpp | 37 +++++++++++++++ 7 files changed, 186 insertions(+), 9 deletions(-) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index dea7ff52..b953f836 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -392,7 +392,7 @@ private: // RB begin bool LoadSWF( const char* fullpath ); - void WriteSWF( const char* filename ); + void WriteSWF( const char* filename, const byte* atlasImageRGBA, int atlasImageWidth, int atlasImageHeight ); bool LoadBinary( const char* bfilename, ID_TIME_T sourceTime ); void WriteBinary( const char* bfilename ); @@ -508,6 +508,9 @@ private: // SWF_Zlib.cpp //---------------------------------- bool Inflate( const byte* input, int inputSize, byte* output, int outputSize ); + // RB begin + bool Deflate( const byte* input, int inputSize, byte* output, int& outputSize ); + // RB end public: //---------------------------------- diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index e7a61360..915f8eba 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -88,6 +88,13 @@ void idFile_SWF::WriteSBits( int value, int numBits ) WriteUBits( value, numBits ); } +void idFile_SWF::WriteU8( uint8 value ) +{ + ByteAlign(); + + WriteByte( value ); +} + void idFile_SWF::WriteU16( uint16 value ) { ByteAlign(); @@ -96,11 +103,18 @@ void idFile_SWF::WriteU16( uint16 value ) WriteByte( value >> 8 ); } +void idFile_SWF::WriteU32( uint32 value ) +{ + ByteAlign(); + + WriteByte( value & 0xFF ); + WriteByte( ( value >> 8 ) & 0xFF ); + WriteByte( ( value >> 16 ) & 0xFF ); + WriteByte( ( value >> 24 ) & 0xFF ); +} + void idFile_SWF::WriteRect( const swfRect_t& rect ) { - uint64 regCurrentBit = 0; - uint64 regCurrentByte = 0; - int nBits = rect.BitCount(); int tl_x = PIXEL2SWFTWIP( rect.tl.x ); @@ -115,4 +129,21 @@ void idFile_SWF::WriteRect( const swfRect_t& rect ) WriteSBits( br_y, nBits ); } +void idFile_SWF::WriteTagHeader( swfTag_t tag, int32 tagLength ) +{ + uint16 tagIDLength = ( ( int ) tag << 6 ); + + if( tagLength < 0x3F ) + { + tagIDLength += tagLength; + WriteU16( tagIDLength ); + } + else + { + tagIDLength += 0x3F; + WriteU16( tagIDLength ); + WriteU32( tagLength ); + } +} + diff --git a/neo/swf/SWF_File.h b/neo/swf/SWF_File.h index 2191810e..0492c99f 100644 --- a/neo/swf/SWF_File.h +++ b/neo/swf/SWF_File.h @@ -58,7 +58,9 @@ public: { if( bitPos > 0 ) { + bitPos = 0; WriteByte( NBits ); + NBits = 0; } } @@ -72,10 +74,14 @@ public: void WriteUBits( int value, int numBits ); void WriteSBits( int value, int numBits ); + void WriteU8( uint8 value ); void WriteU16( uint16 value ); + void WriteU32( uint32 value ); void WriteRect( const swfRect_t& rect ); + void WriteTagHeader( swfTag_t tag, int32 tagLength ); + private: void WriteByte( byte bits ); diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 083e33bb..6dd0c3ca 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -146,7 +146,7 @@ idSWF::WriteSWF RB: bring .bswf back to .swf =================== */ -void idSWF::WriteSWF( const char* filename ) +void idSWF::WriteSWF( const char* filename, const byte* atlasImageRGBA, int atlasImageWidth, int atlasImageHeight ) { idFile_SWF file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); if( file == NULL ) @@ -154,6 +154,7 @@ void idSWF::WriteSWF( const char* filename ) return; } + /* swfHeader_t header; header.W = 'W'; header.S = 'S'; @@ -161,6 +162,15 @@ void idSWF::WriteSWF( const char* filename ) header.compression = 'F'; file.Write( &header, sizeof( header ) ); + */ + + + file.WriteU8( 'F' ); + file.WriteU8( 'W' ); + file.WriteU8( 'S' ); + file.WriteU8( 9 ); + file.WriteU32( 0 ); + // TODO @@ -169,10 +179,94 @@ void idSWF::WriteSWF( const char* filename ) frameSize.br.x = frameWidth; frameSize.br.y = frameHeight; + int fileSize1 = file->Length(); + file.WriteRect( frameSize ); + int fileSize2 = file->Length(); + file.WriteU16( frameRate ); + file.WriteU16( mainsprite->GetFrameCount() ); + + // write FileAttributes tag required for Flash Version >= 8 + file.WriteTagHeader( Tag_FileAttributes, 4 ); + + file.WriteUBits( 0, 1 ); // Reserved, must be 0 + file.WriteUBits( 0, 1 ); // UseDirectBlit + file.WriteUBits( 0, 1 ); // UseGPU + file.WriteUBits( 0, 1 ); // HasMetadata + + file.WriteUBits( 0, 1 ); // ActionScript3 + file.WriteUBits( 0, 1 ); // Reserved, must be 0 + file.WriteUBits( 0, 1 ); // UseNetwork + + file.WriteUBits( 0, 24 ); // Reserved, must be 0 + + file.ByteAlign(); + +#if 1 + for( int i = 0; i < dictionary.Num(); i++ ) + { + const idSWFDictionaryEntry& entry = dictionary[i]; + + //file->WriteFloatString( "\t\n", idSWF::GetDictTypeName( dictionary[i].type ) ); + switch( dictionary[i].type ) + { + case SWF_DICT_IMAGE: + { + int width = atlasImageWidth; + int height = atlasImageHeight; + + // RB: add some extra space for zlib + idTempArray bitmapData( width * height * 4 * 1.02 + 12 ); + uint32 colorDataSize = width * height * 4; //bitstream.Length() - bitstream.Tell(); + int compressedDataSize = bitmapData.Size(); + if( !Deflate( atlasImageRGBA, colorDataSize, ( byte* )bitmapData.Ptr(), compressedDataSize ) ) + { + idLib::Warning( "DefineBitsLossless: Failed to deflate bitmap data" ); + //return; + } + + int tagLength = ( 2 + 1 + 2 + 2 ) + compressedDataSize; + + file.WriteTagHeader( Tag_DefineBitsLossless, tagLength ); + + file.WriteU16( i ); // characterID + file.WriteU8( 5 ); // format + file.WriteU16( entry.imageSize[0] ); // width + file.WriteU16( entry.imageSize[1] ); // height + + + + + + file->Write( bitmapData.Ptr(), compressedDataSize ); + + //file->WriteFloatString( "\t\tWriteFloatString( "%s\"", dictionary[i].material->GetName() ); + } + else + { + file->WriteFloatString( ".\"" ); + } + + file->WriteFloatString( " width=\"%i\" height=\"%i\" atlasOffsetX=\"%i\" atlasOffsetY=\"%i\">\n", + entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); + + file->WriteFloatString( "\t\t\t\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); + + file->WriteFloatString( "\t\t\n" ); + */ + break; + } + } + } +#endif + // parse everything //mainsprite->Load( bitstream, true ); @@ -186,13 +280,14 @@ void idSWF::WriteSWF( const char* filename ) //Mem_Free( fileData ); // add Tag_End - file.WriteU16( Tag_End ); + file.WriteTagHeader( Tag_End, 0 ); // go back and write filesize into header uint32 fileSize = file->Length(); - file->Seek( offsetof( swfHeader_t, fileLength ), FS_SEEK_SET ); - file->WriteBig( fileSize ); + uint32 headerFileLengthOfs = offsetof( swfHeader_t, fileLength ); + file->Seek( headerFileLengthOfs, FS_SEEK_SET ); + file.WriteU32( fileSize ); } /* diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index ae945e47..2bb9756f 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -318,7 +318,7 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) swfFileName += filename; swfFileName.SetFileExtension( ".swf" ); - WriteSWF( swfFileName ); + WriteSWF( swfFileName, atlasExportImageRGBA, atlasExportImageWidth, atlasExportImageHeight ); } if( atlasExportImageRGBA != NULL ) diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index d40d11fc..bde0bfc8 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -53,6 +53,11 @@ public: void WriteXML_PlaceObject3( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); void WriteXML_RemoveObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); void WriteXML_DoAction( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); + + uint16 GetFrameCount() + { + return frameCount; + } // RB end class idSWF* GetSWF() diff --git a/neo/swf/SWF_Zlib.cpp b/neo/swf/SWF_Zlib.cpp index e3ed8d6a..dcfb4dda 100644 --- a/neo/swf/SWF_Zlib.cpp +++ b/neo/swf/SWF_Zlib.cpp @@ -61,3 +61,40 @@ bool idSWF::Inflate( const byte* input, int inputSize, byte* output, int outputS return success; } + +// RB begin +bool idSWF::Deflate( const byte* input, int inputSize, byte* output, int& outputSize ) +{ + struct local_swf_alloc_t + { + static void* zalloc( void* opaque, uint32 items, uint32 size ) + { + return Mem_Alloc( items * size, TAG_SWF ); + } + static void zfree( void* opaque, void* ptr ) + { + Mem_Free( ptr ); + } + }; + z_stream stream; + memset( &stream, 0, sizeof( stream ) ); + stream.next_in = ( Bytef* )input; + stream.avail_in = inputSize; + stream.next_out = ( Bytef* )output; + stream.avail_out = outputSize; + stream.zalloc = local_swf_alloc_t::zalloc; + stream.zfree = local_swf_alloc_t::zfree; + + int err = deflateInit( &stream, Z_NO_COMPRESSION ); + if( err != Z_OK ) + return false; + + err = deflate( &stream, Z_FINISH ); + + outputSize = stream.total_out; + + deflateEnd( &stream ); + + return ( err == Z_STREAM_END ); +} +// RB end From e19e0a691175fb23ddb7b2af06dfcfc5acdd0a2c Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 10 Nov 2013 14:27:18 +0100 Subject: [PATCH 14/43] SWF image export --- neo/swf/SWF_Load.cpp | 109 ++++++++++++++++++++++--------------------- neo/swf/SWF_Zlib.cpp | 2 +- 2 files changed, 57 insertions(+), 54 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 6dd0c3ca..401bbe61 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -29,6 +29,7 @@ If you have questions concerning this license or the applicable additional terms #pragma hdrstop #include "precompiled.h" #include "../renderer/Font.h" +#include "../renderer/Image.h" #pragma warning(disable: 4355) // 'this' : used in base member initializer list @@ -139,22 +140,15 @@ bool idSWF::LoadSWF( const char* fullpath ) return true; } -/* -=================== -idSWF::WriteSWF - -RB: bring .bswf back to .swf -=================== -*/ -void idSWF::WriteSWF( const char* filename, const byte* atlasImageRGBA, int atlasImageWidth, int atlasImageHeight ) +// RB: write new .swf with only the information we care about +void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int atlasImageWidth, int atlasImageHeight ) { - idFile_SWF file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); + idFile_SWF file( fileSystem->OpenFileWrite( swfFilename, "fs_basepath" ) ); if( file == NULL ) { return; } - /* swfHeader_t header; header.W = 'W'; header.S = 'S'; @@ -162,34 +156,22 @@ void idSWF::WriteSWF( const char* filename, const byte* atlasImageRGBA, int atla header.compression = 'F'; file.Write( &header, sizeof( header ) ); - */ - - - file.WriteU8( 'F' ); - file.WriteU8( 'W' ); - file.WriteU8( 'S' ); - file.WriteU8( 9 ); - file.WriteU32( 0 ); - - - - // TODO swfRect_t frameSize; frameSize.br.x = frameWidth; frameSize.br.y = frameHeight; - int fileSize1 = file->Length(); + //int fileSize1 = file->Length(); file.WriteRect( frameSize ); - int fileSize2 = file->Length(); + //int fileSize2 = file->Length(); file.WriteU16( frameRate ); file.WriteU16( mainsprite->GetFrameCount() ); - // write FileAttributes tag required for Flash Version >= 8 + // write FileAttributes tag required for Flash version >= 8 file.WriteTagHeader( Tag_FileAttributes, 4 ); file.WriteUBits( 0, 1 ); // Reserved, must be 0 @@ -205,7 +187,6 @@ void idSWF::WriteSWF( const char* filename, const byte* atlasImageRGBA, int atla file.ByteAlign(); -#if 1 for( int i = 0; i < dictionary.Num(); i++ ) { const idSWFDictionaryEntry& entry = dictionary[i]; @@ -215,14 +196,51 @@ void idSWF::WriteSWF( const char* filename, const byte* atlasImageRGBA, int atla { case SWF_DICT_IMAGE: { - int width = atlasImageWidth; - int height = atlasImageHeight; + int width = entry.imageSize[0]; + int height = entry.imageSize[1]; + + uint32 colorDataSize = width * height * 4; + idTempArray colorData( colorDataSize ); + + idTempArray pngData( colorDataSize ); + + for( int h = 0; h < height; h++ ) + { + for( int w = 0; w < width; w++ ) + { + int atlasPixelOfs = ( entry.imageAtlasOffset[0] + w + ( ( entry.imageAtlasOffset[1] + h ) * atlasImageWidth ) ) * 4; + + //atlasPixelOfs = idMath::ClampInt( atlasPixelOfs + + const byte* atlasPixel = atlasImageRGBA + atlasPixelOfs; + + byte* pixel = &colorData[( w + ( h * width ) ) * 4]; + + pixel[0] = atlasPixel[3]; + pixel[1] = atlasPixel[0]; + pixel[2] = atlasPixel[1]; + pixel[3] = atlasPixel[2]; + + pixel = &pngData[( w + ( h * width ) ) * 4]; + pixel[0] = atlasPixel[0]; + pixel[1] = atlasPixel[1]; + pixel[2] = atlasPixel[2]; + pixel[3] = atlasPixel[3]; + } + } + + idStr imageExportFileName; + idStr filenameWithoutExt = filename; + filenameWithoutExt.StripFileExtension(); + sprintf( imageExportFileName, "generated/%s/image_characterid_%i.png", filenameWithoutExt.c_str(), i ); + + R_WritePNG( imageExportFileName.c_str(), pngData.Ptr(), 4, width, height, true, "fs_basepath" ); // RB: add some extra space for zlib - idTempArray bitmapData( width * height * 4 * 1.02 + 12 ); - uint32 colorDataSize = width * height * 4; //bitstream.Length() - bitstream.Tell(); - int compressedDataSize = bitmapData.Size(); - if( !Deflate( atlasImageRGBA, colorDataSize, ( byte* )bitmapData.Ptr(), compressedDataSize ) ) + idTempArray compressedData( width * height * 4 * 1.02 + 12 ); + int compressedDataSize = compressedData.Size(); + + if( !Deflate( colorData.Ptr(), colorDataSize, ( byte* )compressedData.Ptr(), compressedDataSize ) ) { idLib::Warning( "DefineBitsLossless: Failed to deflate bitmap data" ); //return; @@ -230,18 +248,13 @@ void idSWF::WriteSWF( const char* filename, const byte* atlasImageRGBA, int atla int tagLength = ( 2 + 1 + 2 + 2 ) + compressedDataSize; - file.WriteTagHeader( Tag_DefineBitsLossless, tagLength ); + file.WriteTagHeader( Tag_DefineBitsLossless2, tagLength ); file.WriteU16( i ); // characterID file.WriteU8( 5 ); // format - file.WriteU16( entry.imageSize[0] ); // width - file.WriteU16( entry.imageSize[1] ); // height - - - - - - file->Write( bitmapData.Ptr(), compressedDataSize ); + file.WriteU16( width ); // width + file.WriteU16( height ); // height + file->Write( compressedData.Ptr(), compressedDataSize ); //file->WriteFloatString( "\t\tLoad( bitstream, true ); - // now that all images have been loaded, write out the combined image - //idStr atlasFileName = "generated/"; - //atlasFileName += fullpath; - //atlasFileName.SetFileExtension( ".tga" ); - - //WriteSwfImageAtlas( atlasFileName ); - - //Mem_Free( fileData ); - // add Tag_End file.WriteTagHeader( Tag_End, 0 ); // go back and write filesize into header uint32 fileSize = file->Length(); - uint32 headerFileLengthOfs = offsetof( swfHeader_t, fileLength ); - file->Seek( headerFileLengthOfs, FS_SEEK_SET ); + file->Seek( offsetof( swfHeader_t, fileLength ), FS_SEEK_SET ); file.WriteU32( fileSize ); } +// RB end /* =================== diff --git a/neo/swf/SWF_Zlib.cpp b/neo/swf/SWF_Zlib.cpp index dcfb4dda..f9db5e64 100644 --- a/neo/swf/SWF_Zlib.cpp +++ b/neo/swf/SWF_Zlib.cpp @@ -85,7 +85,7 @@ bool idSWF::Deflate( const byte* input, int inputSize, byte* output, int& output stream.zalloc = local_swf_alloc_t::zalloc; stream.zfree = local_swf_alloc_t::zfree; - int err = deflateInit( &stream, Z_NO_COMPRESSION ); + int err = deflateInit( &stream, Z_DEFAULT_COMPRESSION ); if( err != Z_OK ) return false; From e14b1fff08fe295336cfdd441aed335db284c765 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 12 Nov 2013 18:17:03 +0100 Subject: [PATCH 15/43] SWF sprites export --- neo/swf/SWF_File.cpp | 12 ++++++ neo/swf/SWF_File.h | 1 + neo/swf/SWF_Load.cpp | 9 +++- neo/swf/SWF_SpriteInstance.cpp | 4 +- neo/swf/SWF_Sprites.cpp | 77 +++++++++++++++++++++++++++++++++- neo/swf/SWF_Sprites.h | 2 + 6 files changed, 101 insertions(+), 4 deletions(-) diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index 915f8eba..125babca 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -146,4 +146,16 @@ void idFile_SWF::WriteTagHeader( swfTag_t tag, int32 tagLength ) } } +int32 idFile_SWF::GetTagHeaderSize( swfTag_t tag, int32 tagLength ) +{ + int32 size = 2; + + if( tagLength >= 0x3F ) + { + size = 6; + } + + return size; +} + diff --git a/neo/swf/SWF_File.h b/neo/swf/SWF_File.h index 0492c99f..6861f202 100644 --- a/neo/swf/SWF_File.h +++ b/neo/swf/SWF_File.h @@ -80,6 +80,7 @@ public: void WriteRect( const swfRect_t& rect ); + static int32 GetTagHeaderSize( swfTag_t tag, int32 tagLength ); void WriteTagHeader( swfTag_t tag, int32 tagLength ); private: diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 401bbe61..5b37e494 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -276,11 +276,16 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a */ break; } + + case SWF_DICT_SPRITE: + { + dictionary[i].sprite->WriteSWF( file, i ); + break; + } } } - // parse everything - //mainsprite->Load( bitstream, true ); + mainsprite->WriteSWF( file, dictionary.Num() ); // add Tag_End file.WriteTagHeader( Tag_End, 0 ); diff --git a/neo/swf/SWF_SpriteInstance.cpp b/neo/swf/SWF_SpriteInstance.cpp index 88f5393e..d0660087 100644 --- a/neo/swf/SWF_SpriteInstance.cpp +++ b/neo/swf/SWF_SpriteInstance.cpp @@ -427,7 +427,7 @@ void idSWFSpriteInstance::RunTo( int targetFrame ) targetFrame = sprite->frameOffsets.Num() - 1; } - //actions.Clear(); + // actions.Clear(); uint32 firstActionCommand = sprite->frameOffsets[ targetFrame - 1 ]; @@ -468,9 +468,11 @@ idSWFSpriteInstance::DoAction */ void idSWFSpriteInstance::DoAction( idSWFBitStream& bitstream ) { +#if 1 swfAction_t& action = actions.Alloc(); action.data = bitstream.ReadData( bitstream.Length() ); action.dataLength = bitstream.Length(); +#endif } /* diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 86655efa..193c1fc9 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -546,7 +546,7 @@ void idSWFSprite::WriteXML_RemoveObject2( idFile* file, idSWFBitStream& bitstrea { int depth = bitstream.ReadU16(); - file->WriteFloatString( "%s\t", indentPrefix, depth ); + file->WriteFloatString( "%s\t\n", indentPrefix, depth ); } void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) @@ -558,5 +558,80 @@ void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, co file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); } + +void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) +{ + int tagLength = 4; + + for( int i = 0; i < doInitActions.Num(); i++ ) + { + tagLength += idFile_SWF::GetTagHeaderSize( Tag_DoInitAction, doInitActions[i].Length() ); + tagLength += doInitActions[i].Length(); + } + + for( int i = 0; i < commands.Num(); i++ ) + { + idSWFSprite::swfSpriteCommand_t& command = commands[i]; + + switch( command.tag ) + { + case Tag_PlaceObject2: + //case Tag_PlaceObject3: + //case Tag_RemoveObject2: + //case Tag_DoAction: + tagLength += idFile_SWF::GetTagHeaderSize( command.tag, command.stream.Length() ); + tagLength += command.stream.Length(); + break; + + default: + break; + //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); + } + } + + f.WriteTagHeader( Tag_DefineSprite, tagLength ); + + f.WriteU16( characterID ); + f.WriteU16( frameCount ); + + + /* + for( int i = 0; i < frameLabels.Num(); i++ ) + { + f->WriteFloatString( "%s\t\n", indentPrefix, frameLabels[i].frameNum, frameLabels[i].frameLabel.c_str() ); + } + */ + + for( int i = 0; i < doInitActions.Num(); i++ ) + { + f.WriteTagHeader( Tag_DoInitAction, doInitActions[i].Length() ); + f.Write( doInitActions[i].Ptr(), doInitActions[i].Length() ); + } + + for( int i = 0; i < commands.Num(); i++ ) + { + idSWFSprite::swfSpriteCommand_t& command = commands[i]; + + command.stream.Rewind(); + switch( command.tag ) + { + case Tag_PlaceObject2: + //case Tag_PlaceObject3: + //case Tag_RemoveObject2: + //case Tag_DoAction: + f.WriteTagHeader( command.tag, command.stream.Length() ); + f.Write( command.stream.Ptr(), command.stream.Length() ); + break; + + default: + break; + //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); + } + + } + + f.WriteTagHeader( Tag_End, 0 ); +} + // RB end >>>>>>> 2f86bde... Extended Shape-Command exports diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index bde0bfc8..c63af7db 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -54,6 +54,8 @@ public: void WriteXML_RemoveObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); void WriteXML_DoAction( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); + void WriteSWF( idFile_SWF& f, int characterID = -1 ); + uint16 GetFrameCount() { return frameCount; From 038f798f8845a994e5a415a78971f54df4c47f03 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 12 Nov 2013 23:05:59 +0100 Subject: [PATCH 16/43] SWF shapes export --- neo/swf/SWF_File.cpp | 146 +++++++++++++++++++++++++++++++++++++--- neo/swf/SWF_File.h | 12 +++- neo/swf/SWF_Load.cpp | 136 +++++++++++++++++++++++++++++++------ neo/swf/SWF_Main.cpp | 31 ++------- neo/swf/SWF_Sprites.cpp | 21 +++--- neo/swf/SWF_Types.h | 11 ++- 6 files changed, 286 insertions(+), 71 deletions(-) diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index 125babca..8d779d31 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -44,16 +44,70 @@ idFile_SWF::~idFile_SWF() -//uint idFile_SWF::GetNumBits( int64 value, bool isSigned ) -//{ -// //return idMath::BitCount( -//} - -uint idFile_SWF::GetNumBitsInt( const int value ) +int idFile_SWF::BitCountS( const int value ) { - return idMath::BitCount( value ); + //int nBits = idMath::BitCount( value ); + + + int count = 0; + + int v = value; + while( v > 0 ) + { + if( ( v & 1 ) == 1 ) + { + // lower bit is set + count++; + } + + // shift bits, remove lower bit + v >>= 1; + } + + return count; } +int idFile_SWF::BitCountU( const int value ) +{ + int nBits = idMath::BitCount( value ); + + return nBits; +} + +int idFile_SWF::BitCountFloat( const float value ) +{ + int value2 = ( int ) value; + + int nBits = BitCountS( value2 ); + + return nBits; +} + +int idFile_SWF::EnlargeBitCountS( const int value, int numBits ) +{ + int n = BitCountS( value ); + if( n > numBits ) + { + numBits = n; + } + + return numBits; +} + +int idFile_SWF::EnlargeBitCountU( const int value, int numBits ) +{ + int n = BitCountU( value ); + if( n > numBits ) + { + numBits = n; + } + + return numBits; +} + + + + int idFile_SWF::Write( const void* buffer, int len ) { return file->Write( buffer, len ); @@ -117,10 +171,10 @@ void idFile_SWF::WriteRect( const swfRect_t& rect ) { int nBits = rect.BitCount(); - int tl_x = PIXEL2SWFTWIP( rect.tl.x ); - int br_x = PIXEL2SWFTWIP( rect.br.x ); - int tl_y = PIXEL2SWFTWIP( rect.tl.y ); - int br_y = PIXEL2SWFTWIP( rect.br.y ); + int tl_x = FLOAT2SWFTWIP( rect.tl.x ); + int br_x = FLOAT2SWFTWIP( rect.br.x ); + int tl_y = FLOAT2SWFTWIP( rect.tl.y ); + int br_y = FLOAT2SWFTWIP( rect.br.y ); WriteUBits( nBits, 5 ); WriteSBits( tl_x, nBits ); @@ -129,6 +183,76 @@ void idFile_SWF::WriteRect( const swfRect_t& rect ) WriteSBits( br_y, nBits ); } +void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) +{ + //ByteAlign(); + + bool hasScale = ( matrix.xx != 1.0f || matrix.yy != 1.0f ); + WriteUBits( hasScale ? 1 : 0, 1 ); + + if( hasScale ) + { + int nBits = 0; + + int xx = FLOAT2SWFFIXED16( matrix.xx ); + int yy = FLOAT2SWFFIXED16( matrix.yy ); + + nBits = EnlargeBitCountS( xx, nBits ); + nBits = EnlargeBitCountS( yy, nBits ); + + WriteUBits( 5, nBits ); + WriteSBits( xx, nBits ); + WriteSBits( yy, nBits ); + } + + bool hasRotate = ( matrix.yx != 0 && matrix.xy != 0 ); + WriteUBits( hasRotate ? 1 : 0, 1 ); + + if( hasRotate ) + { + int nBits = 0; + + int yx = FLOAT2SWFFIXED16( matrix.yx ); + int xy = FLOAT2SWFFIXED16( matrix.xy ); + + nBits = EnlargeBitCountS( yx, nBits ); + nBits = EnlargeBitCountS( xy, nBits ); + + WriteUBits( 5, nBits ); + WriteSBits( yx, nBits ); + WriteSBits( xy, nBits ); + } + + int nBits = 0; + int tx = FLOAT2SWFTWIP( matrix.tx ); + int ty = FLOAT2SWFTWIP( matrix.ty ); + + nBits = EnlargeBitCountS( tx, nBits ); + nBits = EnlargeBitCountS( ty, nBits ); + + WriteUBits( 5, nBits ); + WriteSBits( tx, nBits ); + WriteSBits( ty, nBits ); + + ByteAlign(); +} + +void idFile_SWF::WriteColorRGB( const swfColorRGB_t& color ) +{ + WriteByte( color.r ); + WriteByte( color.g ); + WriteByte( color.b ); +} + +void idFile_SWF::WriteColorRGBA( const swfColorRGBA_t& color ) +{ + WriteByte( color.r ); + WriteByte( color.g ); + WriteByte( color.b ); + WriteByte( color.a ); +} + + void idFile_SWF::WriteTagHeader( swfTag_t tag, int32 tagLength ) { uint16 tagIDLength = ( ( int ) tag << 6 ); diff --git a/neo/swf/SWF_File.h b/neo/swf/SWF_File.h index 6861f202..72685fcd 100644 --- a/neo/swf/SWF_File.h +++ b/neo/swf/SWF_File.h @@ -65,9 +65,12 @@ public: } - uint GetNumBits( int64 value, bool isSigned ); - uint GetNumBitsInt( const int value ); - uint GetNumBitsFloat( const int value ); + static int BitCountS( const int value ); + static int BitCountU( const int value ); + static int BitCountFloat( const float value ); + + static int EnlargeBitCountS( const int value, int numBits ); + static int EnlargeBitCountU( const int value, int numBits ); virtual int Write( const void* buffer, int len ); @@ -79,6 +82,9 @@ public: void WriteU32( uint32 value ); void WriteRect( const swfRect_t& rect ); + void WriteMatrix( const swfMatrix_t& matrix ); + void WriteColorRGB( const swfColorRGB_t& color ); + void WriteColorRGBA( const swfColorRGBA_t& color ); static int32 GetTagHeaderSize( swfTag_t tag, int32 tagLength ); void WriteTagHeader( swfTag_t tag, int32 tagLength ); diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 5b37e494..fa0b058f 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -191,7 +191,6 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a { const idSWFDictionaryEntry& entry = dictionary[i]; - //file->WriteFloatString( "\t\n", idSWF::GetDictTypeName( dictionary[i].type ) ); switch( dictionary[i].type ) { case SWF_DICT_IMAGE: @@ -255,37 +254,128 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a file.WriteU16( width ); // width file.WriteU16( height ); // height file->Write( compressedData.Ptr(), compressedDataSize ); - - //file->WriteFloatString( "\t\tWriteFloatString( "%s\"", dictionary[i].material->GetName() ); - } - else - { - file->WriteFloatString( ".\"" ); - } - - file->WriteFloatString( " width=\"%i\" height=\"%i\" atlasOffsetX=\"%i\" atlasOffsetY=\"%i\">\n", - entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); - - file->WriteFloatString( "\t\t\t\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); - - file->WriteFloatString( "\t\t\n" ); - */ break; } +#if 1 + case SWF_DICT_SHAPE: + { + idSWFShape* shape = dictionary[i].shape; + + + + int numFillDraws = 0; + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + + if( fillDraw.style.type == 0 ) + { + numFillDraws++; + } + } + + if( numFillDraws == 0 ) + { + continue; + } + + idFile_Memory* tagMem = new idFile_Memory( "shapeTag" ); + idFile_SWF tag( tagMem ); + + tag.WriteU16( i ); // characterID + tag.WriteRect( shape->startBounds ); + + tag.WriteU8( 0xFF ); + tag.WriteU16( numFillDraws ); + + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + + + if( fillDraw.style.type == 0 ) + { + tag.WriteColorRGBA( fillDraw.style.startColor ); + } + /* + else + + if( fillDraw.style.type == 4 ) + { + //uint8 styleType = ( ( int ) fillDraw.style.type << 4 ); + tag.WriteU8( 0x40 ); + + // bitmap + tag.WriteU16( fillDraw.style.bitmapID ); + tag.WriteMatrix( fillDraw.style.startMatrix ); + } + */ + + // type: 0 = solid, 1 = gradient, 4 = bitmap + //if( fillDraw.style.type == 0 ) + + //file->WriteBig( fillDraw.style.type ); + //file->WriteBig( fillDraw.style.subType ); + //file->Write( &fillDraw.style.startColor, 4 ); + //file->Write( &fillDraw.style.endColor, 4 ); + /*file->WriteBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); + file->WriteBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); + file->WriteBig( fillDraw.style.gradient.numGradients ); + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + file->WriteBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); + file->WriteBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); + file->Write( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); + file->Write( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); + } + file->WriteBig( fillDraw.style.focalPoint ); + file->WriteBig( fillDraw.style.bitmapID ); + file->WriteBig( fillDraw.startVerts.Num() ); + file->WriteBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); + file->WriteBig( fillDraw.endVerts.Num() ); + file->WriteBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); + file->WriteBig( fillDraw.indices.Num() ); + file->WriteBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() );*/ + } + + // TODO + tag.WriteU8( 0xFF ); + tag.WriteU16( 0 ); + + /* + file->WriteBig( shape->lineDraws.Num() ); + for( int d = 0; d < shape->lineDraws.Num(); d++ ) + { + idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + file->WriteBig( lineDraw.style.startWidth ); + file->WriteBig( lineDraw.style.endWidth ); + file->Write( &lineDraw.style.startColor, 4 ); + file->Write( &lineDraw.style.endColor, 4 ); + file->WriteBig( lineDraw.startVerts.Num() ); + file->WriteBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); + file->WriteBig( lineDraw.endVerts.Num() ); + file->WriteBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); + file->WriteBig( lineDraw.indices.Num() ); + file->WriteBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); + } + */ + + file.WriteTagHeader( Tag_DefineShape3, tag->Length() ); + file.Write( tagMem->GetDataPtr(), tagMem->Length() ); + break; + } +#endif + case SWF_DICT_SPRITE: { - dictionary[i].sprite->WriteSWF( file, i ); + //dictionary[i].sprite->WriteSWF( file, i ); break; } } } - mainsprite->WriteSWF( file, dictionary.Num() ); + //mainsprite->WriteSWF( file, dictionary.Num() ); // add Tag_End file.WriteTagHeader( Tag_End, 0 ); @@ -800,10 +890,12 @@ void idSWF::WriteXML( const char* filename ) { idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + /* if( fillDraw.style.type != 4 ) { continue; } + */ file->WriteFloatString( "\t\t\t\n" ); diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 2bb9756f..80d49e88 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -49,33 +49,14 @@ extern idCVar in_useJoystick; // RB begin int swfRect_t::BitCount() const { - int c = 0; + int num = 0; - int num = idMath::BitCount( ( int ) tl.x ); - if( num > c ) - { - c = num; - } + num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( tl.x ), num ); + num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( tl.y ), num ); + num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( br.x ), num ); + num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( br.x ), num ); - num = idMath::BitCount( ( int ) tl.y ); - if( num > c ) - { - c = num; - } - - num = idMath::BitCount( ( int ) br.x ); - if( num > c ) - { - c = num; - } - - num = idMath::BitCount( ( int ) br.y ); - if( num > c ) - { - c = num; - } - - return c; + return num; } // RB end diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 193c1fc9..a7ddd725 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -561,6 +561,10 @@ void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, co void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) { + // TODO frameLabels + + // TODO Tag_ShowFrames + int tagLength = 4; for( int i = 0; i < doInitActions.Num(); i++ ) @@ -576,16 +580,16 @@ void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) switch( command.tag ) { case Tag_PlaceObject2: - //case Tag_PlaceObject3: - //case Tag_RemoveObject2: - //case Tag_DoAction: + case Tag_PlaceObject3: + case Tag_RemoveObject2: + case Tag_DoAction: tagLength += idFile_SWF::GetTagHeaderSize( command.tag, command.stream.Length() ); tagLength += command.stream.Length(); break; default: - break; //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); + break; } } @@ -595,6 +599,7 @@ void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) f.WriteU16( frameCount ); + /* for( int i = 0; i < frameLabels.Num(); i++ ) { @@ -616,16 +621,16 @@ void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) switch( command.tag ) { case Tag_PlaceObject2: - //case Tag_PlaceObject3: - //case Tag_RemoveObject2: - //case Tag_DoAction: + case Tag_PlaceObject3: + case Tag_RemoveObject2: + case Tag_DoAction: f.WriteTagHeader( command.tag, command.stream.Length() ); f.Write( command.stream.Ptr(), command.stream.Length() ); break; default: - break; //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); + break; } } diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 1df85caa..8233c531 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -42,11 +42,18 @@ ID_INLINE float SWFFIXED8( int fixed ) return fixed * ( 1.0f / 256.0f ); } -ID_INLINE int PIXEL2SWFTWIP( float pixel ) +// RB begin +ID_INLINE int FLOAT2SWFTWIP( float value ) { - return ( int )( pixel * 20 ); + return ( int )( value * 20 ); } +ID_INLINE int FLOAT2SWFFIXED16( float value ) +{ + return ( int )( value * 65536 ); +} +// RB end + struct swfHeader_t { byte compression; From dd056c4ecc49ebe0c34cfde3bcc809ca6b55d416 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Wed, 13 Nov 2013 13:59:58 +0100 Subject: [PATCH 17/43] SWF shapes export --- neo/swf/SWF_File.cpp | 26 ++++++++++++++++++++------ neo/swf/SWF_Load.cpp | 28 ++++++++++++++++++---------- neo/swf/SWF_Main.cpp | 13 ------------- neo/swf/SWF_Types.h | 4 ---- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index 8d779d31..090a5a23 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -46,11 +46,11 @@ idFile_SWF::~idFile_SWF() int idFile_SWF::BitCountS( const int value ) { - //int nBits = idMath::BitCount( value ); - - int count = 0; +#if 1 + count = idMath::BitCount( value ); +#else int v = value; while( v > 0 ) { @@ -59,10 +59,11 @@ int idFile_SWF::BitCountS( const int value ) // lower bit is set count++; } - + // shift bits, remove lower bit v >>= 1; } +#endif return count; } @@ -110,6 +111,8 @@ int idFile_SWF::EnlargeBitCountU( const int value, int numBits ) int idFile_SWF::Write( const void* buffer, int len ) { + ByteAlign(); + return file->Write( buffer, len ); } @@ -169,18 +172,25 @@ void idFile_SWF::WriteU32( uint32 value ) void idFile_SWF::WriteRect( const swfRect_t& rect ) { - int nBits = rect.BitCount(); - int tl_x = FLOAT2SWFTWIP( rect.tl.x ); int br_x = FLOAT2SWFTWIP( rect.br.x ); int tl_y = FLOAT2SWFTWIP( rect.tl.y ); int br_y = FLOAT2SWFTWIP( rect.br.y ); + int nBits = 0; + + nBits = EnlargeBitCountS( tl_x, nBits ); + nBits = EnlargeBitCountS( br_x, nBits ); + nBits = EnlargeBitCountS( tl_y, nBits ); + nBits = EnlargeBitCountS( br_y, nBits ); + WriteUBits( nBits, 5 ); WriteSBits( tl_x, nBits ); WriteSBits( br_x, nBits ); WriteSBits( tl_y, nBits ); WriteSBits( br_y, nBits ); + + ByteAlign(); } void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) @@ -239,6 +249,8 @@ void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) void idFile_SWF::WriteColorRGB( const swfColorRGB_t& color ) { + ByteAlign(); + WriteByte( color.r ); WriteByte( color.g ); WriteByte( color.b ); @@ -246,6 +258,8 @@ void idFile_SWF::WriteColorRGB( const swfColorRGB_t& color ) void idFile_SWF::WriteColorRGBA( const swfColorRGBA_t& color ) { + ByteAlign(); + WriteByte( color.r ); WriteByte( color.g ); WriteByte( color.b ); diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index fa0b058f..dab6a189 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -269,7 +269,7 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a { idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - if( fillDraw.style.type == 0 ) + if( fillDraw.style.type == 0 /* || fillDraw.style.type == 4 */ ) { numFillDraws++; } @@ -286,8 +286,15 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a tag.WriteU16( i ); // characterID tag.WriteRect( shape->startBounds ); - tag.WriteU8( 0xFF ); - tag.WriteU16( numFillDraws ); + if( numFillDraws >= 0xFF ) + { + tag.WriteU8( 0xFF ); + tag.WriteU16( numFillDraws ); + } + else + { + tag.WriteU8( numFillDraws ); + } for( int d = 0; d < shape->fillDraws.Num(); d++ ) { @@ -296,22 +303,23 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a if( fillDraw.style.type == 0 ) { + // solid + tag.WriteU8( 0x00 ); tag.WriteColorRGBA( fillDraw.style.startColor ); } /* else - - if( fillDraw.style.type == 4 ) + if( fillDraw.style.type == 4 ) { - //uint8 styleType = ( ( int ) fillDraw.style.type << 4 ); + // bitmap tag.WriteU8( 0x40 ); - // bitmap tag.WriteU16( fillDraw.style.bitmapID ); tag.WriteMatrix( fillDraw.style.startMatrix ); } */ + // type: 0 = solid, 1 = gradient, 4 = bitmap //if( fillDraw.style.type == 0 ) @@ -340,8 +348,8 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a } // TODO - tag.WriteU8( 0xFF ); - tag.WriteU16( 0 ); + tag.WriteU8( 0 ); // no lines + tag.WriteU8( 1 ); // no shapes /* file->WriteBig( shape->lineDraws.Num() ); @@ -361,7 +369,7 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a } */ - file.WriteTagHeader( Tag_DefineShape3, tag->Length() ); + file.WriteTagHeader( Tag_DefineShape3, tagMem->Length() ); file.Write( tagMem->GetDataPtr(), tagMem->Length() ); break; } diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 80d49e88..83d3fab4 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -46,20 +46,7 @@ bool idSWF::isMouseInClientArea = false; extern idCVar in_useJoystick; -// RB begin -int swfRect_t::BitCount() const -{ - int num = 0; - - num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( tl.x ), num ); - num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( tl.y ), num ); - num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( br.x ), num ); - num = idFile_SWF::EnlargeBitCountS( FLOAT2SWFTWIP( br.x ), num ); - - return num; -} -// RB end /* =================== diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 8233c531..e344ab6c 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -67,10 +67,6 @@ struct swfRect_t swfRect_t(); idVec2 tl; idVec2 br; - - // RB begin - int BitCount() const; - // RB end }; struct swfMatrix_t { From a353c3f23315f954efaa62c21fd4b47401045450 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Thu, 15 May 2014 16:58:48 +0200 Subject: [PATCH 18/43] SWF shapes export --- neo/swf/SWF_Load.cpp | 39 ++++++++++++++++++++++++--------------- neo/swf/SWF_Main.cpp | 4 ++-- neo/swf/SWF_Sprites.cpp | 10 ++++++++-- neo/swf/SWF_Types.h | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index dab6a189..2fd2810b 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -262,8 +262,6 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a { idSWFShape* shape = dictionary[i].shape; - - int numFillDraws = 0; for( int d = 0; d < shape->fillDraws.Num(); d++ ) { @@ -833,6 +831,8 @@ idSWF::WriteXML */ void idSWF::WriteXML( const char* filename ) { + const bool exportBitmapShapesOnly = false; + idFileLocal file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); if( file == NULL ) { @@ -898,12 +898,10 @@ void idSWF::WriteXML( const char* filename ) { idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - /* - if( fillDraw.style.type != 4 ) + if( exportBitmapShapesOnly && fillDraw.style.type != 4 ) { continue; } - */ file->WriteFloatString( "\t\t\t\n" ); @@ -979,9 +977,12 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); - m = fillDraw.style.endMatrix; - file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", - m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + if( fillDraw.style.startMatrix != fillDraw.style.endMatrix ) + { + m = fillDraw.style.endMatrix; + file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", + m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + } } for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) @@ -994,9 +995,12 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\t\t\t\n", color.x, color.y, color.z, color.w ); - color = gr.endColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); + idVec4 endColor = gr.endColor.ToVec4(); + if( color != endColor ) + { + file->WriteFloatString( "\t\t\t\t\t\t\n", + color.x, color.y, color.z, endColor.w ); + } } file->WriteFloatString( "\t\t\t\t\n" ); @@ -1028,7 +1032,7 @@ void idSWF::WriteXML( const char* filename ) } // export line draws -#if 0 +#if 1 for( int d = 0; d < shape->lineDraws.Num(); d++ ) { const idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; @@ -1041,9 +1045,12 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\t\t\n", color.x, color.y, color.z, color.w ); - color = lineDraw.style.endColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); + idVec4 endColor = lineDraw.style.endColor.ToVec4(); + if( color != endColor ) + { + file->WriteFloatString( "\t\t\t\t\t\n", + endColor.x, endColor.y, endColor.z, endColor.w ); + } file->WriteFloatString( "\t\t\t\t\n" ); @@ -1089,6 +1096,7 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\n", i, font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); +#if 0 for( int g = 0; g < font->glyphs.Num(); g++ ) { file->WriteFloatString( "\t\t\t\n", font->glyphs[g].code, font->glyphs[g].advance ); @@ -1113,6 +1121,7 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\t\t\n" ); #endif } +#endif file->WriteFloatString( "\t\t\n" ); break; } diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 83d3fab4..ebbaabfa 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -35,9 +35,9 @@ If you have questions concerning this license or the applicable additional terms idCVar swf_loadBinary( "swf_loadBinary", "1", CVAR_INTEGER, "used to set whether to load binary swf from generated" ); // RB begin -idCVar swf_exportSWF( "swf_exportSWF", "1", CVAR_INTEGER, "" ); +idCVar swf_exportAtlas( "swf_exportAtlas", "0", CVAR_INTEGER, "" ); +idCVar swf_exportSWF( "swf_exportSWF", "0", CVAR_INTEGER, "" ); idCVar swf_exportXML( "swf_exportXML", "1", CVAR_INTEGER, "" ); -idCVar swf_exportAtlas( "swf_exportAtlas", "1", CVAR_INTEGER, "" ); // RB end int idSWF::mouseX = -1; diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index a7ddd725..87935d5a 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -409,7 +409,10 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); color = cxf.add; - file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + if( color != vec4_origin ) + { + file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + } } if( ( flags & PlaceFlagHasRatio ) != 0 ) @@ -486,7 +489,10 @@ void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); color = cxf.add; - file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + if( color != vec4_origin ) + { + file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + } } if( ( flags1 & PlaceFlagHasRatio ) != 0 ) diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index e344ab6c..82673850 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -88,6 +88,20 @@ struct swfMatrix_t ty = a.ty; return *this; } + + // RB begin + bool operator==( const swfMatrix_t& a ) + { + return ( xx == a.xx && yy == a.yy && xy == a.xy && yx == a.yx && tx == a.tx && ty == a.ty ); + + } + + bool operator!=( const swfMatrix_t& a ) + { + return !( xx == a.xx && yy == a.yy && xy == a.xy && yx == a.yx && tx == a.tx && ty == a.ty ); + + } + // RB end }; struct swfColorRGB_t { From 8b99afd5c18a1e8435643adb1ea469511e66ab44 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 1 May 2015 13:45:18 +0200 Subject: [PATCH 19/43] Changed SWF export dir to base/exported/ --- neo/swf/SWF_Load.cpp | 10 +++++----- neo/swf/SWF_Main.cpp | 7 ++++--- neo/swf/SWF_Types.h | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 2fd2810b..95bfc9a4 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -231,7 +231,7 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a idStr imageExportFileName; idStr filenameWithoutExt = filename; filenameWithoutExt.StripFileExtension(); - sprintf( imageExportFileName, "generated/%s/image_characterid_%i.png", filenameWithoutExt.c_str(), i ); + sprintf( imageExportFileName, "exported/%s/image_characterid_%i.png", filenameWithoutExt.c_str(), i ); R_WritePNG( imageExportFileName.c_str(), pngData.Ptr(), 4, width, height, true, "fs_basepath" ); @@ -832,7 +832,7 @@ idSWF::WriteXML void idSWF::WriteXML( const char* filename ) { const bool exportBitmapShapesOnly = false; - + idFileLocal file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); if( file == NULL ) { @@ -999,7 +999,7 @@ void idSWF::WriteXML( const char* filename ) if( color != endColor ) { file->WriteFloatString( "\t\t\t\t\t\t\n", - color.x, color.y, color.z, endColor.w ); + color.x, color.y, color.z, endColor.w ); } } @@ -1049,9 +1049,9 @@ void idSWF::WriteXML( const char* filename ) if( color != endColor ) { file->WriteFloatString( "\t\t\t\t\t\n", - endColor.x, endColor.y, endColor.z, endColor.w ); + endColor.x, endColor.y, endColor.z, endColor.w ); } - + file->WriteFloatString( "\t\t\t\t\n" ); for( int v = 0; v < lineDraw.startVerts.Num(); v++ ) diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index ebbaabfa..74a38841 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -36,7 +36,7 @@ If you have questions concerning this license or the applicable additional terms idCVar swf_loadBinary( "swf_loadBinary", "1", CVAR_INTEGER, "used to set whether to load binary swf from generated" ); // RB begin idCVar swf_exportAtlas( "swf_exportAtlas", "0", CVAR_INTEGER, "" ); -idCVar swf_exportSWF( "swf_exportSWF", "0", CVAR_INTEGER, "" ); +idCVar swf_exportSWF( "swf_exportSWF", "1", CVAR_INTEGER, "" ); idCVar swf_exportXML( "swf_exportXML", "1", CVAR_INTEGER, "" ); // RB end @@ -156,7 +156,7 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) // RB begin if( swf_exportXML.GetBool() ) { - idStr xmlFileName = "generated/"; + idStr xmlFileName = "exported/"; xmlFileName += filename; xmlFileName.SetFileExtension( ".xml" ); @@ -266,6 +266,7 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) } idStr atlasFileNameExport = atlasFileName; + atlasFileNameExport.Replace( "generated/", "exported/" ); atlasFileNameExport.SetFileExtension( ".png" ); R_WritePNG( atlasFileNameExport, rgba.Ptr(), 4, img.width, img.height, true, "fs_basepath" ); @@ -282,7 +283,7 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) if( swf_exportSWF.GetBool() ) { - idStr swfFileName = "generated/"; + idStr swfFileName = "exported/"; swfFileName += filename; swfFileName.SetFileExtension( ".swf" ); diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 82673850..ddec8a00 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -88,14 +88,14 @@ struct swfMatrix_t ty = a.ty; return *this; } - + // RB begin bool operator==( const swfMatrix_t& a ) { return ( xx == a.xx && yy == a.yy && xy == a.xy && yx == a.yx && tx == a.tx && ty == a.ty ); } - + bool operator!=( const swfMatrix_t& a ) { return !( xx == a.xx && yy == a.yy && xy == a.xy && yx == a.yx && tx == a.tx && ty == a.ty ); From 71e339a83ba9c2853477132dfa0adec4307a89da Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Thu, 4 Jun 2015 22:28:29 +0200 Subject: [PATCH 20/43] Partial Flash bytecode to ActionScript export --- neo/swf/SWF_Load.cpp | 6 +- neo/swf/SWF_ScriptFunction.cpp | 1241 ++++++++++++++++++++++++++++++++ neo/swf/SWF_ScriptFunction.h | 5 + neo/swf/SWF_SpriteInstance.h | 2 + neo/swf/SWF_Sprites.cpp | 24 +- 5 files changed, 1273 insertions(+), 5 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 95bfc9a4..a04233e9 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -257,7 +257,7 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a break; } -#if 1 +#if 0 case SWF_DICT_SHAPE: { idSWFShape* shape = dictionary[i].shape; @@ -375,13 +375,13 @@ void idSWF::WriteSWF( const char* swfFilename, const byte* atlasImageRGBA, int a case SWF_DICT_SPRITE: { - //dictionary[i].sprite->WriteSWF( file, i ); + dictionary[i].sprite->WriteSWF( file, i ); break; } } } - //mainsprite->WriteSWF( file, dictionary.Num() ); + mainsprite->WriteSWF( file, dictionary.Num() ); // add Tag_End file.WriteTagHeader( Tag_End, 0 ); diff --git a/neo/swf/SWF_ScriptFunction.cpp b/neo/swf/SWF_ScriptFunction.cpp index ce620a94..d44ed61d 100644 --- a/neo/swf/SWF_ScriptFunction.cpp +++ b/neo/swf/SWF_ScriptFunction.cpp @@ -265,6 +265,156 @@ idSWFScriptVar idSWFScriptFunction_Script::Call( idSWFScriptObject* thisObject, return retVal; } +// RB begin +idStr idSWFScriptFunction_Script::CallToScript( idSWFScriptObject* thisObject, const idSWFParmList& parms ) +{ + idSWFBitStream bitstream( data, length, false ); + + // We assume scope[0] is the global scope + assert( scope.Num() > 0 ); + + if( thisObject == NULL ) + { + thisObject = scope[0]; + } + + idSWFScriptObject* locals = idSWFScriptObject::Alloc(); + + idSWFStack stack; + stack.SetNum( parms.Num() + 1 ); + for( int i = 0; i < parms.Num(); i++ ) + { + stack[ parms.Num() - i - 1 ] = parms[i]; + + // Unfortunately at this point we don't have the function name anymore, so our warning messages aren't very detailed + if( i < parameters.Num() ) + { + if( parameters[i].reg > 0 && parameters[i].reg < registers.Num() ) + { + registers[ parameters[i].reg ] = parms[i]; + } + locals->Set( parameters[i].name, parms[i] ); + } + } + // Set any additional parameters to undefined + for( int i = parms.Num(); i < parameters.Num(); i++ ) + { + if( parameters[i].reg > 0 && parameters[i].reg < registers.Num() ) + { + registers[ parameters[i].reg ].SetUndefined(); + } + locals->Set( parameters[i].name, idSWFScriptVar() ); + } + stack.A().SetInteger( parms.Num() ); + + int preloadReg = 1; + if( flags & BIT( 0 ) ) + { + // load "this" into a register + registers[ preloadReg ].SetObject( thisObject ); + preloadReg++; + } + if( ( flags & BIT( 1 ) ) == 0 ) + { + // create "this" + locals->Set( "this", idSWFScriptVar( thisObject ) ); + } + if( flags & BIT( 2 ) ) + { + idSWFScriptObject* arguments = idSWFScriptObject::Alloc(); + // load "arguments" into a register + arguments->MakeArray(); + + int numElements = parms.Num(); + + for( int i = 0; i < numElements; i++ ) + { + arguments->Set( i, parms[i] ); + } + + registers[ preloadReg ].SetObject( arguments ); + preloadReg++; + + arguments->Release(); + } + if( ( flags & BIT( 3 ) ) == 0 ) + { + idSWFScriptObject* arguments = idSWFScriptObject::Alloc(); + + // create "arguments" + arguments->MakeArray(); + + int numElements = parms.Num(); + + for( int i = 0; i < numElements; i++ ) + { + arguments->Set( i, parms[i] ); + } + + locals->Set( "arguments", idSWFScriptVar( arguments ) ); + + arguments->Release(); + } + if( flags & BIT( 4 ) ) + { + // load "super" into a register + registers[ preloadReg ].SetObject( thisObject->GetPrototype() ); + preloadReg++; + } + if( ( flags & BIT( 5 ) ) == 0 ) + { + // create "super" + locals->Set( "super", idSWFScriptVar( thisObject->GetPrototype() ) ); + } + if( flags & BIT( 6 ) ) + { + // preload _root + registers[ preloadReg ] = scope[0]->Get( "_root" ); + preloadReg++; + } + if( flags & BIT( 7 ) ) + { + // preload _parent + if( thisObject->GetSprite() != NULL && thisObject->GetSprite()->parent != NULL ) + { + registers[ preloadReg ].SetObject( thisObject->GetSprite()->parent->scriptObject ); + } + else + { + registers[ preloadReg ].SetNULL(); + } + preloadReg++; + } + if( flags & BIT( 8 ) ) + { + // load "_global" into a register + registers[ preloadReg ].SetObject( scope[0] ); + preloadReg++; + } + + int scopeSize = scope.Num(); + scope.Append( locals ); + locals->AddRef(); + + idStr retVal = ExportToScript( thisObject, stack, bitstream ); + + assert( scope.Num() == scopeSize + 1 ); + for( int i = scopeSize; i < scope.Num(); i++ ) + { + if( verify( scope[i] ) ) + { + scope[i]->Release(); + } + } + scope.SetNum( scopeSize ); + + locals->Release(); + locals = NULL; + + return retVal; +} +// RB end + /* ======================== ::Split @@ -1572,6 +1722,1097 @@ idSWFScriptVar idSWFScriptFunction_Script::Run( idSWFScriptObject* thisObject, i return idSWFScriptVar(); } +// RB begin +idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream ) +{ + static int callstackLevel = -1; + idSWFSpriteInstance* thisSprite = thisObject->GetSprite(); + idSWFSpriteInstance* currentTarget = thisSprite; + + if( currentTarget == NULL ) + { + thisSprite = currentTarget = defaultSprite; + } + + callstackLevel++; + + idStr actionScript; + + //idStr line; + + while( bitstream.Tell() < bitstream.Length() ) + { + swfAction_t code = ( swfAction_t )bitstream.ReadU8(); + uint16 recordLength = 0; + if( code >= 0x80 ) + { + recordLength = bitstream.ReadU16(); + } + + //if( swf_debug.GetInteger() >= 3 ) + { + // stack[0] is always 0 so don't read it + //if( swf_debug.GetInteger() >= 4 ) + { + for( int i = stack.Num() - 1; i >= 0 ; i-- ) + { + idLib::Printf( " %c: %s (%s)\n", ( char )( 64 + stack.Num() - i ), stack[i].ToString().c_str(), stack[i].TypeOf() ); + } + +#if 0 + for( int i = 0; i < registers.Num(); i++ ) + { + if( !registers[i].IsUndefined() ) + { + idLib::Printf( " R%d: %s (%s)\n", i, registers[i].ToString().c_str(), registers[i].TypeOf() ); + } + } +#endif + } + + idLib::Printf( "SWF%d: code %s\n", callstackLevel, GetSwfActionName( code ) ); + } + + switch( code ) + { + case Action_Return: + callstackLevel--; + actionScript += "return " + stack.A().ToString() + "\n"; + return actionScript; + case Action_End: + callstackLevel--; + return actionScript; + case Action_NextFrame: + //if( verify( currentTarget != NULL ) ) + //{ + // currentTarget->NextFrame(); + //} + actionScript += "nextFrame();\n"; + break; + case Action_PrevFrame: + //if( verify( currentTarget != NULL ) ) + //{ + // currentTarget->PrevFrame(); + //} + actionScript += "prevFrame();\n"; + break; + case Action_Play: + //if( verify( currentTarget != NULL ) ) + //{ + // currentTarget->Play(); + //} + actionScript += "play();\n"; + break; + case Action_Stop: + //if( verify( currentTarget != NULL ) ) + //{ + // currentTarget->Stop(); + //} + actionScript += "stop();\n"; + break; + case Action_ToggleQuality: + break; + case Action_StopSounds: + break; + case Action_GotoFrame: + { + assert( recordLength == 2 ); + int frameNum = bitstream.ReadU16() + 1; + + actionScript += va( "gotoAndPlay( %i );\n", frameNum ); + break; + } + case Action_SetTarget: + { + const char* targetName = ( const char* )bitstream.ReadData( recordLength ); + if( verify( thisSprite != NULL ) ) + { + currentTarget = thisSprite->ResolveTarget( targetName ); + } + else if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: no target movie clip for setTarget %s\n", targetName ); + } + break; + } + case Action_GoToLabel: + { + const char* targetName = ( const char* )bitstream.ReadData( recordLength ); + if( verify( currentTarget != NULL ) ) + { + currentTarget->RunTo( currentTarget->FindFrame( targetName ) ); + } + else if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: no target movie clip for runTo %s\n", targetName ); + } + break; + } + case Action_Push: + { + idSWFBitStream pushstream( bitstream.ReadData( recordLength ), recordLength, false ); + while( pushstream.Tell() < pushstream.Length() ) + { + uint8 type = pushstream.ReadU8(); + switch( type ) + { + case 0: + stack.Alloc().SetString( pushstream.ReadString() ); + break; + case 1: + stack.Alloc().SetFloat( pushstream.ReadFloat() ); + break; + case 2: + stack.Alloc().SetNULL(); + break; + case 3: + stack.Alloc().SetUndefined(); + break; + case 4: + stack.Alloc() = registers[ pushstream.ReadU8() ]; + break; + case 5: + stack.Alloc().SetBool( pushstream.ReadU8() != 0 ); + break; + case 6: + stack.Alloc().SetFloat( ( float )pushstream.ReadDouble() ); + break; + case 7: + stack.Alloc().SetInteger( pushstream.ReadS32() ); + break; + case 8: + stack.Alloc().SetString( constants.Get( pushstream.ReadU8() ) ); + break; + case 9: + stack.Alloc().SetString( constants.Get( pushstream.ReadU16() ) ); + break; + } + } + break; + } + case Action_Pop: + + if( stack.A().IsString() ) + { + actionScript += stack.A().ToString() + ";\n"; + //line.Empty(); + } + + stack.Pop( 1 ); + break; + case Action_Add: + //stack.B().SetFloat( stack.B().ToFloat() + stack.A().ToFloat() ); + stack.B().SetString( va( "%s + %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Subtract: + //stack.B().SetFloat( stack.B().ToFloat() - stack.A().ToFloat() ); + stack.B().SetString( va( "%s - %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Multiply: + //stack.B().SetFloat( stack.B().ToFloat() * stack.A().ToFloat() ); + stack.B().SetString( va( "%s * %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Divide: + //stack.B().SetFloat( stack.B().ToFloat() / stack.A().ToFloat() ); + stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Equals: + //stack.B().SetBool( stack.B().ToFloat() == stack.A().ToFloat() ); + stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Less: + //stack.B().SetBool( stack.B().ToFloat() < stack.A().ToFloat() ); + stack.B().SetString( va( "%s < %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_And: + //stack.B().SetBool( stack.B().ToBool() && stack.A().ToBool() ); + stack.B().SetString( va( "%s && %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Or: + //stack.B().SetBool( stack.B().ToBool() || stack.A().ToBool() ); + stack.B().SetString( va( "%s || %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_Not: + //stack.A().SetBool( !stack.A().ToBool() ); + stack.A().SetString( va( "!( %s )", stack.A().ToString().c_str() ) ); + break; + case Action_StringEquals: + //stack.B().SetBool( stack.B().ToString() == stack.A().ToString() ); + stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); + break; + case Action_StringLength: + //stack.A().SetInteger( stack.A().ToString().Length() ); + stack.A().SetString( va( "%s:len()", stack.A().ToString().c_str() ) ); + break; + case Action_StringAdd: + //stack.B().SetString( stack.B().ToString() + stack.A().ToString() ); + stack.B().SetString( stack.B().ToString() + stack.A().ToString() ); + stack.Pop( 1 ); + break; + case Action_StringExtract: + stack.C().SetString( stack.C().ToString().Mid( stack.B().ToInteger(), stack.A().ToInteger() ) ); + stack.Pop( 2 ); + break; + case Action_StringLess: + stack.B().SetBool( stack.B().ToString() < stack.A().ToString() ); + stack.Pop( 1 ); + break; + case Action_StringGreater: + stack.B().SetBool( stack.B().ToString() > stack.A().ToString() ); + stack.Pop( 1 ); + break; + case Action_ToInteger: + stack.A().SetInteger( stack.A().ToInteger() ); + break; + case Action_CharToAscii: + stack.A().SetInteger( stack.A().ToString()[0] ); + break; + case Action_AsciiToChar: + stack.A().SetString( va( "%c", stack.A().ToInteger() ) ); + break; + case Action_Jump: + bitstream.Seek( bitstream.ReadS16() ); + + actionScript += "\n--FIXME Action_Jump not implemented"; + return actionScript; + break; + case Action_If: + { + actionScript += va( "if( %s )\n{\n", stack.A().ToString().c_str() ) ; + + int16 offset = bitstream.ReadS16(); + if( stack.A().ToBool() ) + { + bitstream.Seek( offset ); + } + stack.Pop( 1 ); + break; + } + case Action_GetVariable: + { + idStr variableName = stack.A().ToString(); + + /* + for( int i = scope.Num() - 1; i >= 0; i-- ) + { + stack.A() = scope[i]->Get( variableName ); + if( !stack.A().IsUndefined() ) + { + break; + } + } + if( stack.A().IsUndefined() ) // + { + if( swf_debug.GetInteger() > 1 ) + { + idLib::Printf( "SWF: unknown variable %s\n", variableName.c_str() ); + } + stack.A().SetString( variableName ); + } + */ + + //line += variableName; + break; + } + case Action_SetVariable: + { + idStr variableName = stack.B().ToString(); + bool found = false; + for( int i = scope.Num() - 1; i >= 0; i-- ) + { + if( scope[i]->HasProperty( variableName ) ) + { + scope[i]->Set( variableName, stack.A() ); + found = true; + break; + } + } + if( !found ) + { + thisObject->Set( variableName, stack.A() ); + } + stack.Pop( 2 ); + break; + } + case Action_GotoFrame2: + { + + uint32 frameNum = 0; + uint8 flags = bitstream.ReadU8(); + if( flags & 2 ) + { + frameNum += bitstream.ReadU16(); + } + + if( verify( thisSprite != NULL ) ) + { + if( stack.A().IsString() ) + { + frameNum += thisSprite->FindFrame( stack.A().ToString() ); + } + else + { + frameNum += ( uint32 )stack.A().ToInteger(); + } + if( ( flags & 1 ) != 0 ) + { + thisSprite->Play(); + } + else + { + thisSprite->Stop(); + } + thisSprite->RunTo( frameNum ); + } + else if( swf_debug.GetInteger() > 0 ) + { + if( ( flags & 1 ) != 0 ) + { + idLib::Printf( "SWF: no target movie clip for gotoAndPlay\n" ); + } + else + { + idLib::Printf( "SWF: no target movie clip for gotoAndStop\n" ); + } + } + stack.Pop( 1 ); + break; + } + case Action_GetProperty: + { + if( verify( thisSprite != NULL ) ) + { + idSWFSpriteInstance* target = thisSprite->ResolveTarget( stack.B().ToString() ); + stack.B() = target->scriptObject->Get( GetPropertyName( stack.A().ToInteger() ) ); + } + else if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: no target movie clip for getProperty\n" ); + } + stack.Pop( 1 ); + break; + } + case Action_SetProperty: + { + if( verify( thisSprite != NULL ) ) + { + idSWFSpriteInstance* target = thisSprite->ResolveTarget( stack.C().ToString() ); + target->scriptObject->Set( GetPropertyName( stack.B().ToInteger() ), stack.A() ); + } + else if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: no target movie clip for setProperty\n" ); + } + stack.Pop( 3 ); + break; + } + case Action_Trace: + idLib::PrintfIf( swf_debug.GetInteger() > 0, "SWF Trace: %s\n", stack.A().ToString().c_str() ); + stack.Pop( 1 ); + break; + case Action_GetTime: + stack.Alloc().SetInteger( Sys_Milliseconds() ); + break; + case Action_RandomNumber: + assert( thisSprite && thisSprite->sprite && thisSprite->sprite->GetSWF() ); + stack.A().SetInteger( thisSprite->sprite->GetSWF()->GetRandom().RandomInt( stack.A().ToInteger() ) ); + break; + case Action_CallFunction: + { + idStr functionName = stack.A().ToString(); + idSWFScriptVar function; + idSWFScriptObject* object = NULL; + for( int i = scope.Num() - 1; i >= 0; i-- ) + { + function = scope[i]->Get( functionName ); + if( !function.IsUndefined() ) + { + object = scope[i]; + break; + } + } + stack.Pop( 1 ); + + idStr line = va( "%s( ", functionName.c_str() ); + + idSWFParmList parms; + parms.SetNum( stack.A().ToInteger() ); + stack.Pop( 1 ); + for( int i = 0; i < parms.Num(); i++ ) + { + parms[i] = stack.A(); + + if( stack.A().IsString() ) + { + line += va( "\"%s\"%s", stack.A().ToString().c_str(), ( i < ( parms.Num() - 1 ) ) ? ", " : " " ); + } + else + { + line += va( "%s%s", stack.A().ToString().c_str(), ( i < ( parms.Num() - 1 ) ) ? ", " : " " ); + } + + stack.Pop( 1 ); + } + + line += ")"; + + if( function.IsFunction() && verify( object ) ) + { + stack.Alloc() = function.GetFunction()->Call( object, parms ); + } + else + { + idLib::PrintfIf( swf_debug.GetInteger() > 0, "SWF: unknown function %s\n", functionName.c_str() ); + + stack.Alloc().SetString( line ); + } + + break; + } + case Action_CallMethod: + { + idStr functionName = stack.A().ToString(); + // If the top stack is undefined but there is an object, it's calling the constructor + if( functionName.IsEmpty() || stack.A().IsUndefined() || stack.A().IsNULL() ) + { + functionName = "__constructor__"; + } + idSWFScriptObject* object = NULL; + idSWFScriptVar function; + if( stack.B().IsObject() ) + { + object = stack.B().GetObject(); + function = object->Get( functionName ); + if( !function.IsFunction() ) + { + idLib::PrintfIf( swf_debug.GetInteger() > 1, "SWF: unknown method %s on %s\n", functionName.c_str(), object->DefaultValue( true ).ToString().c_str() ); + } + } + else + { + idLib::PrintfIf( swf_debug.GetInteger() > 1, "SWF: NULL object for method %s\n", functionName.c_str() ); + } + + idStr line = va( "%s.%s( ", stack.B().ToString().c_str(), functionName.c_str() ); + + stack.Pop( 2 ); + + idSWFParmList parms; + parms.SetNum( stack.A().ToInteger() ); + stack.Pop( 1 ); + for( int i = 0; i < parms.Num(); i++ ) + { + parms[i] = stack.A(); + + if( stack.A().IsString() ) + { + line += va( "\"%s\"%s", stack.A().ToString().c_str(), ( i < ( parms.Num() - 1 ) ) ? ", " : " " ); + } + else + { + line += va( "%s%s", stack.A().ToString().c_str(), ( i < ( parms.Num() - 1 ) ) ? ", " : " " ); + } + + stack.Pop( 1 ); + } + + line += ")"; + + if( function.IsFunction() ) + { + stack.Alloc() = function.GetFunction()->Call( object, parms ); + } + else + { + stack.Alloc().SetString( line ); + } + + //line.Empty(); + break; + } + case Action_ConstantPool: + { + constants.Clear(); + uint16 numConstants = bitstream.ReadU16(); + for( int i = 0; i < numConstants; i++ ) + { + constants.Append( idSWFScriptString::Alloc( bitstream.ReadString() ) ); + } + break; + } + case Action_DefineFunction: + { + idStr functionName = bitstream.ReadString(); + + idSWFScriptFunction_Script* newFunction = idSWFScriptFunction_Script::Alloc(); + newFunction->SetScope( scope ); + newFunction->SetConstants( constants ); + newFunction->SetDefaultSprite( defaultSprite ); + + uint16 numParms = bitstream.ReadU16(); + newFunction->AllocParameters( numParms ); + for( int i = 0; i < numParms; i++ ) + { + newFunction->SetParameter( i, 0, bitstream.ReadString() ); + } + uint16 codeSize = bitstream.ReadU16(); + newFunction->SetData( bitstream.ReadData( codeSize ), codeSize ); + + if( functionName.IsEmpty() ) + { + stack.Alloc().SetFunction( newFunction ); + } + else + { + thisObject->Set( functionName, idSWFScriptVar( newFunction ) ); + } + newFunction->Release(); + break; + } + case Action_DefineFunction2: + { + idStr functionName = bitstream.ReadString(); + + idSWFScriptFunction_Script* newFunction = idSWFScriptFunction_Script::Alloc(); + newFunction->SetScope( scope ); + newFunction->SetConstants( constants ); + newFunction->SetDefaultSprite( defaultSprite ); + + uint16 numParms = bitstream.ReadU16(); + + // The number of registers is from 0 to 255, although valid values are 1 to 256. + // There must always be at least one register for DefineFunction2, to hold "this" or "super" when required. + uint8 numRegs = bitstream.ReadU8() + 1; + + // Note that SWF byte-ordering causes the flag bits to be reversed per-byte + // from how the swf_file_format_spec_v10.pdf document describes the ordering in ActionDefineFunction2. + // PreloadThisFlag is byte 0, not 7, PreloadGlobalFlag is 8, not 15. + uint16 flags = bitstream.ReadU16(); + + newFunction->AllocParameters( numParms ); + newFunction->AllocRegisters( numRegs ); + newFunction->SetFlags( flags ); + + for( int i = 0; i < numParms; i++ ) + { + uint8 reg = bitstream.ReadU8(); + const char* name = bitstream.ReadString(); + if( reg >= numRegs ) + { + idLib::Warning( "SWF: Parameter %s in function %s bound to out of range register %d", name, functionName.c_str(), reg ); + reg = 0; + } + newFunction->SetParameter( i, reg, name ); + } + + uint16 codeSize = bitstream.ReadU16(); + newFunction->SetData( bitstream.ReadData( codeSize ), codeSize ); + + if( functionName.IsEmpty() ) + { + stack.Alloc().SetFunction( newFunction ); + } + else + { + thisObject->Set( functionName, idSWFScriptVar( newFunction ) ); + } + newFunction->Release(); + break; + } + case Action_Enumerate: + { + idStr variableName = stack.A().ToString(); + for( int i = scope.Num() - 1; i >= 0; i-- ) + { + stack.A() = scope[i]->Get( variableName ); + if( !stack.A().IsUndefined() ) + { + break; + } + } + if( !stack.A().IsObject() ) + { + stack.A().SetNULL(); + } + else + { + idSWFScriptObject* object = stack.A().GetObject(); + object->AddRef(); + stack.A().SetNULL(); + for( int i = 0; i < object->NumVariables(); i++ ) + { + stack.Alloc().SetString( object->EnumVariable( i ) ); + } + object->Release(); + } + break; + } + case Action_Enumerate2: + { + if( !stack.A().IsObject() ) + { + stack.A().SetNULL(); + } + else + { + idSWFScriptObject* object = stack.A().GetObject(); + object->AddRef(); + stack.A().SetNULL(); + for( int i = 0; i < object->NumVariables(); i++ ) + { + stack.Alloc().SetString( object->EnumVariable( i ) ); + } + object->Release(); + } + break; + } + case Action_Equals2: + { + //stack.B().SetBool( stack.A().AbstractEquals( stack.B() ) ); + stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + + stack.Pop( 1 ); + break; + } + case Action_StrictEquals: + { + //stack.B().SetBool( stack.A().StrictEquals( stack.B() ) ); + stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + + stack.Pop( 1 ); + break; + } + case Action_GetMember: + { + if( ( stack.B().IsUndefined() || stack.B().IsNULL() ) && swf_debug.GetInteger() > 1 ) + { + idLib::Printf( "SWF: tried to get member %s on an invalid object in sprite '%s'\n", stack.A().ToString().c_str(), thisSprite != NULL ? thisSprite->GetName() : "" ); + } + + /* + if( stack.B().IsObject() ) + { + idSWFScriptObject* object = stack.B().GetObject(); + if( stack.A().IsNumeric() ) + { + stack.B() = object->Get( stack.A().ToInteger() ); + } + else + { + stack.B() = object->Get( stack.A().ToString() ); + } + if( stack.B().IsUndefined() && swf_debug.GetInteger() > 1 ) + { + idLib::Printf( "SWF: unknown member %s\n", stack.A().ToString().c_str() ); + } + } + else if( stack.B().IsString() ) + { + idStr propertyName = stack.A().ToString(); + if( propertyName.Cmp( "length" ) == 0 ) + { + stack.B().SetInteger( stack.B().ToString().Length() ); + } + else if( propertyName.Cmp( "value" ) == 0 ) + { + // Do nothing + } + else + { + stack.B().SetUndefined(); + } + } + else if( stack.B().IsFunction() ) + { + idStr propertyName = stack.A().ToString(); + if( propertyName.Cmp( "prototype" ) == 0 ) + { + // if this is a function, it's a class definition function, and it just wants the prototype object + // create it if it hasn't been already, and return it + idSWFScriptFunction* sfs = stack.B().GetFunction(); + idSWFScriptObject* object = sfs->GetPrototype(); + + if( object == NULL ) + { + object = idSWFScriptObject::Alloc(); + // Set the __proto__ to the main Object prototype + idSWFScriptVar baseObjConstructor = scope[0]->Get( "Object" ); + idSWFScriptFunction* baseObj = baseObjConstructor.GetFunction(); + object->Set( "__proto__", baseObj->GetPrototype() ); + sfs->SetPrototype( object ); + } + + stack.B() = idSWFScriptVar( object ); + } + else + { + stack.B().SetUndefined(); + } + } + else + { + stack.B().SetUndefined(); + } + */ + + //line = va( "%s.%s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ); + + //line += va( "%s.", stack.B().ToString().c_str() ); + + //actionScript += line; + //line.Empty(); + + stack.B().SetString( va( "%s.%s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + + stack.Pop( 1 ); + break; + } + case Action_SetMember: + { + //if( stack.C().IsObject() ) + { + //idSWFScriptObject* object = stack.C().GetObject(); + //if( stack.B().IsNumeric() ) + //{ + // object->Set( stack.B().ToInteger(), stack.A() ); + // + // line += va( ".%s = %i;\n", stack.B().ToString().c_str(), stack.A().ToInteger() ); + //} + //else + + actionScript += va( "%s.%s = %s;\n", stack.C().ToString().c_str(), stack.B().ToString().c_str(), stack.A().ToString().c_str() ); + } + stack.Pop( 3 ); + break; + } + case Action_InitArray: + { + idSWFScriptObject* object = idSWFScriptObject::Alloc(); + object->MakeArray(); + + int numElements = stack.A().ToInteger(); + stack.Pop( 1 ); + + for( int i = 0; i < numElements; i++ ) + { + object->Set( i, stack.A() ); + stack.Pop( 1 ); + } + + stack.Alloc().SetObject( object ); + + object->Release(); + break; + } + case Action_InitObject: + { + idSWFScriptObject* object = idSWFScriptObject::Alloc(); + + int numElements = stack.A().ToInteger(); + stack.Pop( 1 ); + + for( int i = 0; i < numElements; i++ ) + { + object->Set( stack.B().ToString(), stack.A() ); + stack.Pop( 2 ); + } + + stack.Alloc().SetObject( object ); + + object->Release(); + break; + } + case Action_NewObject: + { + idSWFScriptObject* object = idSWFScriptObject::Alloc(); + + idStr functionName = stack.A().ToString(); + stack.Pop( 1 ); + + if( functionName.Cmp( "Array" ) == 0 ) + { + object->MakeArray(); + + int numElements = stack.A().ToInteger(); + stack.Pop( 1 ); + + for( int i = 0; i < numElements; i++ ) + { + object->Set( i, stack.A() ); + stack.Pop( 1 ); + } + + idSWFScriptVar baseObjConstructor = scope[0]->Get( "Object" ); + idSWFScriptFunction* baseObj = baseObjConstructor.GetFunction(); + object->Set( "__proto__", baseObj->GetPrototype() ); + // object prototype is not set here because it will be auto created from Object later + } + else + { + idSWFParmList parms; + parms.SetNum( stack.A().ToInteger() ); + stack.Pop( 1 ); + for( int i = 0; i < parms.Num(); i++ ) + { + parms[i] = stack.A(); + stack.Pop( 1 ); + } + + idSWFScriptVar objdef = scope[0]->Get( functionName ); + if( objdef.IsFunction() ) + { + idSWFScriptFunction* constructorFunction = objdef.GetFunction(); + object->Set( "__proto__", constructorFunction->GetPrototype() ); + object->SetPrototype( constructorFunction->GetPrototype() ); + constructorFunction->Call( object, parms ); + } + else + { + idLib::Warning( "SWF: Unknown class definition %s", functionName.c_str() ); + } + } + + stack.Alloc().SetObject( object ); + + object->Release(); + break; + } + case Action_Extends: + { + idSWFScriptFunction* superclassConstructorFunction = stack.A().GetFunction(); + idSWFScriptFunction* subclassConstructorFunction = stack.B().GetFunction(); + stack.Pop( 2 ); + + idSWFScriptObject* scriptObject = idSWFScriptObject::Alloc(); + scriptObject->SetPrototype( superclassConstructorFunction->GetPrototype() ); + scriptObject->Set( "__proto__", idSWFScriptVar( superclassConstructorFunction->GetPrototype() ) ); + scriptObject->Set( "__constructor__", idSWFScriptVar( superclassConstructorFunction ) ); + + subclassConstructorFunction->SetPrototype( scriptObject ); + + scriptObject->Release(); + break; + } + case Action_TargetPath: + { + if( !stack.A().IsObject() ) + { + stack.A().SetUndefined(); + } + else + { + idSWFScriptObject* object = stack.A().GetObject(); + if( object->GetSprite() == NULL ) + { + stack.A().SetUndefined(); + } + else + { + idStr dotName = object->GetSprite()->name.c_str(); + for( idSWFSpriteInstance* target = object->GetSprite()->parent; target != NULL; target = target->parent ) + { + dotName = target->name + "." + dotName; + } + stack.A().SetString( dotName ); + } + } + break; + } + case Action_With: + { + int withSize = bitstream.ReadU16(); + idSWFBitStream bitstream2( bitstream.ReadData( withSize ), withSize, false ); + if( stack.A().IsObject() ) + { + idSWFScriptObject* withObject = stack.A().GetObject(); + withObject->AddRef(); + stack.Pop( 1 ); + scope.Append( withObject ); + Run( thisObject, stack, bitstream2 ); + scope.SetNum( scope.Num() - 1 ); + withObject->Release(); + } + else + { + if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: with() invalid object specified\n" ); + } + stack.Pop( 1 ); + } + break; + } + case Action_ToNumber: + stack.A().SetFloat( stack.A().ToFloat() ); + break; + case Action_ToString: + stack.A().SetString( stack.A().ToString() ); + break; + case Action_TypeOf: + stack.A().SetString( stack.A().TypeOf() ); + break; + case Action_Add2: + { + if( stack.A().IsString() || stack.B().IsString() ) + { + stack.B().SetString( stack.B().ToString() + stack.A().ToString() ); + } + else + { + stack.B().SetFloat( stack.B().ToFloat() + stack.A().ToFloat() ); + } + stack.Pop( 1 ); + break; + } + case Action_Less2: + { + if( stack.A().IsString() && stack.B().IsString() ) + { + stack.B().SetBool( stack.B().ToString() < stack.A().ToString() ); + } + else + { + stack.B().SetBool( stack.B().ToFloat() < stack.A().ToFloat() ); + } + stack.Pop( 1 ); + break; + } + case Action_Greater: + { + if( stack.A().IsString() && stack.B().IsString() ) + { + stack.B().SetBool( stack.B().ToString() > stack.A().ToString() ); + } + else + { + stack.B().SetBool( stack.B().ToFloat() > stack.A().ToFloat() ); + } + stack.Pop( 1 ); + break; + } + case Action_Modulo: + { + int32 a = stack.A().ToInteger(); + int32 b = stack.B().ToInteger(); + if( a == 0 ) + { + stack.B().SetUndefined(); + } + else + { + stack.B().SetInteger( b % a ); + } + stack.Pop( 1 ); + break; + } + case Action_BitAnd: + stack.B().SetInteger( stack.B().ToInteger() & stack.A().ToInteger() ); + stack.Pop( 1 ); + break; + case Action_BitLShift: + stack.B().SetInteger( stack.B().ToInteger() << stack.A().ToInteger() ); + stack.Pop( 1 ); + break; + case Action_BitOr: + stack.B().SetInteger( stack.B().ToInteger() | stack.A().ToInteger() ); + stack.Pop( 1 ); + break; + case Action_BitRShift: + stack.B().SetInteger( stack.B().ToInteger() >> stack.A().ToInteger() ); + stack.Pop( 1 ); + break; + case Action_BitURShift: + stack.B().SetInteger( ( uint32 )stack.B().ToInteger() >> stack.A().ToInteger() ); + stack.Pop( 1 ); + break; + case Action_BitXor: + stack.B().SetInteger( stack.B().ToInteger() ^ stack.A().ToInteger() ); + stack.Pop( 1 ); + break; + case Action_Decrement: + stack.A().SetFloat( stack.A().ToFloat() - 1.0f ); + break; + case Action_Increment: + stack.A().SetFloat( stack.A().ToFloat() + 1.0f ); + break; + case Action_PushDuplicate: + { + idSWFScriptVar dup = stack.A(); + stack.Alloc() = dup; + break; + } + case Action_StackSwap: + { + idSWFScriptVar temp = stack.A(); + stack.A() = stack.B(); + stack.A() = temp; + break; + } + case Action_StoreRegister: + { + uint8 registerNumber = bitstream.ReadU8(); + registers[ registerNumber ] = stack.A(); + break; + } + case Action_DefineLocal: + { + scope[scope.Num() - 1]->Set( stack.B().ToString(), stack.A() ); + stack.Pop( 2 ); + break; + } + case Action_DefineLocal2: + { + scope[scope.Num() - 1]->Set( stack.A().ToString(), idSWFScriptVar() ); + stack.Pop( 1 ); + break; + } + case Action_Delete: + { + if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: Delete ignored\n" ); + } + // We no longer support deleting variables because the performance cost of updating the hash tables is not worth it + stack.Pop( 2 ); + break; + } + case Action_Delete2: + { + if( swf_debug.GetInteger() > 0 ) + { + idLib::Printf( "SWF: Delete2 ignored\n" ); + } + // We no longer support deleting variables because the performance cost of updating the hash tables is not worth it + stack.Pop( 1 ); + break; + } + // These are functions we just don't support because we never really needed to + case Action_CloneSprite: + case Action_RemoveSprite: + case Action_Call: + case Action_SetTarget2: + case Action_NewMethod: + default: + idLib::Warning( "SWF: Unhandled Action %s", idSWF::GetActionName( code ) ); + // We have to abort here because the rest of the script is basically meaningless now + assert( false ); + callstackLevel--; + return actionScript; + } + } + callstackLevel--; + return actionScript; +} + /* ======================== idSWF::Invoke diff --git a/neo/swf/SWF_ScriptFunction.h b/neo/swf/SWF_ScriptFunction.h index 778179c2..ca64d8b5 100644 --- a/neo/swf/SWF_ScriptFunction.h +++ b/neo/swf/SWF_ScriptFunction.h @@ -252,8 +252,13 @@ public: virtual idSWFScriptVar Call( idSWFScriptObject* thisObject, const idSWFParmList& parms ); + // RB begin + idStr CallToScript( idSWFScriptObject* thisObject, const idSWFParmList& parms ); + private: idSWFScriptVar Run( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream ); + idStr ExportToScript( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream ); + // RB end private: int refCount; diff --git a/neo/swf/SWF_SpriteInstance.h b/neo/swf/SWF_SpriteInstance.h index 51acca96..4cd03aa3 100644 --- a/neo/swf/SWF_SpriteInstance.h +++ b/neo/swf/SWF_SpriteInstance.h @@ -304,4 +304,6 @@ public: //SWF_NATIVE_VAR_DECLARE( onLoad ); }; +extern idSWFScriptObject_SpriteInstancePrototype spriteInstanceScriptObjectPrototype; + #endif diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 87935d5a..837de719 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -561,7 +561,27 @@ void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, co base64.Encode( bitstream.Ptr(), bitstream.Length() ); - file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); + idSWFScriptObject* scriptObject = idSWFScriptObject::Alloc(); + scriptObject->SetPrototype( &spriteInstanceScriptObjectPrototype ); +// scriptObject->SetSprite( this ); + + idSWFScriptFunction_Script* actionScript = idSWFScriptFunction_Script::Alloc(); + + idList scope; + //scope.Append( swf->globals ); + scope.Append( scriptObject ); + actionScript->SetScope( scope ); +// actionScript->SetDefaultSprite( this ); + + actionScript->SetData( bitstream.Ptr(), bitstream.Length() ); + idStr scriptText = actionScript->CallToScript( scriptObject, idSWFParmList() ); + + //file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); + + file->WriteFloatString( "%s\t\n%s\t\t%s\n%s\t\n", indentPrefix, bitstream.Length(), indentPrefix, scriptText.c_str(), indentPrefix ); + + delete actionScript; + delete scriptObject; } @@ -641,7 +661,7 @@ void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) } - f.WriteTagHeader( Tag_End, 0 ); + //f.WriteTagHeader( Tag_End, 0 ); } // RB end From 21de7488019b30b89181e21d2e06dd6ae5f024ce Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sat, 6 Jun 2015 16:28:43 +0200 Subject: [PATCH 21/43] Flash bytecode to Lua --- neo/swf/SWF_Load.cpp | 4 +- neo/swf/SWF_ScriptFunction.cpp | 419 +++++++++++++++++++++++++-------- neo/swf/SWF_ScriptFunction.h | 23 +- neo/swf/SWF_ScriptVar.cpp | 12 +- neo/swf/SWF_ScriptVar.h | 24 +- neo/swf/SWF_Sprites.cpp | 16 +- neo/swf/SWF_Sprites.h | 12 +- 7 files changed, 391 insertions(+), 119 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index a04233e9..7f6c2c2c 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -1085,7 +1085,7 @@ void idSWF::WriteXML( const char* filename ) case SWF_DICT_SPRITE: { - dictionary[i].sprite->WriteXML( file, "\t\t", i ); + dictionary[i].sprite->WriteXML( file, i, "\t\t" ); break; } @@ -1235,7 +1235,7 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\t\n" ); - mainsprite->WriteXML( file, "\t" ); + mainsprite->WriteXML( file, dictionary.Num(), "\t" ); file->WriteFloatString( "\n" ); } diff --git a/neo/swf/SWF_ScriptFunction.cpp b/neo/swf/SWF_ScriptFunction.cpp index d44ed61d..1a50df64 100644 --- a/neo/swf/SWF_ScriptFunction.cpp +++ b/neo/swf/SWF_ScriptFunction.cpp @@ -266,7 +266,7 @@ idSWFScriptVar idSWFScriptFunction_Script::Call( idSWFScriptObject* thisObject, } // RB begin -idStr idSWFScriptFunction_Script::CallToScript( idSWFScriptObject* thisObject, const idSWFParmList& parms ) +idStr idSWFScriptFunction_Script::CallToScript( idSWFScriptObject* thisObject, const idSWFParmList& parms, const char* filename, int characterID, int actionID ) { idSWFBitStream bitstream( data, length, false ); @@ -396,7 +396,7 @@ idStr idSWFScriptFunction_Script::CallToScript( idSWFScriptObject* thisObject, c scope.Append( locals ); locals->AddRef(); - idStr retVal = ExportToScript( thisObject, stack, bitstream ); + idStr retVal = ExportToScript( thisObject, stack, bitstream, filename, characterID, actionID ); assert( scope.Num() == scopeSize + 1 ); for( int i = scopeSize; i < scope.Num(); i++ ) @@ -1723,7 +1723,68 @@ idSWFScriptVar idSWFScriptFunction_Script::Run( idSWFScriptObject* thisObject, i } // RB begin -idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream ) +idStr idSWFScriptFunction_Script::UpdateIndent( int indentLevel ) const +{ + idStr indent; + for( int i = 0; i < indentLevel; i++ ) + { + indent += '\t'; + } + + return indent; +} + +void idSWFScriptFunction_Script::AddLine( const idStr& line ) +{ + ActionBlock& block = currentBlock->blocks.Alloc(); + block.parent = currentBlock; + block.line = line; +} + +void idSWFScriptFunction_Script::AddBlock( const idStr& line ) +{ + ActionBlock& block = currentBlock->blocks.Alloc(); + block.parent = currentBlock; + + block.line = line; + currentBlock = █ +} + +void idSWFScriptFunction_Script::QuitCurrentBlock() +{ + if( currentBlock->parent != NULL ) + { + currentBlock = currentBlock->parent; + } +} + +idStr idSWFScriptFunction_Script::BuildActionCode( const idList& blocks, int level ) +{ + idStr ret; + + idStr prefix = UpdateIndent( level ); + + for( int i = 0; i < blocks.Num(); i++ ) + { + const ActionBlock& block = blocks[i]; + + if( !block.line.IsEmpty() ) + { + ret += prefix + block.line + "\n"; + } + + if( block.blocks.Num() ) + { + ret += BuildActionCode( block.blocks, ++level ); + + ret += prefix + "end\n"; + } + } + + return ret; +} + +idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream, const char* filename, int characterID, int actionID ) { static int callstackLevel = -1; idSWFSpriteInstance* thisSprite = thisObject->GetSprite(); @@ -1736,9 +1797,14 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, callstackLevel++; - idStr actionScript; +// idStr actionScript; + + actionBlocks.Clear(); + currentBlock = &actionBlocks.Alloc(); + currentBlock->line = va( "function sprite%i_action%i()", characterID, actionID ); - //idStr line; + //int indentLevel = 0; + //idStr indent; while( bitstream.Tell() < bitstream.Length() ) { @@ -1754,9 +1820,11 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, // stack[0] is always 0 so don't read it //if( swf_debug.GetInteger() >= 4 ) { + //idLib::Printf( "%s.Sprite%i script:\n%s\n", filename, characterID, actionScript.c_str() ); + for( int i = stack.Num() - 1; i >= 0 ; i-- ) { - idLib::Printf( " %c: %s (%s)\n", ( char )( 64 + stack.Num() - i ), stack[i].ToString().c_str(), stack[i].TypeOf() ); + idLib::Printf( "%s.Sprite%i %c: %s (%s)\n", filename, characterID, ( char )( 64 + stack.Num() - i ), stack[i].ToString().c_str(), stack[i].TypeOf() ); } #if 0 @@ -1770,45 +1838,33 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, #endif } - idLib::Printf( "SWF%d: code %s\n", callstackLevel, GetSwfActionName( code ) ); + idLib::Printf( "%s.Sprite%i SWF%d: code %s\n", filename, characterID, callstackLevel, GetSwfActionName( code ) ); } switch( code ) { case Action_Return: callstackLevel--; - actionScript += "return " + stack.A().ToString() + "\n"; - return actionScript; + AddLine( "return " + stack.A().ToString() ); + goto finish; case Action_End: + + QuitCurrentBlock(); + //actionScript += indent + "end\n"; + callstackLevel--; - return actionScript; + goto finish; case Action_NextFrame: - //if( verify( currentTarget != NULL ) ) - //{ - // currentTarget->NextFrame(); - //} - actionScript += "nextFrame();\n"; + AddLine( "nextFrame()" ); break; case Action_PrevFrame: - //if( verify( currentTarget != NULL ) ) - //{ - // currentTarget->PrevFrame(); - //} - actionScript += "prevFrame();\n"; + AddLine( "prevFrame()" ); break; case Action_Play: - //if( verify( currentTarget != NULL ) ) - //{ - // currentTarget->Play(); - //} - actionScript += "play();\n"; + AddLine( "play()" ); break; case Action_Stop: - //if( verify( currentTarget != NULL ) ) - //{ - // currentTarget->Stop(); - //} - actionScript += "stop();\n"; + AddLine( "stop()" ); break; case Action_ToggleQuality: break; @@ -1819,33 +1875,19 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, assert( recordLength == 2 ); int frameNum = bitstream.ReadU16() + 1; - actionScript += va( "gotoAndPlay( %i );\n", frameNum ); + AddLine( va( "gotoAndPlay( %i )", frameNum ) ); break; } case Action_SetTarget: { const char* targetName = ( const char* )bitstream.ReadData( recordLength ); - if( verify( thisSprite != NULL ) ) - { - currentTarget = thisSprite->ResolveTarget( targetName ); - } - else if( swf_debug.GetInteger() > 0 ) - { - idLib::Printf( "SWF: no target movie clip for setTarget %s\n", targetName ); - } + AddLine( va( "setTarget( \"%s\" )", targetName ) ); break; } case Action_GoToLabel: { const char* targetName = ( const char* )bitstream.ReadData( recordLength ); - if( verify( currentTarget != NULL ) ) - { - currentTarget->RunTo( currentTarget->FindFrame( targetName ) ); - } - else if( swf_debug.GetInteger() > 0 ) - { - idLib::Printf( "SWF: no target movie clip for runTo %s\n", targetName ); - } + AddLine( va( "gotoLabel( \"%s\" )", targetName ) ); break; } case Action_Push: @@ -1891,75 +1933,83 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, break; } case Action_Pop: - - if( stack.A().IsString() ) + { + if( stack.A().IsString() || stack.A().IsResult() ) { - actionScript += stack.A().ToString() + ";\n"; + AddLine( stack.A().ToString() ); //line.Empty(); } + if( stack.A().IsNumeric() ) + { + QuitCurrentBlock(); + + //*block += indent + "end\n"; + } + stack.Pop( 1 ); break; + } case Action_Add: //stack.B().SetFloat( stack.B().ToFloat() + stack.A().ToFloat() ); - stack.B().SetString( va( "%s + %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s + %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Subtract: //stack.B().SetFloat( stack.B().ToFloat() - stack.A().ToFloat() ); - stack.B().SetString( va( "%s - %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s - %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Multiply: //stack.B().SetFloat( stack.B().ToFloat() * stack.A().ToFloat() ); - stack.B().SetString( va( "%s * %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s * %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Divide: //stack.B().SetFloat( stack.B().ToFloat() / stack.A().ToFloat() ); - stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Equals: //stack.B().SetBool( stack.B().ToFloat() == stack.A().ToFloat() ); - stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Less: //stack.B().SetBool( stack.B().ToFloat() < stack.A().ToFloat() ); - stack.B().SetString( va( "%s < %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s < %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_And: //stack.B().SetBool( stack.B().ToBool() && stack.A().ToBool() ); - stack.B().SetString( va( "%s && %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s && %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Or: //stack.B().SetBool( stack.B().ToBool() || stack.A().ToBool() ); - stack.B().SetString( va( "%s || %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s || %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_Not: //stack.A().SetBool( !stack.A().ToBool() ); - stack.A().SetString( va( "!( %s )", stack.A().ToString().c_str() ) ); + stack.A().SetResult( va( "!( %s )", stack.A().ToString().c_str() ) ); break; case Action_StringEquals: //stack.B().SetBool( stack.B().ToString() == stack.A().ToString() ); - stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; case Action_StringLength: //stack.A().SetInteger( stack.A().ToString().Length() ); - stack.A().SetString( va( "%s:len()", stack.A().ToString().c_str() ) ); + stack.A().SetResult( va( "%s:len()", stack.A().ToString().c_str() ) ); break; case Action_StringAdd: //stack.B().SetString( stack.B().ToString() + stack.A().ToString() ); - stack.B().SetString( stack.B().ToString() + stack.A().ToString() ); + stack.B().SetResult( stack.B().ToString() + stack.A().ToString() ); stack.Pop( 1 ); break; case Action_StringExtract: - stack.C().SetString( stack.C().ToString().Mid( stack.B().ToInteger(), stack.A().ToInteger() ) ); + stack.C().SetResult( stack.C().ToString().Mid( stack.B().ToInteger(), stack.A().ToInteger() ) ); stack.Pop( 2 ); break; case Action_StringLess: @@ -1980,20 +2030,22 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, stack.A().SetString( va( "%c", stack.A().ToInteger() ) ); break; case Action_Jump: - bitstream.Seek( bitstream.ReadS16() ); + // RB: do not jump and continue translating to Lua or ActionScript + bitstream.ReadS16(); - actionScript += "\n--FIXME Action_Jump not implemented"; - return actionScript; + //bitstream.Seek( bitstream.ReadS16() ); + + QuitCurrentBlock(); break; case Action_If: { - actionScript += va( "if( %s )\n{\n", stack.A().ToString().c_str() ) ; + AddBlock( va( "if( %s ) then\n", stack.A().ToString().c_str() ) ); int16 offset = bitstream.ReadS16(); - if( stack.A().ToBool() ) - { - bitstream.Seek( offset ); - } + //if( stack.A().ToBool() ) + //{ + // bitstream.Seek( offset ); + //} stack.Pop( 1 ); break; } @@ -2026,6 +2078,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, case Action_SetVariable: { idStr variableName = stack.B().ToString(); + /* bool found = false; for( int i = scope.Num() - 1; i >= 0; i-- ) { @@ -2040,6 +2093,17 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, { thisObject->Set( variableName, stack.A() ); } + */ + if( stack.A().IsString() ) + { + AddLine( va( "%s = \"%s\"\n", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + + } + else + { + AddLine( va( "%s = %s\n", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + stack.Pop( 2 ); break; } @@ -2053,6 +2117,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, frameNum += bitstream.ReadU16(); } + /* if( verify( thisSprite != NULL ) ) { if( stack.A().IsString() ) @@ -2084,6 +2149,18 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, idLib::Printf( "SWF: no target movie clip for gotoAndStop\n" ); } } + */ + + if( ( flags & 1 ) != 0 ) + { + AddLine( va( "gotoAndPlay( %i )", frameNum ) ); + } + else + { + AddLine( va( "gotoAndStop( %i )", frameNum ) ); + } + AddLine( va( "gotoAndPlay( %i )", frameNum ) ); + stack.Pop( 1 ); break; } @@ -2116,7 +2193,14 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, break; } case Action_Trace: - idLib::PrintfIf( swf_debug.GetInteger() > 0, "SWF Trace: %s\n", stack.A().ToString().c_str() ); + if( stack.A().IsString() ) + { + AddLine( va( "trace( \"%s\" )\n", stack.A().ToString().c_str() ) ); + } + else + { + AddLine( va( "trace( %s )\n", stack.A().ToString().c_str() ) ); + } stack.Pop( 1 ); break; case Action_GetTime: @@ -2173,7 +2257,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, { idLib::PrintfIf( swf_debug.GetInteger() > 0, "SWF: unknown function %s\n", functionName.c_str() ); - stack.Alloc().SetString( line ); + stack.Alloc().SetResult( line ); } break; @@ -2233,7 +2317,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, } else { - stack.Alloc().SetString( line ); + stack.Alloc().SetResult( line ); } //line.Empty(); @@ -2258,15 +2342,46 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, newFunction->SetConstants( constants ); newFunction->SetDefaultSprite( defaultSprite ); + idStr line; + if( functionName.IsEmpty() ) + { + line = "function( "; + } + else + { + line = va( "function %( ", functionName.c_str() ); + } + uint16 numParms = bitstream.ReadU16(); newFunction->AllocParameters( numParms ); for( int i = 0; i < numParms; i++ ) { - newFunction->SetParameter( i, 0, bitstream.ReadString() ); + const char* parm = bitstream.ReadString(); + + if( stack.A().IsString() ) + { + line += va( "\"%s\"%s", parm, ( i < ( numParms - 1 ) ) ? ", " : " " ); + } + else + { + line += va( "%s%s", parm, ( i < ( numParms - 1 ) ) ? ", " : " " ); + } + + newFunction->SetParameter( i, 0, parm ); } + + line += ")"; + uint16 codeSize = bitstream.ReadU16(); newFunction->SetData( bitstream.ReadData( codeSize ), codeSize ); + if( functionName.IsEmpty() ) + { + stack.Alloc().SetResult( line ); + } + AddBlock( line ); + + /* if( functionName.IsEmpty() ) { stack.Alloc().SetFunction( newFunction ); @@ -2275,6 +2390,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, { thisObject->Set( functionName, idSWFScriptVar( newFunction ) ); } + */ newFunction->Release(); break; } @@ -2302,6 +2418,16 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, newFunction->AllocRegisters( numRegs ); newFunction->SetFlags( flags ); + idStr line; + if( functionName.IsEmpty() ) + { + line = "function( "; + } + else + { + line = va( "function %( ", functionName.c_str() ); + } + for( int i = 0; i < numParms; i++ ) { uint8 reg = bitstream.ReadU8(); @@ -2312,11 +2438,29 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, reg = 0; } newFunction->SetParameter( i, reg, name ); + + if( stack.A().IsString() ) + { + line += va( "\"%s\"%s", name, ( i < ( numParms - 1 ) ) ? ", " : " " ); + } + else + { + line += va( "%s%s", name, ( i < ( numParms - 1 ) ) ? ", " : " " ); + } } + line += ")"; + + if( functionName.IsEmpty() ) + { + stack.Alloc().SetResult( line ); + } + AddBlock( line ); + uint16 codeSize = bitstream.ReadU16(); newFunction->SetData( bitstream.ReadData( codeSize ), codeSize ); + /* if( functionName.IsEmpty() ) { stack.Alloc().SetFunction( newFunction ); @@ -2325,6 +2469,8 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, { thisObject->Set( functionName, idSWFScriptVar( newFunction ) ); } + */ + newFunction->Release(); break; } @@ -2378,7 +2524,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, case Action_Equals2: { //stack.B().SetBool( stack.A().AbstractEquals( stack.B() ) ); - stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; @@ -2386,7 +2532,7 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, case Action_StrictEquals: { //stack.B().SetBool( stack.A().StrictEquals( stack.B() ) ); - stack.B().SetString( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + stack.B().SetResult( va( "%s == %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); stack.Pop( 1 ); break; @@ -2471,7 +2617,20 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, //actionScript += line; //line.Empty(); - stack.B().SetString( va( "%s.%s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + idStr& member = stack.A().ToString(); + //if( stack.A().IsString() ) + //{ + // stack.B().SetResult( va( "%s[\"%s\"]", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + //} + //else + if( member.Find( ' ' ) > 0 || member.Find( '\"' ) > 0 ) + { + stack.B().SetResult( va( "%s[%s]", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + else + { + stack.B().SetResult( va( "%s.%s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } stack.Pop( 1 ); break; @@ -2489,7 +2648,15 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, //} //else - actionScript += va( "%s.%s = %s;\n", stack.C().ToString().c_str(), stack.B().ToString().c_str(), stack.A().ToString().c_str() ); + if( stack.A().IsString() ) + { + AddLine( va( "%s.%s = \"%s\"\n", stack.C().ToString().c_str(), stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + + } + else + { + AddLine( va( "%s.%s = %s\n", stack.C().ToString().c_str(), stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } } stack.Pop( 3 ); break; @@ -2652,49 +2819,75 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, break; } case Action_ToNumber: - stack.A().SetFloat( stack.A().ToFloat() ); - break; - case Action_ToString: - stack.A().SetString( stack.A().ToString() ); - break; - case Action_TypeOf: - stack.A().SetString( stack.A().TypeOf() ); - break; - case Action_Add2: - { - if( stack.A().IsString() || stack.B().IsString() ) + if( stack.A().IsString() ) { - stack.B().SetString( stack.B().ToString() + stack.A().ToString() ); + stack.A().SetResult( va( "tonumber( \"%s\" )", stack.A().ToString().c_str() ) ); } else { - stack.B().SetFloat( stack.B().ToFloat() + stack.A().ToFloat() ); + stack.A().SetResult( va( "tonumber( %s )", stack.A().ToString().c_str() ) ); + } + break; + case Action_ToString: + stack.A().SetResult( va( "tostring( %s )", stack.A().ToString().c_str() ) ); + break; + case Action_TypeOf: + if( stack.A().IsString() ) + { + stack.A().SetResult( va( "type( \"%s\" )", stack.A().ToString().c_str() ) ); + } + else + { + stack.A().SetResult( va( "type( %s )", stack.A().ToString().c_str() ) ); + } + break; + case Action_Add2: + { + if( stack.B().IsString() && stack.A().IsString() ) + { + stack.B().SetResult( va( "\"%s\" .. \"%s\"", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + else if( stack.B().IsString() && !stack.A().IsString() ) + { + stack.B().SetResult( va( "\"%s\" + %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + else if( !stack.B().IsString() && stack.A().IsString() ) + { + stack.B().SetResult( va( "%s + \"%s\"", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + else + { + stack.B().SetResult( va( "%s + %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); } stack.Pop( 1 ); break; } case Action_Less2: { + /* if( stack.A().IsString() && stack.B().IsString() ) { - stack.B().SetBool( stack.B().ToString() < stack.A().ToString() ); + stack.B().SetString( va( "%s < %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); } else + */ { - stack.B().SetBool( stack.B().ToFloat() < stack.A().ToFloat() ); + stack.B().SetResult( va( "%s < %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); } stack.Pop( 1 ); break; } case Action_Greater: { + /* if( stack.A().IsString() && stack.B().IsString() ) { stack.B().SetBool( stack.B().ToString() > stack.A().ToString() ); } else + */ { - stack.B().SetBool( stack.B().ToFloat() > stack.A().ToFloat() ); + stack.B().SetResult( va( "%s > %s", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); } stack.Pop( 1 ); break; @@ -2739,11 +2932,21 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, stack.Pop( 1 ); break; case Action_Decrement: - stack.A().SetFloat( stack.A().ToFloat() - 1.0f ); + { + //stack.A().SetFloat( stack.A().ToFloat() - 1.0f ); + + const char* a = stack.A().ToString().c_str(); + stack.A().SetResult( va( "%s - 1", a ) ); break; + } case Action_Increment: - stack.A().SetFloat( stack.A().ToFloat() + 1.0f ); + { + //stack.A().SetFloat( stack.A().ToFloat() + 1.0f ); + + const char* a = stack.A().ToString().c_str(); + stack.A().SetResult( va( "%s + 1", a ) ); break; + } case Action_PushDuplicate: { idSWFScriptVar dup = stack.A(); @@ -2766,12 +2969,25 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, case Action_DefineLocal: { scope[scope.Num() - 1]->Set( stack.B().ToString(), stack.A() ); + + if( stack.A().IsString() ) + { + AddLine( va( "local %s = \"%s\"\n", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + else + { + AddLine( va( "local %s = %s\n", stack.B().ToString().c_str(), stack.A().ToString().c_str() ) ); + } + stack.Pop( 2 ); break; } case Action_DefineLocal2: { scope[scope.Num() - 1]->Set( stack.A().ToString(), idSWFScriptVar() ); + + AddLine( va( "local %s = {}\n", stack.A().ToString().c_str() ) ); + stack.Pop( 1 ); break; } @@ -2806,12 +3022,19 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, // We have to abort here because the rest of the script is basically meaningless now assert( false ); callstackLevel--; - return actionScript; + goto finish; } } callstackLevel--; + +finish: + idStr actionScript = BuildActionCode( actionBlocks, 0 ); + + idLib::Printf( "%s.Sprite%i script:\n%s\n", filename, characterID, actionScript.c_str() ); + return actionScript; } +// RB end /* ======================== diff --git a/neo/swf/SWF_ScriptFunction.h b/neo/swf/SWF_ScriptFunction.h index ca64d8b5..93a1fb01 100644 --- a/neo/swf/SWF_ScriptFunction.h +++ b/neo/swf/SWF_ScriptFunction.h @@ -253,11 +253,30 @@ public: virtual idSWFScriptVar Call( idSWFScriptObject* thisObject, const idSWFParmList& parms ); // RB begin - idStr CallToScript( idSWFScriptObject* thisObject, const idSWFParmList& parms ); + idStr CallToScript( idSWFScriptObject* thisObject, const idSWFParmList& parms, const char* filename, int characterID, int actionID ); private: idSWFScriptVar Run( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream ); - idStr ExportToScript( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream ); + + + + struct ActionBlock + { + ActionBlock* parent = NULL; + idStr line; + idList blocks; + }; + idList actionBlocks; + ActionBlock* currentBlock; + + idStr UpdateIndent( int indentLevel ) const; + void AddLine( const idStr& line ); + void AddBlock( const idStr& line ); + void QuitCurrentBlock(); + + idStr BuildActionCode( const idList& blocks, int level ); + + idStr ExportToScript( idSWFScriptObject* thisObject, idSWFStack& stack, idSWFBitStream& bitstream, const char* filename, int characterID, int actionID ); // RB end private: diff --git a/neo/swf/SWF_ScriptVar.cpp b/neo/swf/SWF_ScriptVar.cpp index 6450bfc6..4264da0a 100644 --- a/neo/swf/SWF_ScriptVar.cpp +++ b/neo/swf/SWF_ScriptVar.cpp @@ -292,7 +292,11 @@ idStr idSWFScriptVar::ToString() const return idStrId( value.i ).GetLocalizedString(); case SWF_VAR_STRING: return *value.string; - + // RB begin + case SWF_VAR_RESULT: + return *value.string; + // RB end + case SWF_VAR_FLOAT: return va( "%g", value.f ); case SWF_VAR_BOOL: @@ -521,7 +525,11 @@ const char* idSWFScriptVar::TypeOf() const return "stringid"; case SWF_VAR_STRING: return "string"; - + // RB begin + case SWF_VAR_RESULT: + return "result"; + // RB end + case SWF_VAR_FLOAT: return "number"; case SWF_VAR_BOOL: diff --git a/neo/swf/SWF_ScriptVar.h b/neo/swf/SWF_ScriptVar.h index 61a56b2f..e8b8fc19 100644 --- a/neo/swf/SWF_ScriptVar.h +++ b/neo/swf/SWF_ScriptVar.h @@ -138,6 +138,22 @@ public: value.string = s; s->AddRef(); } + + // RB begin + void SetResult( const idStr& s ) + { + Free(); + type = SWF_VAR_RESULT; + value.string = idSWFScriptString::Alloc( s ); + } + void SetResult( const char* s ) + { + Free(); + type = SWF_VAR_RESULT; + value.string = idSWFScriptString::Alloc( s ); + } + // RB end + void SetFloat( float f ) { Free(); @@ -232,6 +248,11 @@ public: return ( type == SWF_VAR_FLOAT ) || ( type == SWF_VAR_INTEGER ) || ( type == SWF_VAR_BOOL ); } + bool IsResult() const + { + return ( type == SWF_VAR_RESULT ); + } + enum swfScriptVarType { SWF_VAR_STRINGID, @@ -242,7 +263,8 @@ public: SWF_VAR_BOOL, SWF_VAR_INTEGER, SWF_VAR_FUNCTION, - SWF_VAR_OBJECT + SWF_VAR_OBJECT, + SWF_VAR_RESULT // RB: for P-Code to Lua }; swfScriptVarType GetType() const diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 837de719..552e5524 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -296,7 +296,7 @@ void idSWFSprite::Write( idFile* f ) idSWFSprite::WriteXML ======================== */ -void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID ) +void idSWFSprite::WriteXML( idFile* f, int characterID, const char* indentPrefix ) { if( characterID >= 0 ) { @@ -344,7 +344,7 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID // WriteXML_PlaceObject2( command.stream, indentPrefix ); // break; -#define HANDLE_SWF_TAG( x ) case Tag_##x: WriteXML_##x( f, command.stream, indentPrefix ); break; +#define HANDLE_SWF_TAG( x ) case Tag_##x: WriteXML_##x( f, command.stream, characterID, i, indentPrefix ); break; HANDLE_SWF_TAG( PlaceObject2 ); HANDLE_SWF_TAG( PlaceObject3 ); HANDLE_SWF_TAG( RemoveObject2 ); @@ -375,7 +375,7 @@ void idSWFSprite::WriteXML( idFile* f, const char* indentPrefix, int characterID // RB end ======= -void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { uint64 flags = bitstream.ReadU8(); int depth = bitstream.ReadU16(); @@ -454,7 +454,7 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream } -void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { uint64 flags1 = bitstream.ReadU8(); uint64 flags2 = bitstream.ReadU8(); @@ -548,14 +548,14 @@ void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream file->WriteFloatString( "%s\t\n", indentPrefix ); } -void idSWFSprite::WriteXML_RemoveObject2( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +void idSWFSprite::WriteXML_RemoveObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { int depth = bitstream.ReadU16(); file->WriteFloatString( "%s\t\n", indentPrefix, depth ); } -void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, const char* indentPrefix ) +void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix ) { idBase64 base64; @@ -574,11 +574,11 @@ void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, co // actionScript->SetDefaultSprite( this ); actionScript->SetData( bitstream.Ptr(), bitstream.Length() ); - idStr scriptText = actionScript->CallToScript( scriptObject, idSWFParmList() ); + idStr scriptText = actionScript->CallToScript( scriptObject, idSWFParmList(), file->GetName(), characterID, commandID ); //file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); - file->WriteFloatString( "%s\t\n%s\t\t%s\n%s\t\n", indentPrefix, bitstream.Length(), indentPrefix, scriptText.c_str(), indentPrefix ); + file->WriteFloatString( "%s\t\n%s%s\t\n", indentPrefix, bitstream.Length(), scriptText.c_str(), indentPrefix ); delete actionScript; delete scriptObject; diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index c63af7db..5a01494f 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -48,13 +48,13 @@ public: void Write( idFile* f ); // RB begin - void WriteXML( idFile* f, const char* indentPrefix = "", int characterID = -1 ); - void WriteXML_PlaceObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); - void WriteXML_PlaceObject3( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); - void WriteXML_RemoveObject2( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); - void WriteXML_DoAction( idFile* f, idSWFBitStream& bitstream, const char* indentPrefix = "" ); + void WriteXML( idFile* f, int characterID, const char* indentPrefix = "" ); + void WriteXML_PlaceObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteXML_PlaceObject3( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteXML_RemoveObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteXML_DoAction( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); - void WriteSWF( idFile_SWF& f, int characterID = -1 ); + void WriteSWF( idFile_SWF& f, int characterID ); uint16 GetFrameCount() { From aa3f3aef71c11e669d7208c1926afd0b10fbc82b Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sat, 6 Jun 2015 23:28:51 +0200 Subject: [PATCH 22/43] JSON Flash export 1 --- neo/swf/SWF.h | 5 ++ neo/swf/SWF_Load.cpp | 145 +++++++++++++++++++-------------- neo/swf/SWF_Main.cpp | 10 +-- neo/swf/SWF_ScriptFunction.cpp | 34 -------- neo/swf/SWF_Sprites.cpp | 2 +- 5 files changed, 95 insertions(+), 101 deletions(-) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index b953f836..ced22606 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -405,9 +405,14 @@ private: // RB begin ======= +<<<<<<< HEAD >>>>>>> 534d415... SWF export void LoadXML( const char* filename ); void WriteXML( const char* filename ); +======= + void LoadJSON( const char* filename ); + void WriteJSON( const char* filename ); +>>>>>>> 24a45e0... JSON Flash export 1 // RB end >>>>>>> c4098bc... XML Flash part 1 diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 7f6c2c2c..8d0a367a 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -826,10 +826,10 @@ void idSWF::SetBackgroundColor( idSWFBitStream& bitstream ) /* =================== -idSWF::WriteXML +idSWF::WriteJSON =================== */ -void idSWF::WriteXML( const char* filename ) +void idSWF::WriteJSON( const char* filename ) { const bool exportBitmapShapesOnly = false; @@ -839,60 +839,67 @@ void idSWF::WriteXML( const char* filename ) return; } - file->WriteFloatString( "\n" ); - file->WriteFloatString( "\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); + //file->WriteFloatString( "\n" ); + file->WriteFloatString( "{\n \t\"version\": %i,\n \t\"timestamp\": %i,\n \t\"frameWidth\": %f,\n \t\"frameHeight\": %f,\n \t\"frameRate\": %i,\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); - file->WriteFloatString( "\t\n" ); + file->WriteFloatString( "\t\"dict\":\n\t[\n" ); for( int i = 0; i < dictionary.Num(); i++ ) { const idSWFDictionaryEntry& entry = dictionary[i]; - //file->WriteFloatString( "\t\n", idSWF::GetDictTypeName( dictionary[i].type ) ); + if( dictionary[i].type != SWF_DICT_NULL ) + { + file->WriteFloatString( "\t\t{\n" ); + + file->WriteFloatString( "\t\t\t\"type\": \"%s\",\n", idSWF::GetDictTypeName( dictionary[i].type ) ); + file->WriteFloatString( "\t\t\t\"characterID\": %i,\n", i ); + } + switch( dictionary[i].type ) { case SWF_DICT_IMAGE: { - file->WriteFloatString( "\t\tWriteFloatString( "%s\"", dictionary[i].material->GetName() ); + file->WriteFloatString( "\t\t\t\"material\": \"%s\"", dictionary[i].material->GetName() ); } else { - file->WriteFloatString( ".\"" ); + idStr filenameWithoutExt = filename; + filenameWithoutExt.StripFileExtension(); + + file->WriteFloatString( "\t\t\t\"imagefile\": \"exported/%s/image_characterid_%i.png\",\n", filenameWithoutExt.c_str(), i ); } - file->WriteFloatString( " width=\"%i\" height=\"%i\" atlasOffsetX=\"%i\" atlasOffsetY=\"%i\">\n", + file->WriteFloatString( "\t\t\t\"width\": %i, \"height\": %i, \"atlasOffsetX\": %i, \"atlasOffsetY\": %i,\n", entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); - file->WriteFloatString( "\t\t\t\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); - - file->WriteFloatString( "\t\t\n" ); + file->WriteFloatString( "\t\t\t\"channelScale\": { \"x\": %f, \"y\": %f, \"z\": %f, \"w\": %f }\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); break; } +#if 1 case SWF_DICT_MORPH: case SWF_DICT_SHAPE: { idSWFShape* shape = dictionary[i].shape; - file->WriteFloatString( "\t\t\n", i ); - float x = shape->startBounds.tl.y; float y = shape->startBounds.tl.x; float width = fabs( shape->startBounds.br.y - shape->startBounds.tl.y ); float height = fabs( shape->startBounds.br.x - shape->startBounds.tl.x ); - file->WriteFloatString( "\t\t\t\n", x, y, width, height ); + file->WriteFloatString( "\t\t\t\"startBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); x = shape->endBounds.tl.y; y = shape->endBounds.tl.x; width = fabs( shape->endBounds.br.y - shape->endBounds.tl.y ); height = fabs( shape->endBounds.br.x - shape->endBounds.tl.x ); - file->WriteFloatString( "\t\t\t\n", x, y, width, height ); + file->WriteFloatString( "\t\t\t\"endBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); // export fill draws + file->WriteFloatString( "\t\t\t\"fillDraws\":\n\t\t\t[\n" ); for( int d = 0; d < shape->fillDraws.Num(); d++ ) { @@ -903,9 +910,9 @@ void idSWF::WriteXML( const char* filename ) continue; } - file->WriteFloatString( "\t\t\t\n" ); + file->WriteFloatString( "\t\t\t\t{\n" ); - file->WriteFloatString( "\t\t\t\tWriteFloatString( " subType=" ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"subType\": " ); if( fillDraw.style.subType == 0 ) { file->WriteFloatString( "\"linear\"" ); @@ -950,89 +957,101 @@ void idSWF::WriteXML( const char* filename ) if( fillDraw.style.type == 1 && fillDraw.style.subType == 3 ) { - file->WriteFloatString( " focalPoint=\"%f\"", fillDraw.style.focalPoint ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"focalPoint\": %f", fillDraw.style.focalPoint ); } if( fillDraw.style.type == 4 ) { - file->WriteFloatString( " bitmapID=\"%i\"", fillDraw.style.bitmapID ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"bitmapID\": %i", fillDraw.style.bitmapID ); } - file->WriteFloatString( ">\n" ); - if( fillDraw.style.type == 0 ) { idVec4 color = fillDraw.style.startColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); - + file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); + color = fillDraw.style.endColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ],\n", color.x, color.y, color.z, color.w ); } if( fillDraw.style.type > 0 ) { swfMatrix_t m = fillDraw.style.startMatrix; - file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", - m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); - + file->WriteFloatString( ",\n\t\t\t\t\t\t\"startMatrix\": [ %f, %f, %f, %f, %f, %f ]", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + if( fillDraw.style.startMatrix != fillDraw.style.endMatrix ) { m = fillDraw.style.endMatrix; - file->WriteFloatString( "\t\t\t\t\t%f %f %f %f %f %f\n", - m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endMatrix\": [ %f, %f, %f, %f, %f, %f ],\n", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); } } - for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + if( fillDraw.style.gradient.numGradients ) { - swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + file->WriteFloatString( "\t\t\t\t\t\"gradients\":\n\t\t\t\t\t[\n" ); - file->WriteFloatString( "\t\t\t\t\t\n", gr.startRatio, gr.endRatio ); - - idVec4 color = gr.startColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); - - idVec4 endColor = gr.endColor.ToVec4(); - if( color != endColor ) + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) { - file->WriteFloatString( "\t\t\t\t\t\t\n", - color.x, color.y, color.z, endColor.w ); + swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + + file->WriteFloatString( "\t\t\t\t\t\n", gr.startRatio, gr.endRatio ); + + idVec4 color = gr.startColor.ToVec4(); + file->WriteFloatString( "\t\t\t\t\t\t\n", + color.x, color.y, color.z, color.w ); + + idVec4 endColor = gr.endColor.ToVec4(); + if( color != endColor ) + { + file->WriteFloatString( "\t\t\t\t\t\t\n", + color.x, color.y, color.z, endColor.w ); + } + + file->WriteFloatString( "\t\t\t\t\t\t}%s\n", ( g == ( fillDraw.style.gradient.numGradients - 1 ) ) ? "" : "," ); } + + file->WriteFloatString( "\t\t\t\t\t\t],\n" ); } - file->WriteFloatString( "\t\t\t\t\n" ); + file->WriteFloatString( "\t\t\t\t\t},\n" ); + file->WriteFloatString( "\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) { const idVec2& vert = fillDraw.startVerts[v]; - file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.startVerts.Num() - 1 ) ) ? "" : "," ); } + file->WriteFloatString( "\t\t\t\t\t],\n" ); - for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + if( fillDraw.endVerts.Num() ) { - const idVec2& vert = fillDraw.endVerts[v]; - - file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); + file->WriteFloatString( "\t\t\t\t\t\"endVerts\":\n\t\t\t\t\t[\n" ); + for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + { + const idVec2& vert = fillDraw.endVerts[v]; + + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.endVerts.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t\t],\n" ); } - file->WriteFloatString( "\t\t\t\t", fillDraw.indices.Num() ); + file->WriteFloatString( "\t\t\t\t\t\"indices\": [ " ); for( int v = 0; v < fillDraw.indices.Num(); v++ ) { const uint16& vert = fillDraw.indices[v]; - file->WriteFloatString( "%i ", vert ); + file->WriteFloatString( "%i%s", vert, ( v == fillDraw.indices.Num() - 1 ) ? "" : ", " ); } - file->WriteFloatString( "\n" ); + file->WriteFloatString( "]\n" ); - file->WriteFloatString( "\t\t\t\n" ); + file->WriteFloatString( "\t\t\t\t}%s\n", ( d == ( shape->fillDraws.Num() - 1 ) ) ? "" : "," ); } + file->WriteFloatString( "\t\t\t]\n" ); + // export line draws -#if 1 +#if 0 for( int d = 0; d < shape->lineDraws.Num(); d++ ) { const idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; @@ -1078,10 +1097,9 @@ void idSWF::WriteXML( const char* filename ) file->WriteFloatString( "\n" ); } #endif - - file->WriteFloatString( "\t\t\n" ); break; } +#endif case SWF_DICT_SPRITE: { @@ -1230,12 +1248,17 @@ void idSWF::WriteXML( const char* filename ) } } - //file->WriteFloatString( "\t\n" ); + if( dictionary[i].type != SWF_DICT_NULL ) + { + file->WriteFloatString( "\t\t}%s\n", ( i == ( dictionary.Num() - 1 ) ) ? "" : "," ); + } } - file->WriteFloatString( "\t\n" ); + file->WriteFloatString( "\t],\n" ); + file->WriteFloatString( "\t\"mainsprite\": {\n" ); mainsprite->WriteXML( file, dictionary.Num(), "\t" ); + file->WriteFloatString( "}\n" ); file->WriteFloatString( "\n" ); } diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 74a38841..7a50c51a 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -156,11 +156,11 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) // RB begin if( swf_exportXML.GetBool() ) { - idStr xmlFileName = "exported/"; - xmlFileName += filename; - xmlFileName.SetFileExtension( ".xml" ); + idStr jsonFileName = "exported/"; + jsonFileName += filename; + jsonFileName.SetFileExtension( ".json" ); - WriteXML( xmlFileName ); + WriteJSON( jsonFileName ); } idStr atlasFileName = binaryFileName; diff --git a/neo/swf/SWF_ScriptFunction.cpp b/neo/swf/SWF_ScriptFunction.cpp index 1a50df64..13f83b74 100644 --- a/neo/swf/SWF_ScriptFunction.cpp +++ b/neo/swf/SWF_ScriptFunction.cpp @@ -2117,40 +2117,6 @@ idStr idSWFScriptFunction_Script::ExportToScript( idSWFScriptObject* thisObject, frameNum += bitstream.ReadU16(); } - /* - if( verify( thisSprite != NULL ) ) - { - if( stack.A().IsString() ) - { - frameNum += thisSprite->FindFrame( stack.A().ToString() ); - } - else - { - frameNum += ( uint32 )stack.A().ToInteger(); - } - if( ( flags & 1 ) != 0 ) - { - thisSprite->Play(); - } - else - { - thisSprite->Stop(); - } - thisSprite->RunTo( frameNum ); - } - else if( swf_debug.GetInteger() > 0 ) - { - if( ( flags & 1 ) != 0 ) - { - idLib::Printf( "SWF: no target movie clip for gotoAndPlay\n" ); - } - else - { - idLib::Printf( "SWF: no target movie clip for gotoAndStop\n" ); - } - } - */ - if( ( flags & 1 ) != 0 ) { AddLine( va( "gotoAndPlay( %i )", frameNum ) ); diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 552e5524..555d5301 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -321,7 +321,7 @@ void idSWFSprite::WriteXML( idFile* f, int characterID, const char* indentPrefix } -#if 1 +#if 0 idBase64 base64; for( int i = 0; i < commands.Num(); i++ ) From 30e5e52f71d5e7e32d4166d84dea799785a328e3 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 7 Jun 2015 01:07:13 +0200 Subject: [PATCH 23/43] JSON Flash export 2 --- neo/swf/SWF_Load.cpp | 14 +++--- neo/swf/SWF_Sprites.cpp | 105 +++++++++++++++++++++------------------- neo/swf/SWF_Sprites.h | 10 ++-- 3 files changed, 66 insertions(+), 63 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 8d0a367a..4e4d6f5d 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -868,7 +868,7 @@ void idSWF::WriteJSON( const char* filename ) idStr filenameWithoutExt = filename; filenameWithoutExt.StripFileExtension(); - file->WriteFloatString( "\t\t\t\"imagefile\": \"exported/%s/image_characterid_%i.png\",\n", filenameWithoutExt.c_str(), i ); + file->WriteFloatString( "\t\t\t\"imagefile\": \"%s/image_characterid_%i.png\",\n", filenameWithoutExt.c_str(), i ); } file->WriteFloatString( "\t\t\t\"width\": %i, \"height\": %i, \"atlasOffsetX\": %i, \"atlasOffsetY\": %i,\n", @@ -1013,7 +1013,7 @@ void idSWF::WriteJSON( const char* filename ) file->WriteFloatString( "\t\t\t\t\t\t],\n" ); } - file->WriteFloatString( "\t\t\t\t\t},\n" ); + file->WriteFloatString( "\n\t\t\t\t\t},\n" ); file->WriteFloatString( "\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) @@ -1103,7 +1103,7 @@ void idSWF::WriteJSON( const char* filename ) case SWF_DICT_SPRITE: { - dictionary[i].sprite->WriteXML( file, i, "\t\t" ); + dictionary[i].sprite->WriteJSON( file, i ); break; } @@ -1256,11 +1256,9 @@ void idSWF::WriteJSON( const char* filename ) file->WriteFloatString( "\t],\n" ); - file->WriteFloatString( "\t\"mainsprite\": {\n" ); - mainsprite->WriteXML( file, dictionary.Num(), "\t" ); - file->WriteFloatString( "}\n" ); - - file->WriteFloatString( "\n" ); + file->WriteFloatString( "\t\"mainsprite\":\n\t{\n" ); + mainsprite->WriteJSON( file, dictionary.Num() ); + file->WriteFloatString( "\t}\n}\n" ); } // RB end diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 555d5301..2b02296a 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -291,39 +291,38 @@ void idSWFSprite::Write( idFile* f ) // RB begin -/* -======================== -idSWFSprite::WriteXML -======================== -*/ -void idSWFSprite::WriteXML( idFile* f, int characterID, const char* indentPrefix ) +void idSWFSprite::WriteJSON( idFile* f, int characterID ) { - if( characterID >= 0 ) - { - f->WriteFloatString( "%s\n", indentPrefix, characterID, frameCount, frameOffsets.Num() ); - } - else - { - f->WriteFloatString( "%s\n", indentPrefix, frameCount, frameOffsets.Num() ); - } + f->WriteFloatString( "\t\t\t\"frameCount\": %i,\n", frameCount ); - f->WriteFloatString( "%s\t", indentPrefix ); - for( int i = 0; i < frameOffsets.Num(); i++ ) + //f->WriteFloatString( "\t\t\t\"frameOffsets\": %i,\n", frameOffsets.Num() ); + + if( frameOffsets.Num() ) { - f->WriteFloatString( "%i ", frameOffsets[i] ); + f->WriteFloatString( "\t\t\t\"frameOffsets\": [ " ); + for( int i = 0; i < frameOffsets.Num(); i++ ) + { + f->WriteFloatString( "%i%s", frameOffsets[i], ( i == frameOffsets.Num() - 1 ) ? "" : ", " ); + } + f->WriteFloatString( " ],\n" ); } - f->WriteFloatString( "\n" ); //f->WriteFloatString( "\t\t", frameLabels.Num() ); - for( int i = 0; i < frameLabels.Num(); i++ ) + if( frameLabels.Num() ) { - f->WriteFloatString( "%s\t\n", indentPrefix, frameLabels[i].frameNum, frameLabels[i].frameLabel.c_str() ); + f->WriteFloatString( "\t\t\t\"frameLabels\":\n\t\t\t[\n" ); + for( int i = 0; i < frameLabels.Num(); i++ ) + { + f->WriteFloatString( "\t\t\t\t\"label\": { \"frameNum\": %i, \"frameLabel\": \"%s\" }%s\n", frameLabels[i].frameNum, frameLabels[i].frameLabel.c_str(), ( i == frameLabels.Num() - 1 ) ? "" : ", " ); + } + f->WriteFloatString( "\t\t\t],\n" ); } -#if 0 +#if 1 idBase64 base64; + f->WriteFloatString( "\t\t\t\"commands\":\n\t\t\t[\n" ); for( int i = 0; i < commands.Num(); i++ ) { idSWFSprite::swfSpriteCommand_t& command = commands[i]; @@ -344,7 +343,7 @@ void idSWFSprite::WriteXML( idFile* f, int characterID, const char* indentPrefix // WriteXML_PlaceObject2( command.stream, indentPrefix ); // break; -#define HANDLE_SWF_TAG( x ) case Tag_##x: WriteXML_##x( f, command.stream, characterID, i, indentPrefix ); break; +#define HANDLE_SWF_TAG( x ) case Tag_##x: WriteJSON_##x( f, command.stream, characterID, i ); break; HANDLE_SWF_TAG( PlaceObject2 ); HANDLE_SWF_TAG( PlaceObject3 ); HANDLE_SWF_TAG( RemoveObject2 ); @@ -355,49 +354,49 @@ void idSWFSprite::WriteXML( idFile* f, int characterID, const char* indentPrefix break; //idLib::Printf( "Export Sprite: Unhandled tag %s\n", idSWF::GetTagName( command.tag ) ); } - - //f->WriteFloatString( "%s\t\n", indentPrefix ); } + f->WriteFloatString( "\n\t\t\t]\n" ); - for( int i = 0; i < doInitActions.Num(); i++ ) + if( doInitActions.Num() ) { - base64.Encode( doInitActions[i].Ptr(), doInitActions[i].Length() ); - - f->WriteFloatString( "%s\t%s\n", indentPrefix, doInitActions[i].Length(), base64.c_str() ); - - //f->WriteFloatString( "%s\t%s\n", indentPrefix, doInitActions[i].Length(), doInitActions[i].Ptr() ); + f->WriteFloatString( ",\n\t\t\t\"doInitActions\":\t\t\t[\n" ); + for( int i = 0; i < doInitActions.Num(); i++ ) + { + base64.Encode( doInitActions[i].Ptr(), doInitActions[i].Length() ); + + f->WriteFloatString( "\t\t\t\"DoInitAction\": { \"streamLength\": %i, \"stream\": \"%s\" }%s\n", doInitActions[i].Length(), base64.c_str(), ( i == doInitActions.Num() - 1 ) ? "" : ", " ); + } + f->WriteFloatString( "\t\t\t]" ); } #endif - - f->WriteFloatString( "%s\n", indentPrefix ); } <<<<<<< HEAD // RB end ======= -void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) +void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { uint64 flags = bitstream.ReadU8(); int depth = bitstream.ReadU16(); - file->WriteFloatString( "%s\tWriteFloatString( "%s\t\t\t\t{\n", ( commandID != 0 ) ? ",\n" : "" ); + file->WriteFloatString( "\t\t\t\t\t\"type\": \"Tag_PlaceObject2\",\n" ); + file->WriteFloatString( "\t\t\t\t\t\"flags\": %i, \"depth\": %i ", flags, depth ); int characterID = -1; if( ( flags & PlaceFlagHasCharacter ) != 0 ) { characterID = bitstream.ReadU16(); - file->WriteFloatString( " characterID=\"%i\"", characterID ); + file->WriteFloatString( ",\n\t\t\t\t\t\"characterID\": %i", characterID ); } - file->WriteFloatString( ">\n" ); - if( ( flags & PlaceFlagHasMatrix ) != 0 ) { swfMatrix_t m; bitstream.ReadMatrix( m ); - file->WriteFloatString( "%s\t\t%f %f %f %f %f %f\n", indentPrefix, m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + file->WriteFloatString( ",\n\t\t\t\t\t\"startMatrix\": [ %f, %f, %f, %f, %f, %f ]", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); } if( ( flags & PlaceFlagHasColorTransform ) != 0 ) @@ -406,12 +405,12 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream bitstream.ReadColorXFormRGBA( cxf ); idVec4 color = cxf.mul; - file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\"mulColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); color = cxf.add; if( color != vec4_origin ) { - file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\"addColor\": [%f %f %f %f ]", color.x, color.y, color.z, color.w ); } } @@ -419,14 +418,14 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream { float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); - file->WriteFloatString( "%s\t\t%f\n", indentPrefix, ratio ); + file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %f", ratio ); } if( ( flags & PlaceFlagHasName ) != 0 ) { idStr name = bitstream.ReadString(); - file->WriteFloatString( "%s\t\t%s\n", indentPrefix, name.c_str() ); + file->WriteFloatString( ",\n\t\t\t\t\t\"name\": \"%s\"", name.c_str() ); /*if( display->spriteInstance ) { @@ -442,7 +441,7 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream if( ( flags & PlaceFlagHasClipDepth ) != 0 ) { uint16 clipDepth = bitstream.ReadU16(); - file->WriteFloatString( "%s\t\t%i\n", indentPrefix, clipDepth ); + file->WriteFloatString( ",\n\t\t\t\t\t\"clipDepth\": %i", clipDepth ); } if( ( flags & PlaceFlagHasClipActions ) != 0 ) @@ -450,11 +449,11 @@ void idSWFSprite::WriteXML_PlaceObject2( idFile* file, idSWFBitStream& bitstream // FIXME: clip actions } - file->WriteFloatString( "%s\t\n", indentPrefix ); + file->WriteFloatString( "\n\t\t\t\t}" ); } -void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) +void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { uint64 flags1 = bitstream.ReadU8(); uint64 flags2 = bitstream.ReadU8(); @@ -548,19 +547,22 @@ void idSWFSprite::WriteXML_PlaceObject3( idFile* file, idSWFBitStream& bitstream file->WriteFloatString( "%s\t\n", indentPrefix ); } -void idSWFSprite::WriteXML_RemoveObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) +void idSWFSprite::WriteJSON_RemoveObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { int depth = bitstream.ReadU16(); - file->WriteFloatString( "%s\t\n", indentPrefix, depth ); + file->WriteFloatString( "%s\t\t\t\t{\t\"type\": \"Tag_RemoveObject2\", \"depth\": %i }", ( commandID != 0 ) ? ",\n" : "", depth ); } -void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix ) +void idSWFSprite::WriteJSON_DoAction( idFile* file, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix ) { idBase64 base64; base64.Encode( bitstream.Ptr(), bitstream.Length() ); + file->WriteFloatString( "%s\t\t\t\t{\t\"type\": \"Tag_DoAction\", \"streamLength\": %i, \"stream\": \"%s\" }", ( commandID != 0 ) ? ",\n" : "", bitstream.Length(), base64.c_str() ); + +#if 0 idSWFScriptObject* scriptObject = idSWFScriptObject::Alloc(); scriptObject->SetPrototype( &spriteInstanceScriptObjectPrototype ); // scriptObject->SetSprite( this ); @@ -578,10 +580,13 @@ void idSWFSprite::WriteXML_DoAction( idFile* file, idSWFBitStream& bitstream, in //file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); - file->WriteFloatString( "%s\t\n%s%s\t\n", indentPrefix, bitstream.Length(), scriptText.c_str(), indentPrefix ); - delete actionScript; delete scriptObject; +#endif + + + + } diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index 5a01494f..4c22105a 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -48,11 +48,11 @@ public: void Write( idFile* f ); // RB begin - void WriteXML( idFile* f, int characterID, const char* indentPrefix = "" ); - void WriteXML_PlaceObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); - void WriteXML_PlaceObject3( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); - void WriteXML_RemoveObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); - void WriteXML_DoAction( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteJSON( idFile* f, int characterID ); + void WriteJSON_PlaceObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteJSON_PlaceObject3( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteJSON_RemoveObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); + void WriteJSON_DoAction( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); void WriteSWF( idFile_SWF& f, int characterID ); From 5ddcb22a15806e3f1330ba400b735e31d2181394 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 7 Jun 2015 15:23:48 +0200 Subject: [PATCH 24/43] JSON Flash export 3 --- neo/swf/SWF_Load.cpp | 21 +++++++---------- neo/swf/SWF_Sprites.cpp | 52 ++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 4e4d6f5d..f1625129 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -971,7 +971,7 @@ void idSWF::WriteJSON( const char* filename ) file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); color = fillDraw.style.endColor.ToVec4(); - file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ],\n", color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]\n", color.x, color.y, color.z, color.w ); } if( fillDraw.style.type > 0 ) @@ -1111,8 +1111,8 @@ void idSWF::WriteJSON( const char* filename ) { const idSWFFont* font = dictionary[i].font; - file->WriteFloatString( "\t\t\n", - i, font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); + file->WriteFloatString( "\t\t\t\"name\": \"%s\", \"ascent\": %i, \"descent\": %i, \"leading\": %i, \"glyphsNum\": %i\n", + font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); #if 0 for( int g = 0; g < font->glyphs.Num(); g++ ) @@ -1140,7 +1140,6 @@ void idSWF::WriteJSON( const char* filename ) #endif } #endif - file->WriteFloatString( "\t\t\n" ); break; } @@ -1211,24 +1210,22 @@ void idSWF::WriteJSON( const char* filename ) { const idSWFEditText* et = dictionary[i].edittext; - file->WriteFloatString( "\t\t\n", - i, + idStr initialText = idStr::CStyleQuote( et->initialText.c_str() ); + + file->WriteFloatString( "\t\t\t\"flags\": %i, \"fontID\": %i, \"fontHeight\": %i, \"maxLength\": %i, \"align\": \"%s\", \"leftMargin\": %i, \"rightMargin\": %i, \"indent\": %i, \"leading\": %i, \"variable\": \"%s\", \"initialText\": %s,\n", et->flags, et->fontID, et->fontHeight, et->maxLength, idSWF::GetEditTextAlignName( et->align ), et->leftMargin, et->rightMargin, et->indent, et->leading, - et->variable.c_str(), et->initialText.c_str() ); + et->variable.c_str(), initialText.c_str() ); float x = et->bounds.tl.y; float y = et->bounds.tl.x; float width = fabs( et->bounds.br.y - et->bounds.tl.y ); float height = fabs( et->bounds.br.x - et->bounds.tl.x ); - file->WriteFloatString( "\t\t\t\n", x, y, width, height ); + file->WriteFloatString( "\t\t\t\"bounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); idVec4 color = et->color.ToVec4(); - file->WriteFloatString( "\t\t\t\n", - color.x, color.y, color.z, color.w ); - - file->WriteFloatString( "\t\t\n" ); + file->WriteFloatString( "\t\t\t\"color\": [ %f, %f, %f, %f ]\n", color.x, color.y, color.z, color.w ); //file->WriteBig( et->bounds.tl ); //file->WriteBig( et->bounds.br ); diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 2b02296a..4fe1b1a4 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -313,7 +313,7 @@ void idSWFSprite::WriteJSON( idFile* f, int characterID ) f->WriteFloatString( "\t\t\t\"frameLabels\":\n\t\t\t[\n" ); for( int i = 0; i < frameLabels.Num(); i++ ) { - f->WriteFloatString( "\t\t\t\t\"label\": { \"frameNum\": %i, \"frameLabel\": \"%s\" }%s\n", frameLabels[i].frameNum, frameLabels[i].frameLabel.c_str(), ( i == frameLabels.Num() - 1 ) ? "" : ", " ); + f->WriteFloatString( "\t\t\t\t{ \"frameNum\": %i, \"frameLabel\": \"%s\" }%s\n", frameLabels[i].frameNum, frameLabels[i].frameLabel.c_str(), ( i == frameLabels.Num() - 1 ) ? "" : ", " ); } f->WriteFloatString( "\t\t\t],\n" ); } @@ -376,21 +376,20 @@ void idSWFSprite::WriteJSON( idFile* f, int characterID ) void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { - uint64 flags = bitstream.ReadU8(); + uint64 flags1 = bitstream.ReadU8(); int depth = bitstream.ReadU16(); file->WriteFloatString( "%s\t\t\t\t{\n", ( commandID != 0 ) ? ",\n" : "" ); file->WriteFloatString( "\t\t\t\t\t\"type\": \"Tag_PlaceObject2\",\n" ); - file->WriteFloatString( "\t\t\t\t\t\"flags\": %i, \"depth\": %i ", flags, depth ); + file->WriteFloatString( "\t\t\t\t\t\"flags\": %i, \"depth\": %i", flags1, depth ); - int characterID = -1; - if( ( flags & PlaceFlagHasCharacter ) != 0 ) + if( ( flags1 & PlaceFlagHasCharacter ) != 0 ) { - characterID = bitstream.ReadU16(); + int characterID = bitstream.ReadU16(); file->WriteFloatString( ",\n\t\t\t\t\t\"characterID\": %i", characterID ); } - if( ( flags & PlaceFlagHasMatrix ) != 0 ) + if( ( flags1 & PlaceFlagHasMatrix ) != 0 ) { swfMatrix_t m; @@ -399,7 +398,7 @@ void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstrea file->WriteFloatString( ",\n\t\t\t\t\t\"startMatrix\": [ %f, %f, %f, %f, %f, %f ]", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); } - if( ( flags & PlaceFlagHasColorTransform ) != 0 ) + if( ( flags1 & PlaceFlagHasColorTransform ) != 0 ) { swfColorXform_t cxf; bitstream.ReadColorXFormRGBA( cxf ); @@ -414,14 +413,14 @@ void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstrea } } - if( ( flags & PlaceFlagHasRatio ) != 0 ) + if( ( flags1 & PlaceFlagHasRatio ) != 0 ) { float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %f", ratio ); } - if( ( flags & PlaceFlagHasName ) != 0 ) + if( ( flags1 & PlaceFlagHasName ) != 0 ) { idStr name = bitstream.ReadString(); @@ -438,13 +437,13 @@ void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstrea }*/ } - if( ( flags & PlaceFlagHasClipDepth ) != 0 ) + if( ( flags1 & PlaceFlagHasClipDepth ) != 0 ) { uint16 clipDepth = bitstream.ReadU16(); file->WriteFloatString( ",\n\t\t\t\t\t\"clipDepth\": %i", clipDepth ); } - if( ( flags & PlaceFlagHasClipActions ) != 0 ) + if( ( flags1 & PlaceFlagHasClipActions ) != 0 ) { // FIXME: clip actions } @@ -459,24 +458,23 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea uint64 flags2 = bitstream.ReadU8(); int depth = bitstream.ReadU16(); - file->WriteFloatString( "%s\tWriteFloatString( "%s\t\t\t\t{\n", ( commandID != 0 ) ? ",\n" : "" ); + file->WriteFloatString( "\t\t\t\t\t\"type\": \"Tag_PlaceObject3\",\n" ); + file->WriteFloatString( "\t\t\t\t\t\"flags1\": %i, \"flags2\": %i, \"depth\": %i", flags1, flags2, depth ); - int characterID = -1; if( ( flags1 & PlaceFlagHasCharacter ) != 0 ) { - characterID = bitstream.ReadU16(); - file->WriteFloatString( " characterID=\"%i\"", characterID ); + int characterID = bitstream.ReadU16(); + file->WriteFloatString( ",\n\t\t\t\t\t\"characterID\": %i", characterID ); } - file->WriteFloatString( ">\n" ); - if( ( flags1 & PlaceFlagHasMatrix ) != 0 ) { swfMatrix_t m; bitstream.ReadMatrix( m ); - file->WriteFloatString( "%s\t\t%f %f %f %f %f %f\n", indentPrefix, m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + file->WriteFloatString( ",\n\t\t\t\t\t\"startMatrix\": [ %f, %f, %f, %f, %f, %f ]", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); } if( ( flags1 & PlaceFlagHasColorTransform ) != 0 ) @@ -485,12 +483,12 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea bitstream.ReadColorXFormRGBA( cxf ); idVec4 color = cxf.mul; - file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\"mulColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); color = cxf.add; if( color != vec4_origin ) { - file->WriteFloatString( "%s\t\t\n", indentPrefix, color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\"addColor\": [%f %f %f %f ]", color.x, color.y, color.z, color.w ); } } @@ -498,14 +496,14 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea { float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); - file->WriteFloatString( "%s\t\t%f\n", indentPrefix, ratio ); + file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %f", ratio ); } if( ( flags1 & PlaceFlagHasName ) != 0 ) { idStr name = bitstream.ReadString(); - file->WriteFloatString( "%s\t\t%s\n", indentPrefix, name.c_str() ); + file->WriteFloatString( ",\n\t\t\t\t\t\"name\": \"%s\"", name.c_str() ); /*if( display->spriteInstance ) { @@ -521,7 +519,7 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea if( ( flags1 & PlaceFlagHasClipDepth ) != 0 ) { uint16 clipDepth = bitstream.ReadU16(); - file->WriteFloatString( "%s\t\t%i\n", indentPrefix, clipDepth ); + file->WriteFloatString( ",\n\t\t\t\t\t\"clipDepth\": %i", clipDepth ); } if( ( flags2 & PlaceFlagHasFilterList ) != 0 ) @@ -529,14 +527,14 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea // we don't support filters and because the filter list is variable length we // can't support anything after the filter list either (blend modes and clip actions) //idLib::Warning( "PlaceObject3: has filters" ); - file->WriteFloatString( "%s\t\t\n", indentPrefix ); + file->WriteFloatString( ",\n\t\t\t\t\t\"hasFilterList\": true" ); return; } if( ( flags2 & PlaceFlagHasBlendMode ) != 0 ) { uint8 blendMode = bitstream.ReadU8(); - file->WriteFloatString( "%s\t\t%i\n", indentPrefix, blendMode ); + file->WriteFloatString( ",\n\t\t\t\t\t\"blendMode\": %i", blendMode ); } if( ( flags1 & PlaceFlagHasClipActions ) != 0 ) @@ -544,7 +542,7 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea // FIXME: clip actions } - file->WriteFloatString( "%s\t\n", indentPrefix ); + file->WriteFloatString( "\n\t\t\t\t}" ); } void idSWFSprite::WriteJSON_RemoveObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) From 17f3059d646dac535f6d10ca16bf1c2f0d3d881a Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 7 Jun 2015 23:09:23 +0200 Subject: [PATCH 25/43] JSON Flash export 4 --- neo/swf/SWF_Load.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index f1625129..1605d923 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -884,17 +884,17 @@ void idSWF::WriteJSON( const char* filename ) { idSWFShape* shape = dictionary[i].shape; - float x = shape->startBounds.tl.y; - float y = shape->startBounds.tl.x; - float width = fabs( shape->startBounds.br.y - shape->startBounds.tl.y ); - float height = fabs( shape->startBounds.br.x - shape->startBounds.tl.x ); + float x = shape->startBounds.tl.x; + float y = shape->startBounds.tl.y; + float width = fabs( shape->startBounds.br.x - shape->startBounds.tl.x ); + float height = fabs( shape->startBounds.br.y - shape->startBounds.tl.y ); file->WriteFloatString( "\t\t\t\"startBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); - x = shape->endBounds.tl.y; - y = shape->endBounds.tl.x; - width = fabs( shape->endBounds.br.y - shape->endBounds.tl.y ); - height = fabs( shape->endBounds.br.x - shape->endBounds.tl.x ); + x = shape->endBounds.tl.x; + y = shape->endBounds.tl.y; + width = fabs( shape->endBounds.br.x - shape->endBounds.tl.x ); + height = fabs( shape->endBounds.br.y - shape->endBounds.tl.y ); file->WriteFloatString( "\t\t\t\"endBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); @@ -1217,10 +1217,10 @@ void idSWF::WriteJSON( const char* filename ) et->leftMargin, et->rightMargin, et->indent, et->leading, et->variable.c_str(), initialText.c_str() ); - float x = et->bounds.tl.y; - float y = et->bounds.tl.x; - float width = fabs( et->bounds.br.y - et->bounds.tl.y ); - float height = fabs( et->bounds.br.x - et->bounds.tl.x ); + float x = et->bounds.tl.x; + float y = et->bounds.tl.y; + float width = fabs( et->bounds.br.x - et->bounds.tl.x ); + float height = fabs( et->bounds.br.y - et->bounds.tl.y ); file->WriteFloatString( "\t\t\t\"bounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); From 6d4a3f107bea51f4ef93da3585b0e795a3410adc Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 8 Jun 2015 01:57:53 +0200 Subject: [PATCH 26/43] JSON Flash export 5 --- neo/swf/SWF_Load.cpp | 46 +++++++++++++++++++++++++++++------------ neo/swf/SWF_Sprites.cpp | 4 ++-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 1605d923..afad9e85 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -900,6 +900,11 @@ void idSWF::WriteJSON( const char* filename ) // export fill draws file->WriteFloatString( "\t\t\t\"fillDraws\":\n\t\t\t[\n" ); + + if( shape->fillDraws.Num() > 1 ) + { + idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "%s.Shape%i has %i fill draws\n", filename, i, shape->fillDraws.Num() ); + } for( int d = 0; d < shape->fillDraws.Num(); d++ ) { @@ -1013,37 +1018,52 @@ void idSWF::WriteJSON( const char* filename ) file->WriteFloatString( "\t\t\t\t\t\t],\n" ); } - file->WriteFloatString( "\n\t\t\t\t\t},\n" ); + file->WriteFloatString( "\n\t\t\t\t\t}" ); - file->WriteFloatString( "\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); - for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + if( fillDraw.startVerts.Num() ) { - const idVec2& vert = fillDraw.startVerts[v]; + file->WriteFloatString( ",\n\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); + for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + { + const idVec2& vert = fillDraw.startVerts[v]; - file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.startVerts.Num() - 1 ) ) ? "" : "," ); + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.startVerts.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t\t]" ); } - file->WriteFloatString( "\t\t\t\t\t],\n" ); if( fillDraw.endVerts.Num() ) { - file->WriteFloatString( "\t\t\t\t\t\"endVerts\":\n\t\t\t\t\t[\n" ); + file->WriteFloatString( ",\n\t\t\t\t\t\"endVerts\":\n\t\t\t\t\t[\n" ); for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) { const idVec2& vert = fillDraw.endVerts[v]; file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.endVerts.Num() - 1 ) ) ? "" : "," ); } - file->WriteFloatString( "\t\t\t\t\t],\n" ); + file->WriteFloatString( "\t\t\t\t\t]" ); } - file->WriteFloatString( "\t\t\t\t\t\"indices\": [ " ); - for( int v = 0; v < fillDraw.indices.Num(); v++ ) + if( fillDraw.indices.Num() ) { - const uint16& vert = fillDraw.indices[v]; + file->WriteFloatString( ",\n\t\t\t\t\t\"indices\": [ " ); +#if 0 + for( int v = 0; v < fillDraw.indices.Num(); v++ ) + { + const uint16& vert = fillDraw.indices[v]; - file->WriteFloatString( "%i%s", vert, ( v == fillDraw.indices.Num() - 1 ) ? "" : ", " ); + file->WriteFloatString( "%i%s", vert, ( v == fillDraw.indices.Num() - 1 ) ? "" : ", " ); + } +#else + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + const uint16& vert = fillDraw.indices[v]; + + file->WriteFloatString( "%i%s", vert, ( v == 0 ) ? "" : ", " ); + } +#endif + file->WriteFloatString( "]\n" ); } - file->WriteFloatString( "]\n" ); file->WriteFloatString( "\t\t\t\t}%s\n", ( d == ( shape->fillDraws.Num() - 1 ) ) ? "" : "," ); } diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 4fe1b1a4..1d5e8856 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -409,7 +409,7 @@ void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstrea color = cxf.add; if( color != vec4_origin ) { - file->WriteFloatString( ",\n\t\t\t\t\t\"addColor\": [%f %f %f %f ]", color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\"addColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); } } @@ -488,7 +488,7 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea color = cxf.add; if( color != vec4_origin ) { - file->WriteFloatString( ",\n\t\t\t\t\t\"addColor\": [%f %f %f %f ]", color.x, color.y, color.z, color.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\"addColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); } } From d4eb9f8d930f411cb032d5d6b511dff7c612b26c Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 9 Jun 2015 17:58:24 +0200 Subject: [PATCH 27/43] swf_show debug rendering --- neo/swf/SWF.h | 10 + neo/swf/SWF_Load.cpp | 20 +- neo/swf/SWF_Main.cpp | 3 + neo/swf/SWF_Render.cpp | 461 ++++++++++++++++++++++++++++++++++++++++- neo/swf/SWF_Types.h | 35 ++++ 5 files changed, 520 insertions(+), 9 deletions(-) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index ced22606..cefbc453 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -385,6 +385,9 @@ private: const idMaterial* guiCursor_arrow; const idMaterial* guiCursor_hand; const idMaterial* white; + // RB begin + const idFont* debugFont; + // RB end private: friend class idSWFSprite; @@ -450,6 +453,13 @@ private: uint64 GLStateForRenderState( const swfRenderState_t& renderState ); void FindTooltipIcons( idStr* text ); + // RB: debugging tools + swfRect_t CalcRect( const idSWFSpriteInstance* sprite, const swfRenderState_t& renderState ); + void DrawRect( idRenderSystem* gui, const swfRect_t& rect, const idVec4& color ); + int DrawText( idRenderSystem* gui, float x, float y, float scale, idVec4 color, const char* text, float adjust, int limit, int style ); + int DrawText( idRenderSystem* gui, const char* text, float textScale, int textAlign, idVec4 color, const swfRect_t& rectDraw, bool wrap, int cursor = -1, bool calcOnly = false, idList* breaks = NULL, int limit = 0 ); + // RB end + //---------------------------------- // SWF_Image.cpp //---------------------------------- diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index afad9e85..88b717e6 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -900,7 +900,7 @@ void idSWF::WriteJSON( const char* filename ) // export fill draws file->WriteFloatString( "\t\t\t\"fillDraws\":\n\t\t\t[\n" ); - + if( shape->fillDraws.Num() > 1 ) { idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "%s.Shape%i has %i fill draws\n", filename, i, shape->fillDraws.Num() ); @@ -1026,7 +1026,7 @@ void idSWF::WriteJSON( const char* filename ) for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) { const idVec2& vert = fillDraw.startVerts[v]; - + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.startVerts.Num() - 1 ) ) ? "" : "," ); } file->WriteFloatString( "\t\t\t\t\t]" ); @@ -1051,7 +1051,7 @@ void idSWF::WriteJSON( const char* filename ) for( int v = 0; v < fillDraw.indices.Num(); v++ ) { const uint16& vert = fillDraw.indices[v]; - + file->WriteFloatString( "%i%s", vert, ( v == fillDraw.indices.Num() - 1 ) ? "" : ", " ); } #else @@ -1267,15 +1267,19 @@ void idSWF::WriteJSON( const char* filename ) if( dictionary[i].type != SWF_DICT_NULL ) { - file->WriteFloatString( "\t\t}%s\n", ( i == ( dictionary.Num() - 1 ) ) ? "" : "," ); + //file->WriteFloatString( "\t\t}%s\n", ( i == ( dictionary.Num() - 1 ) ) ? "" : "," ); + file->WriteFloatString( "\t\t},\n" ); } } - file->WriteFloatString( "\t],\n" ); - - file->WriteFloatString( "\t\"mainsprite\":\n\t{\n" ); + file->WriteFloatString( "\t\t{\n" ); + file->WriteFloatString( "\t\t\t\"type\": \"SPRITE\",\n" ); + file->WriteFloatString( "\t\t\t\"characterID\": %i,\n", dictionary.Num() ); + file->WriteFloatString( "\t\t\t\"mainsprite\": true,\n" ); mainsprite->WriteJSON( file, dictionary.Num() ); - file->WriteFloatString( "\t}\n}\n" ); + file->WriteFloatString( "\t\t}\n" ); + file->WriteFloatString( "\t]\n" ); + file->WriteFloatString( "}\n" ); } // RB end diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 7a50c51a..66f2688f 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -68,6 +68,9 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) guiCursor_hand = declManager->FindMaterial( "ui/assets/guicursor_hand" ); white = declManager->FindMaterial( "_white" ); + // RB: + debugFont = renderSystem->RegisterFont( "Arial Narrow" ); + tooltipButtonImage.Append( keyButtonImages_t( "", "guis/assets/hud/controller/xb360/a", "guis/assets/hud/controller/ps3/cross", 37, 37, 0 ) ); tooltipButtonImage.Append( keyButtonImages_t( "", "guis/assets/hud/controller/xb360/b", "guis/assets/hud/controller/ps3/circle", 37, 37, 0 ) ); tooltipButtonImage.Append( keyButtonImages_t( "", "guis/assets/hud/controller/xb360/x", "guis/assets/hud/controller/ps3/square", 37, 37, 0 ) ); diff --git a/neo/swf/SWF_Render.cpp b/neo/swf/SWF_Render.cpp index aa99543b..bcbe8ab5 100644 --- a/neo/swf/SWF_Render.cpp +++ b/neo/swf/SWF_Render.cpp @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -42,6 +42,8 @@ idCVar swf_skipSolids( "swf_skipSolids", "0", CVAR_BOOL, "" ); idCVar swf_skipGradients( "swf_skipGradients", "0", CVAR_BOOL, "" ); idCVar swf_skipLineDraws( "swf_skipLineDraws", "0", CVAR_BOOL, "" ); idCVar swf_skipBitmaps( "swf_skipBitmaps", "0", CVAR_BOOL, "" ); + +idCVar swf_show( "swf_show", "0", CVAR_INTEGER, "" ); // RB end extern idCVar swf_textStrokeSize; @@ -462,6 +464,33 @@ void idSWF::RenderSprite( idRenderSystem* gui, idSWFSpriteInstance* spriteInstan //idLib::Warning( "%s: Tried to render an unrenderable character %d", filename.c_str(), entry->type ); } } + + // RB begin + if( swf_show.GetInteger() > 0 && !spriteInstance->name.IsEmpty() )//Icmp( "buttonBar" ) == 0 ) + { + swfRect_t rect = CalcRect( spriteInstance, renderState ); + + DrawRect( gui, rect, colorRed ); + + if( swf_show.GetInteger() > 1 ) + { + idVec4 color = colorWhite; + + if( spriteInstance->parent != NULL && spriteInstance->parent == mainspriteInstance ) + { + color = colorCyan; + } + + idStr str; + //str = display.spriteInstance->name.c_str(); + sprintf( str, "%s\n%s", spriteInstance->name.c_str(), GetName() ); + + DrawText( gui, str, 0.35f, 0, color, swfRect_t( rect.tl.x, rect.tl.y, 300, 40 ), false ); + //DrawText( gui, str, 0.25 * 2, 0, colorWhite, swfRect_t( rect.tl.x, rect.tl.y, 300, 40 ), false ); + } + } + // RB end + for( int j = 0; j < activeMasks.Num(); j++ ) { const swfDisplayEntry_t* mask = activeMasks[ j ]; @@ -1876,4 +1905,434 @@ void idSWF::FindTooltipIcons( idStr* text ) } } +// RB begin +swfRect_t idSWF::CalcRect( const idSWFSpriteInstance* spriteInstance, const swfRenderState_t& renderState ) +{ + swfRect_t bounds; + bounds.tl.x = renderState.matrix.tx; + bounds.tl.y = renderState.matrix.ty; + bounds.br.x = renderState.matrix.tx; + bounds.br.y = renderState.matrix.ty; + + if( spriteInstance == NULL ) + { + idLib::Warning( "%s: CalcRect: spriteInstance == NULL", filename.c_str() ); + return bounds; + } + +#if 1 + for( int i = 0; i < spriteInstance->displayList.Num(); i++ ) + { + const swfDisplayEntry_t& display = spriteInstance->displayList[i]; + + idSWFDictionaryEntry* entry = FindDictionaryEntry( display.characterID ); + if( entry == NULL ) + { + continue; + } + + swfRenderState_t renderState2; + renderState2.matrix = display.matrix.Multiply( renderState.matrix ); + + if( entry->type == SWF_DICT_SPRITE ) + { + swfRect_t spriteBounds = CalcRect( display.spriteInstance, renderState2 ); + + if( spriteBounds.tl.x < bounds.tl.x ) + { + bounds.tl.x = spriteBounds.tl.x; + } + else if( spriteBounds.br.x > bounds.br.x ) + { + bounds.br.x = spriteBounds.br.x; + } + + if( spriteBounds.tl.y < bounds.tl.y ) + { + bounds.tl.y = spriteBounds.tl.y; + } + else if( spriteBounds.br.y > bounds.br.y ) + { + bounds.br.y = spriteBounds.br.y; + } + } + else if( entry->type == SWF_DICT_SHAPE ) + { + const idSWFShape* shape = entry->shape; + + for( int i = 0; i < shape->fillDraws.Num(); i++ ) + { + const idSWFShapeDrawFill& fill = shape->fillDraws[i]; + + for( int j = 0; j < fill.startVerts.Num(); j++ ) + { + const idVec2& xy = fill.startVerts[j]; + + idVec2 p = renderState.matrix.Transform( xy );//.Scale( scaleToVirtual ); + + if( p.x < bounds.tl.x ) + { + bounds.tl.x = p.x; + } + else if( p.x > bounds.br.x ) + { + bounds.br.x = p.x; + } + + if( p.y < bounds.tl.y ) + { + bounds.tl.y = p.y; + } + else if( p.y > bounds.br.y ) + { + bounds.br.y = p.y; + } + } + } + } + else if( entry->type == SWF_DICT_MORPH ) + { + // TODO + } + else if( entry->type == SWF_DICT_EDITTEXT ) + { + // TODO + } + else + { + //idLib::Warning( "%s: Tried to render an unrenderable character %d", filename.c_str(), entry->type ); + } + } +#endif + + return bounds; +} +void idSWF::DrawRect( idRenderSystem* gui, const swfRect_t& rect, const idVec4& color ) +{ + renderSystem->SetColor( color ); + + float x = rect.tl.x; + float y = rect.tl.y; + float w = fabs( rect.br.x - rect.tl.x ); + float h = fabs( rect.br.y - rect.tl.y ); + + float size = 1; + + DrawStretchPic( x, y, size, h, 0, 0, 0, 0, white ); + DrawStretchPic( x + w - size, y, size, h, 0, 0, 0, 0, white ); + DrawStretchPic( x, y, w, size, 0, 0, 0, 0, white ); + DrawStretchPic( x, y + h - size, w, size, 0, 0, 0, 0, white ); +} + +static triIndex_t quadPicIndexes[6] = { 3, 0, 2, 2, 0, 1 }; +int idSWF::DrawText( idRenderSystem* gui, float x, float y, float scale, idVec4 color, const char* text, float adjust, int limit, int style ) +{ + /* + if( !matIsIdentity || cursor != -1 ) + { + // fallback to old code + return idDeviceContext::DrawText( x, y, scale, color, text, adjust, limit, style, cursor ); + } + */ + + idStr drawText = text; + + if( drawText.Length() == 0 ) + { + return 0; + } + if( color.w == 0.0f ) + { + return 0; + } + + const uint32 currentColor = PackColor( color ); + uint32 currentColorNativeByteOrder = LittleLong( currentColor ); + + int len = drawText.Length(); + if( limit > 0 && len > limit ) + { + len = limit; + } + + int charIndex = 0; + while( charIndex < drawText.Length() ) + { + uint32 textChar = drawText.UTF8Char( charIndex ); + if( textChar == C_COLOR_ESCAPE ) + { + // I'm not sure if inline text color codes are used anywhere in the game, + // they may only be needed for multi-color user names + idVec4 newColor; + uint32 colorIndex = drawText.UTF8Char( charIndex ); + if( colorIndex == C_COLOR_DEFAULT ) + { + newColor = color; + } + else + { + newColor = idStr::ColorForIndex( colorIndex ); + newColor[3] = color[3]; + } + renderSystem->SetColor( newColor ); + currentColorNativeByteOrder = LittleLong( PackColor( newColor ) ); + continue; + } + + scaledGlyphInfo_t glyphInfo; + debugFont->GetScaledGlyph( scale, textChar, glyphInfo ); + + // PaintChar( x, y, glyphInfo ); + float drawY = y - glyphInfo.top; + float drawX = x + glyphInfo.left; + float w = glyphInfo.width; + float h = glyphInfo.height; + float s = glyphInfo.s1; + float t = glyphInfo.t1; + float s2 = glyphInfo.s2; + float t2 = glyphInfo.t2; + + float xOffset = 0; + float yOffset = 0; + + //idDrawVert* verts = gui->AllocTris( fill.startVerts.Num(), fill.indices.Ptr(), fill.indices.Num(), material, renderState.stereoDepth ); + + //if( !ClippedCoords( &drawX, &drawY, &w, &h, &s, &t, &s2, &t2 ) ) + { + float x1 = xOffset + drawX * scaleToVirtual.x; + float x2 = xOffset + ( drawX + w ) * scaleToVirtual.x; + float y1 = yOffset + drawY * scaleToVirtual.y; + float y2 = yOffset + ( drawY + h ) * scaleToVirtual.y; + idDrawVert* verts = gui->AllocTris( 4, quadPicIndexes, 6, glyphInfo.material, STEREO_DEPTH_TYPE_NONE ); + if( verts != NULL ) + { + verts[0].xyz[0] = x1; + verts[0].xyz[1] = y1; + verts[0].xyz[2] = 0.0f; + verts[0].SetTexCoord( s, t ); + verts[0].SetNativeOrderColor( currentColorNativeByteOrder ); + verts[0].ClearColor2(); + + verts[1].xyz[0] = x2; + verts[1].xyz[1] = y1; + verts[1].xyz[2] = 0.0f; + verts[1].SetTexCoord( s2, t ); + verts[1].SetNativeOrderColor( currentColorNativeByteOrder ); + verts[1].ClearColor2(); + + verts[2].xyz[0] = x2; + verts[2].xyz[1] = y2; + verts[2].xyz[2] = 0.0f; + verts[2].SetTexCoord( s2, t2 ); + verts[2].SetNativeOrderColor( currentColorNativeByteOrder ); + verts[2].ClearColor2(); + + verts[3].xyz[0] = x1; + verts[3].xyz[1] = y2; + verts[3].xyz[2] = 0.0f; + verts[3].SetTexCoord( s, t2 ); + verts[3].SetNativeOrderColor( currentColorNativeByteOrder ); + verts[3].ClearColor2(); + } + } + + x += glyphInfo.xSkip + adjust; + } + return drawText.Length(); +} + +int idSWF::DrawText( idRenderSystem* gui, const char* text, float textScale, int textAlign, idVec4 color, const swfRect_t& rectDraw, bool wrap, int cursor, bool calcOnly, idList* breaks, int limit ) +{ + int count = 0; + int charIndex = 0; + int lastBreak = 0; + float y = 0.0f; + float textWidth = 0.0f; + float textWidthAtLastBreak = 0.0f; + + float charSkip = idMath::Ftoi( debugFont->GetMaxCharWidth( textScale ) ) + 1; + float lineSkip = idMath::Ftoi( debugFont->GetMaxCharWidth( textScale ) ); + + bool lineBreak = false; + bool wordBreak = false; + + float rectWidth = fabs( rectDraw.br.x - rectDraw.tl.x ); + float rectHeight = fabs( rectDraw.br.y - rectDraw.tl.y ); + + idStr drawText = text; + idStr textBuffer; + + if( !calcOnly && !( text && *text ) ) + { + //if( cursor == 0 ) + //{ + // renderSystem->SetColor( color ); + // DrawEditCursor( rectDraw.tl.x, lineSkip + rectDraw.y, textScale ); + //} + return idMath::Ftoi( rectWidth / charSkip ); + } + + y = lineSkip + rectDraw.y(); + + if( breaks ) + { + breaks->Append( 0 ); + } + + while( charIndex < drawText.Length() ) + { + uint32 textChar = drawText.UTF8Char( charIndex ); + + // See if we need to start a new line. + if( textChar == '\n' || textChar == '\r' || charIndex == drawText.Length() ) + { + lineBreak = true; + if( charIndex < drawText.Length() ) + { + // New line character and we still have more text to read. + char nextChar = drawText[ charIndex + 1 ]; + if( ( textChar == '\n' && nextChar == '\r' ) || ( textChar == '\r' && nextChar == '\n' ) ) + { + // Just absorb extra newlines. + textChar = drawText.UTF8Char( charIndex ); + } + } + } + + // Check for escape colors if not then simply get the glyph width. + if( textChar == C_COLOR_ESCAPE && charIndex < drawText.Length() ) + { + textBuffer.AppendUTF8Char( textChar ); + textChar = drawText.UTF8Char( charIndex ); + } + + // If the character isn't a new line then add it to the text buffer. + if( textChar != '\n' && textChar != '\r' ) + { + textWidth += debugFont->GetGlyphWidth( textScale, textChar ); + textBuffer.AppendUTF8Char( textChar ); + } + + if( !lineBreak && ( textWidth > rectWidth ) ) + { + // The next character will cause us to overflow, if we haven't yet found a suitable + // break spot, set it to be this character + if( textBuffer.Length() > 0 && lastBreak == 0 ) + { + lastBreak = textBuffer.Length(); + textWidthAtLastBreak = textWidth; + } + wordBreak = true; + } + else if( lineBreak || ( wrap && ( textChar == ' ' || textChar == '\t' ) ) ) + { + // The next character is in view, so if we are a break character, store our position + lastBreak = textBuffer.Length(); + textWidthAtLastBreak = textWidth; + } + + // We need to go to a new line + if( lineBreak || wordBreak ) + { + float x = rectDraw.tl.x; + + if( textWidthAtLastBreak > 0 ) + { + textWidth = textWidthAtLastBreak; + } + + // Align text if needed +#if 0 + if( textAlign == ALIGN_RIGHT ) + { + x = rectDraw.tl.x + rectWidth - textWidth; + } + else if( textAlign == ALIGN_CENTER ) + { + x = rectDraw.tl.x + ( rectWidth - textWidth ) / 2; + } +#endif + + if( wrap || lastBreak > 0 ) + { + // This is a special case to handle breaking in the middle of a word. + // if we didn't do this, the cursor would appear on the end of this line + // and the beginning of the next. + if( wordBreak && cursor >= lastBreak && lastBreak == textBuffer.Length() ) + { + cursor++; + } + } + + // Draw what's in the current text buffer. + if( !calcOnly ) + { + if( lastBreak > 0 ) + { + count += DrawText( gui, x, y, textScale, color, textBuffer.Left( lastBreak ).c_str(), 0, 0, 0 ); + textBuffer = textBuffer.Right( textBuffer.Length() - lastBreak ); + } + else + { + count += DrawText( gui, x, y, textScale, color, textBuffer.c_str(), 0, 0, 0 ); + textBuffer.Clear(); + } + } + + if( cursor < lastBreak ) + { + cursor = -1; + } + else if( cursor >= 0 ) + { + cursor -= ( lastBreak + 1 ); + } + + // If wrap is disabled return at this point. + if( !wrap ) + { + return lastBreak; + } + + // If we've hit the allowed character limit then break. + if( limit && count > limit ) + { + break; + } + + y += lineSkip + 5; + + if( !calcOnly && y > rectDraw.Bottom() ) + { + break; + } + + // If breaks were requested then make a note of this one. + if( breaks ) + { + breaks->Append( drawText.Length() - charIndex ); + } + + // Reset necessary parms for next line. + lastBreak = 0; + textWidth = 0; + textWidthAtLastBreak = 0; + lineBreak = false; + wordBreak = false; + + // Reassess the remaining width + for( int i = 0; i < textBuffer.Length(); ) + { + if( textChar != C_COLOR_ESCAPE ) + { + textWidth += debugFont->GetGlyphWidth( textScale, textBuffer.UTF8Char( i ) ); + } + } + + continue; + } + } + + return idMath::Ftoi( rectWidth / charSkip ); +} \ No newline at end of file diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index ddec8a00..1c229c0a 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -67,6 +67,41 @@ struct swfRect_t swfRect_t(); idVec2 tl; idVec2 br; + + // RB: helpers + swfRect_t( float x, float y, float w, float h ) + { + tl.x = x; + tl.y = y; + br.x = x + w; + br.y = y + h; + } + + float x() const + { + return tl.x; + } + + float y() const + { + return tl.y; + } + + float w() const + { + return fabs( br.x - tl.x ); + } + + float h() const + { + return fabs( br.y - tl.y ); + } + + float Bottom() const + { + return br.y; + } + // RB end }; struct swfMatrix_t { From a7472618a72fb7fa91bc7de8ca07e9cb01d0f413 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Wed, 10 Jun 2015 01:29:30 +0200 Subject: [PATCH 28/43] swf_show debug rendering --- neo/swf/SWF_Load.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 88b717e6..05a8554e 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -889,6 +889,7 @@ void idSWF::WriteJSON( const char* filename ) float width = fabs( shape->startBounds.br.x - shape->startBounds.tl.x ); float height = fabs( shape->startBounds.br.y - shape->startBounds.tl.y ); + //file->WriteFloatString( "\t\t\t\"imageSize\": { \"width\": %f, \"height\": %f },\n", dictionary[i].imageSize[0], dictionary[i].imageSize[1] ); file->WriteFloatString( "\t\t\t\"startBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); x = shape->endBounds.tl.x; @@ -1047,7 +1048,7 @@ void idSWF::WriteJSON( const char* filename ) if( fillDraw.indices.Num() ) { file->WriteFloatString( ",\n\t\t\t\t\t\"indices\": [ " ); -#if 0 +#if 1 for( int v = 0; v < fillDraw.indices.Num(); v++ ) { const uint16& vert = fillDraw.indices[v]; From cec8cb2d45b50f6dd3dc84276895dde47998b134 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 3 Jul 2015 23:24:00 +0200 Subject: [PATCH 29/43] Added RapidJSON 1.0.2 --- neo/libs/rapidjson/CHANGELOG.md | 79 + neo/libs/rapidjson/CMakeLists.txt | 114 + neo/libs/rapidjson/example/CMakeLists.txt | 29 + .../example/capitalize/capitalize.cpp | 66 + .../rapidjson/example/condense/condense.cpp | 32 + .../example/messagereader/messagereader.cpp | 96 + neo/libs/rapidjson/example/pretty/pretty.cpp | 30 + .../example/prettyauto/prettyauto.cpp | 56 + .../rapidjson/example/serialize/serialize.cpp | 157 ++ .../rapidjson/example/simpledom/simpledom.cpp | 29 + .../example/simplereader/simplereader.cpp | 38 + .../example/simplewriter/simplewriter.cpp | 35 + .../rapidjson/example/tutorial/tutorial.cpp | 151 ++ .../rapidjson/include/rapidjson/allocators.h | 261 +++ .../rapidjson/include/rapidjson/document.h | 2014 +++++++++++++++++ .../include/rapidjson/encodedstream.h | 261 +++ .../rapidjson/include/rapidjson/encodings.h | 625 +++++ .../rapidjson/include/rapidjson/error/en.h | 65 + .../rapidjson/include/rapidjson/error/error.h | 146 ++ .../include/rapidjson/filereadstream.h | 88 + .../include/rapidjson/filewritestream.h | 91 + .../include/rapidjson/internal/biginteger.h | 280 +++ .../include/rapidjson/internal/diyfp.h | 247 ++ .../include/rapidjson/internal/dtoa.h | 217 ++ .../include/rapidjson/internal/ieee754.h | 77 + .../include/rapidjson/internal/itoa.h | 304 +++ .../include/rapidjson/internal/meta.h | 181 ++ .../include/rapidjson/internal/pow10.h | 55 + .../include/rapidjson/internal/stack.h | 179 ++ .../include/rapidjson/internal/strfunc.h | 39 + .../include/rapidjson/internal/strtod.h | 270 +++ .../include/rapidjson/memorybuffer.h | 70 + .../include/rapidjson/memorystream.h | 61 + .../include/rapidjson/msinttypes/inttypes.h | 316 +++ .../include/rapidjson/msinttypes/stdint.h | 300 +++ .../include/rapidjson/prettywriter.h | 207 ++ .../rapidjson/include/rapidjson/rapidjson.h | 654 ++++++ neo/libs/rapidjson/include/rapidjson/reader.h | 1452 ++++++++++++ .../include/rapidjson/stringbuffer.h | 93 + neo/libs/rapidjson/include/rapidjson/writer.h | 395 ++++ neo/libs/rapidjson/license.txt | 57 + neo/libs/rapidjson/readme.md | 129 ++ 42 files changed, 10046 insertions(+) create mode 100644 neo/libs/rapidjson/CHANGELOG.md create mode 100644 neo/libs/rapidjson/CMakeLists.txt create mode 100644 neo/libs/rapidjson/example/CMakeLists.txt create mode 100644 neo/libs/rapidjson/example/capitalize/capitalize.cpp create mode 100644 neo/libs/rapidjson/example/condense/condense.cpp create mode 100644 neo/libs/rapidjson/example/messagereader/messagereader.cpp create mode 100644 neo/libs/rapidjson/example/pretty/pretty.cpp create mode 100644 neo/libs/rapidjson/example/prettyauto/prettyauto.cpp create mode 100644 neo/libs/rapidjson/example/serialize/serialize.cpp create mode 100644 neo/libs/rapidjson/example/simpledom/simpledom.cpp create mode 100644 neo/libs/rapidjson/example/simplereader/simplereader.cpp create mode 100644 neo/libs/rapidjson/example/simplewriter/simplewriter.cpp create mode 100644 neo/libs/rapidjson/example/tutorial/tutorial.cpp create mode 100644 neo/libs/rapidjson/include/rapidjson/allocators.h create mode 100644 neo/libs/rapidjson/include/rapidjson/document.h create mode 100644 neo/libs/rapidjson/include/rapidjson/encodedstream.h create mode 100644 neo/libs/rapidjson/include/rapidjson/encodings.h create mode 100644 neo/libs/rapidjson/include/rapidjson/error/en.h create mode 100644 neo/libs/rapidjson/include/rapidjson/error/error.h create mode 100644 neo/libs/rapidjson/include/rapidjson/filereadstream.h create mode 100644 neo/libs/rapidjson/include/rapidjson/filewritestream.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/biginteger.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/diyfp.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/dtoa.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/ieee754.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/itoa.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/meta.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/pow10.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/stack.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/strfunc.h create mode 100644 neo/libs/rapidjson/include/rapidjson/internal/strtod.h create mode 100644 neo/libs/rapidjson/include/rapidjson/memorybuffer.h create mode 100644 neo/libs/rapidjson/include/rapidjson/memorystream.h create mode 100644 neo/libs/rapidjson/include/rapidjson/msinttypes/inttypes.h create mode 100644 neo/libs/rapidjson/include/rapidjson/msinttypes/stdint.h create mode 100644 neo/libs/rapidjson/include/rapidjson/prettywriter.h create mode 100644 neo/libs/rapidjson/include/rapidjson/rapidjson.h create mode 100644 neo/libs/rapidjson/include/rapidjson/reader.h create mode 100644 neo/libs/rapidjson/include/rapidjson/stringbuffer.h create mode 100644 neo/libs/rapidjson/include/rapidjson/writer.h create mode 100644 neo/libs/rapidjson/license.txt create mode 100644 neo/libs/rapidjson/readme.md diff --git a/neo/libs/rapidjson/CHANGELOG.md b/neo/libs/rapidjson/CHANGELOG.md new file mode 100644 index 00000000..8ad9b3c3 --- /dev/null +++ b/neo/libs/rapidjson/CHANGELOG.md @@ -0,0 +1,79 @@ +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] + +## [1.0.2] - 2015-05-14 + +### Added +* Add Value::XXXMember(...) overloads for std::string (#335) + +### Fixed +* Include rapidjson.h for all internal/error headers. +* Parsing some numbers incorrectly in full-precision mode (`kFullPrecisionParseFlag`) (#342) +* Fix alignment of 64bit platforms (#328) +* Fix MemoryPoolAllocator::Clear() to clear user-buffer (0691502573f1afd3341073dd24b12c3db20fbde4) + +### Changed +* CMakeLists for include as a thirdparty in projects (#334, #337) +* Change Document::ParseStream() to use stack allocator for Reader (ffbe38614732af8e0b3abdc8b50071f386a4a685) + +## [1.0.1] - 2015-04-25 + +### Added +* Changelog following [Keep a CHANGELOG](https://github.com/olivierlacan/keep-a-changelog) suggestions. + +### Fixed +* Parsing of some numbers (e.g. "1e-00011111111111") causing assertion (#314). +* Visual C++ 32-bit compilation error in `diyfp.h` (#317). + +## [1.0.0] - 2015-04-22 + +### Added +* 100% [Coverall](https://coveralls.io/r/miloyip/rapidjson?branch=master) coverage. +* Version macros (#311) + +### Fixed +* A bug in trimming long number sequence (4824f12efbf01af72b8cb6fc96fae7b097b73015). +* Double quote in unicode escape (#288). +* Negative zero roundtrip (double only) (#289). +* Standardize behavior of `memcpy()` and `malloc()` (0c5c1538dcfc7f160e5a4aa208ddf092c787be5a, #305, 0e8bbe5e3ef375e7f052f556878be0bd79e9062d). + +### Removed +* Remove an invalid `Document::ParseInsitu()` API (e7f1c6dd08b522cfcf9aed58a333bd9a0c0ccbeb). + +## 1.0-beta - 2015-04-8 + +### Added +* RFC 7159 (#101) +* Optional Iterative Parser (#76) +* Deep-copy values (#20) +* Error code and message (#27) +* ASCII Encoding (#70) +* `kParseStopWhenDoneFlag` (#83) +* `kParseFullPrecisionFlag` (881c91d696f06b7f302af6d04ec14dd08db66ceb) +* Add `Key()` to handler concept (#134) +* C++11 compatibility and support (#128) +* Optimized number-to-string and vice versa conversions (#137, #80) +* Short-String Optimization (#131) +* Local stream optimization by traits (#32) +* Travis & Appveyor Continuous Integration, with Valgrind verification (#24, #242) +* Redo all documentation (English, Simplified Chinese) + +### Changed +* Copyright ownership transfered to THL A29 Limited (a Tencent company). +* Migrating from Premake to CMAKE (#192) +* Resolve all warning reports + +### Removed +* Remove other JSON libraries for performance comparison (#180) + +## 0.11 - 2012-11-16 + +## 0.1 - 2011-11-18 + +[Unreleased]: https://github.com/miloyip/rapidjson/compare/v1.0.2...HEAD +[1.0.2]: https://github.com/miloyip/rapidjson/compare/v1.0.1...v1.0.2 +[1.0.1]: https://github.com/miloyip/rapidjson/compare/v1.0.0...v1.0.1 +[1.0.0]: https://github.com/miloyip/rapidjson/compare/v1.0-beta...v1.0.0 diff --git a/neo/libs/rapidjson/CMakeLists.txt b/neo/libs/rapidjson/CMakeLists.txt new file mode 100644 index 00000000..68139ba3 --- /dev/null +++ b/neo/libs/rapidjson/CMakeLists.txt @@ -0,0 +1,114 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules) + +PROJECT(RapidJSON CXX) + +set(LIB_MAJOR_VERSION "1") +set(LIB_MINOR_VERSION "0") +set(LIB_PATCH_VERSION "2") +set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}") + +# compile in release with debug info mode by default +SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build Type") + +# Build all binaries in a separate directory +SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +option(RAPIDJSON_BUILD_DOC "Build rapidjson documentation." ON) +option(RAPIDJSON_BUILD_EXAMPLES "Build rapidjson examples." ON) +option(RAPIDJSON_BUILD_TESTS "Build rapidjson perftests and unittests." ON) +option(RAPIDJSON_BUILD_THIRDPARTY_GTEST + "Use gtest installation in `thirdparty/gtest` by default if available" OFF) + +option(RAPIDJSON_HAS_STDSTRING "" OFF) +if(RAPIDJSON_HAS_STDSTRING) + add_definitions(-DRAPIDJSON_HAS_STDSTRING) +endif() + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra") +elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra") +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) +endif() + +#add extra search paths for libraries and includes +SET(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The directory the headers are installed in") +SET(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE STRING "Directory where lib will install") +SET(DOC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/share/doc/${PROJECT_NAME}" CACHE PATH "Path to the documentation") + +IF(UNIX OR CYGWIN) + SET(_CMAKE_INSTALL_DIR "${LIB_INSTALL_DIR}/cmake/${PROJECT_NAME}") +ELSEIF(WIN32) + SET(_CMAKE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/cmake") +ENDIF() +SET(CMAKE_INSTALL_DIR "${_CMAKE_INSTALL_DIR}" CACHE PATH "The directory cmake fiels are installed in") + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) + +if(RAPIDJSON_BUILD_DOC) + add_subdirectory(doc) +endif() + +add_custom_target(travis_doc) +add_custom_command(TARGET travis_doc + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/travis-doxygen.sh) + +if(RAPIDJSON_BUILD_EXAMPLES) + add_subdirectory(example) +endif() + +if(RAPIDJSON_BUILD_TESTS) + if(MSVC11) + # required for VS2012 due to missing support for variadic templates + add_definitions(-D_VARIADIC_MAX=10) + endif(MSVC11) + add_subdirectory(test) + include(CTest) +endif() + +# pkg-config +IF (UNIX OR CYGWIN) + CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc + @ONLY) + INSTALL (FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc + DESTINATION "${LIB_INSTALL_DIR}/pkgconfig" + COMPONENT pkgconfig) +ENDIF() + +install(FILES readme.md + DESTINATION "${DOC_INSTALL_DIR}" + COMPONENT doc) + +install(DIRECTORY include/rapidjson + DESTINATION "${INCLUDE_INSTALL_DIR}" + COMPONENT dev) + +install(DIRECTORY example/ + DESTINATION "${DOC_INSTALL_DIR}/examples" + COMPONENT examples + # Following patterns are for excluding the intermediate/object files + # from an install of in-source CMake build. + PATTERN "CMakeFiles" EXCLUDE + PATTERN "Makefile" EXCLUDE + PATTERN "cmake_install.cmake" EXCLUDE) + +# Provide config and version files to be used by other applications +# =============================== + +export(PACKAGE ${PROJECT_NAME}) + +# cmake-modules +CONFIGURE_FILE(${PROJECT_NAME}Config.cmake.in + ${PROJECT_NAME}Config.cmake + @ONLY) +CONFIGURE_FILE(${PROJECT_NAME}ConfigVersion.cmake.in + ${PROJECT_NAME}ConfigVersion.cmake + @ONLY) +INSTALL(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + DESTINATION "${CMAKE_INSTALL_DIR}" + COMPONENT dev) diff --git a/neo/libs/rapidjson/example/CMakeLists.txt b/neo/libs/rapidjson/example/CMakeLists.txt new file mode 100644 index 00000000..64632127 --- /dev/null +++ b/neo/libs/rapidjson/example/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (c) 2011 Milo Yip (miloyip@gmail.com) +# Copyright (c) 2013 Rafal Jeczalik (rjeczalik@gmail.com) +# Distributed under the MIT License (see license.txt file) + +set(EXAMPLES + capitalize + condense + messagereader + pretty + prettyauto + serialize + simpledom + simplereader + simplewriter + tutorial) + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default") +elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default") +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) +endif() + +foreach (example ${EXAMPLES}) + add_executable(${example} ${example}/${example}.cpp) +endforeach() + +add_custom_target(examples ALL DEPENDS ${EXAMPLES}) diff --git a/neo/libs/rapidjson/example/capitalize/capitalize.cpp b/neo/libs/rapidjson/example/capitalize/capitalize.cpp new file mode 100644 index 00000000..a19ed506 --- /dev/null +++ b/neo/libs/rapidjson/example/capitalize/capitalize.cpp @@ -0,0 +1,66 @@ +// JSON condenser example + +// This example parses JSON from stdin with validation, +// and re-output the JSON content to stdout with all string capitalized, and without whitespace. + +#include "rapidjson/reader.h" +#include "rapidjson/writer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" +#include +#include + +using namespace rapidjson; + +template +struct CapitalizeFilter { + CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {} + + bool Null() { return out_.Null(); } + bool Bool(bool b) { return out_.Bool(b); } + bool Int(int i) { return out_.Int(i); } + bool Uint(unsigned u) { return out_.Uint(u); } + bool Int64(int64_t i) { return out_.Int64(i); } + bool Uint64(uint64_t u) { return out_.Uint64(u); } + bool Double(double d) { return out_.Double(d); } + bool String(const char* str, SizeType length, bool) { + buffer_.clear(); + for (SizeType i = 0; i < length; i++) + buffer_.push_back(std::toupper(str[i])); + return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string + } + bool StartObject() { return out_.StartObject(); } + bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); } + bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); } + bool StartArray() { return out_.StartArray(); } + bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); } + + OutputHandler& out_; + std::vector buffer_; + +private: + CapitalizeFilter(const CapitalizeFilter&); + CapitalizeFilter& operator=(const CapitalizeFilter&); +}; + +int main(int, char*[]) { + // Prepare JSON reader and input stream. + Reader reader; + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + + // Prepare JSON writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + Writer writer(os); + + // JSON reader parse from the input stream and let writer generate the output. + CapitalizeFilter > filter(writer); + if (!reader.Parse(is, filter)) { + fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} diff --git a/neo/libs/rapidjson/example/condense/condense.cpp b/neo/libs/rapidjson/example/condense/condense.cpp new file mode 100644 index 00000000..437168dd --- /dev/null +++ b/neo/libs/rapidjson/example/condense/condense.cpp @@ -0,0 +1,32 @@ +// JSON condenser example + +// This example parses JSON text from stdin with validation, +// and re-output the JSON content to stdout without whitespace. + +#include "rapidjson/reader.h" +#include "rapidjson/writer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" + +using namespace rapidjson; + +int main(int, char*[]) { + // Prepare JSON reader and input stream. + Reader reader; + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + + // Prepare JSON writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + Writer writer(os); + + // JSON reader parse from the input stream and let writer generate the output. + if (!reader.Parse(is, writer)) { + fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} diff --git a/neo/libs/rapidjson/example/messagereader/messagereader.cpp b/neo/libs/rapidjson/example/messagereader/messagereader.cpp new file mode 100644 index 00000000..50255b37 --- /dev/null +++ b/neo/libs/rapidjson/example/messagereader/messagereader.cpp @@ -0,0 +1,96 @@ +// Reading a message JSON with Reader (SAX-style API). +// The JSON should be an object with key-string pairs. + +#include "rapidjson/reader.h" +#include "rapidjson/error/en.h" +#include +#include +#include + +using namespace std; +using namespace rapidjson; + +typedef map MessageMap; + +#if defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +struct MessageHandler + : public BaseReaderHandler, MessageHandler> { + MessageHandler() : messages_(), state_(kExpectObjectStart), name_() {} + + bool StartObject() { + switch (state_) { + case kExpectObjectStart: + state_ = kExpectNameOrObjectEnd; + return true; + default: + return false; + } + } + + bool String(const char* str, SizeType length, bool) { + switch (state_) { + case kExpectNameOrObjectEnd: + name_ = string(str, length); + state_ = kExpectValue; + return true; + case kExpectValue: + messages_.insert(MessageMap::value_type(name_, string(str, length))); + state_ = kExpectNameOrObjectEnd; + return true; + default: + return false; + } + } + + bool EndObject(SizeType) { return state_ == kExpectNameOrObjectEnd; } + + bool Default() { return false; } // All other events are invalid. + + MessageMap messages_; + enum State { + kExpectObjectStart, + kExpectNameOrObjectEnd, + kExpectValue + }state_; + std::string name_; +}; + +#if defined(__GNUC__) +RAPIDJSON_DIAG_POP +#endif + +void ParseMessages(const char* json, MessageMap& messages) { + Reader reader; + MessageHandler handler; + StringStream ss(json); + if (reader.Parse(ss, handler)) + messages.swap(handler.messages_); // Only change it if success. + else { + ParseErrorCode e = reader.GetParseErrorCode(); + size_t o = reader.GetErrorOffset(); + cout << "Error: " << GetParseError_En(e) << endl;; + cout << " at offset " << o << " near '" << string(json).substr(o, 10) << "...'" << endl; + } +} + +int main() { + MessageMap messages; + + const char* json1 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\" }"; + cout << json1 << endl; + ParseMessages(json1, messages); + + for (MessageMap::const_iterator itr = messages.begin(); itr != messages.end(); ++itr) + cout << itr->first << ": " << itr->second << endl; + + cout << endl << "Parse a JSON with invalid schema." << endl; + const char* json2 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\", \"foo\" : {} }"; + cout << json2 << endl; + ParseMessages(json2, messages); + + return 0; +} diff --git a/neo/libs/rapidjson/example/pretty/pretty.cpp b/neo/libs/rapidjson/example/pretty/pretty.cpp new file mode 100644 index 00000000..81ca3413 --- /dev/null +++ b/neo/libs/rapidjson/example/pretty/pretty.cpp @@ -0,0 +1,30 @@ +// JSON pretty formatting example +// This example can only handle UTF-8. For handling other encodings, see prettyauto example. + +#include "rapidjson/reader.h" +#include "rapidjson/prettywriter.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" + +using namespace rapidjson; + +int main(int, char*[]) { + // Prepare reader and input stream. + Reader reader; + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + + // Prepare writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + PrettyWriter writer(os); + + // JSON reader parse from the input stream and let writer generate the output. + if (!reader.Parse(is, writer)) { + fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} diff --git a/neo/libs/rapidjson/example/prettyauto/prettyauto.cpp b/neo/libs/rapidjson/example/prettyauto/prettyauto.cpp new file mode 100644 index 00000000..1699a10d --- /dev/null +++ b/neo/libs/rapidjson/example/prettyauto/prettyauto.cpp @@ -0,0 +1,56 @@ +// JSON pretty formatting example +// This example can handle UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE. +// The input firstly convert to UTF8, and then write to the original encoding with pretty formatting. + +#include "rapidjson/reader.h" +#include "rapidjson/prettywriter.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/encodedstream.h" // NEW +#include "rapidjson/error/en.h" +#ifdef _WIN32 +#include +#include +#endif + +using namespace rapidjson; + +int main(int, char*[]) { +#ifdef _WIN32 + // Prevent Windows converting between CR+LF and LF + _setmode(_fileno(stdin), _O_BINARY); // NEW + _setmode(_fileno(stdout), _O_BINARY); // NEW +#endif + + // Prepare reader and input stream. + //Reader reader; + GenericReader, UTF8<> > reader; // CHANGED + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + AutoUTFInputStream eis(is); // NEW + + // Prepare writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + +#if 1 + // Use the same Encoding of the input. Also use BOM according to input. + typedef AutoUTFOutputStream OutputStream; // NEW + OutputStream eos(os, eis.GetType(), eis.HasBOM()); // NEW + PrettyWriter, AutoUTF > writer(eos); // CHANGED +#else + // You may also use static bound encoding type, such as output to UTF-16LE with BOM + typedef EncodedOutputStream,FileWriteStream> OutputStream; // NEW + OutputStream eos(os, true); // NEW + PrettyWriter, UTF16LE<> > writer(eos); // CHANGED +#endif + + // JSON reader parse from the input stream and let writer generate the output. + //if (!reader.Parse(is, writer)) { + if (!reader.Parse(eis, writer)) { // CHANGED + fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} diff --git a/neo/libs/rapidjson/example/serialize/serialize.cpp b/neo/libs/rapidjson/example/serialize/serialize.cpp new file mode 100644 index 00000000..261f0f02 --- /dev/null +++ b/neo/libs/rapidjson/example/serialize/serialize.cpp @@ -0,0 +1,157 @@ +// Serialize example +// This example shows writing JSON string with writer directly. + +#include "rapidjson/prettywriter.h" // for stringify JSON +#include +#include +#include + +using namespace rapidjson; + +class Person { +public: + Person(const std::string& name, unsigned age) : name_(name), age_(age) {} + virtual ~Person(); + +protected: + template + void Serialize(Writer& writer) const { + // This base class just write out name-value pairs, without wrapping within an object. + writer.String("name"); +#ifdef RAPIDJSON_HAS_STDSTRING + writer.String(name_); +#else + writer.String(name_.c_str(), (SizeType)name_.length()); // Supplying length of string is faster. +#endif + writer.String("age"); + writer.Uint(age_); + } + +private: + std::string name_; + unsigned age_; +}; + +Person::~Person() { +} + +class Education { +public: + Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {} + + template + void Serialize(Writer& writer) const { + writer.StartObject(); + + writer.String("school"); +#ifdef RAPIDJSON_HAS_STDSTRING + writer.String(school_); +#else + writer.String(school_.c_str(), (SizeType)school_.length()); +#endif + + writer.String("GPA"); + writer.Double(GPA_); + + writer.EndObject(); + } + +private: + std::string school_; + double GPA_; +}; + +class Dependent : public Person { +public: + Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {} + Dependent(const Dependent& rhs) : Person(rhs), education_(0) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); } + virtual ~Dependent(); + + Dependent& operator=(const Dependent& rhs) { + if (this == &rhs) + return *this; + delete education_; + education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); + return *this; + } + + template + void Serialize(Writer& writer) const { + writer.StartObject(); + + Person::Serialize(writer); + + writer.String("education"); + if (education_) + education_->Serialize(writer); + else + writer.Null(); + + writer.EndObject(); + } + +private: + + Education *education_; +}; + +Dependent::~Dependent() { + delete education_; +} + +class Employee : public Person { +public: + Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married) {} + virtual ~Employee(); + + void AddDependent(const Dependent& dependent) { + dependents_.push_back(dependent); + } + + template + void Serialize(Writer& writer) const { + writer.StartObject(); + + Person::Serialize(writer); + + writer.String("married"); + writer.Bool(married_); + + writer.String(("dependents")); + writer.StartArray(); + for (std::vector::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr) + dependentItr->Serialize(writer); + writer.EndArray(); + + writer.EndObject(); + } + +private: + std::vector dependents_; + bool married_; +}; + +Employee::~Employee() { +} + +int main(int, char*[]) { + std::vector employees; + + employees.push_back(Employee("Milo YIP", 34, true)); + employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5))); + employees.back().AddDependent(Dependent("Mio YIP", 1)); + + employees.push_back(Employee("Percy TSE", 30, false)); + + StringBuffer sb; + PrettyWriter writer(sb); + + writer.StartArray(); + for (std::vector::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr) + employeeItr->Serialize(writer); + writer.EndArray(); + + puts(sb.GetString()); + + return 0; +} diff --git a/neo/libs/rapidjson/example/simpledom/simpledom.cpp b/neo/libs/rapidjson/example/simpledom/simpledom.cpp new file mode 100644 index 00000000..80384199 --- /dev/null +++ b/neo/libs/rapidjson/example/simpledom/simpledom.cpp @@ -0,0 +1,29 @@ +// JSON simple example +// This example does not handle errors. + +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include + +using namespace rapidjson; + +int main() { + // 1. Parse a JSON string into DOM. + const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; + Document d; + d.Parse(json); + + // 2. Modify it by DOM. + Value& s = d["stars"]; + s.SetInt(s.GetInt() + 1); + + // 3. Stringify the DOM + StringBuffer buffer; + Writer writer(buffer); + d.Accept(writer); + + // Output {"project":"rapidjson","stars":11} + std::cout << buffer.GetString() << std::endl; + return 0; +} diff --git a/neo/libs/rapidjson/example/simplereader/simplereader.cpp b/neo/libs/rapidjson/example/simplereader/simplereader.cpp new file mode 100644 index 00000000..edbdb635 --- /dev/null +++ b/neo/libs/rapidjson/example/simplereader/simplereader.cpp @@ -0,0 +1,38 @@ +#include "rapidjson/reader.h" +#include + +using namespace rapidjson; +using namespace std; + +struct MyHandler { + bool Null() { cout << "Null()" << endl; return true; } + bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; } + bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; } + bool Uint(unsigned u) { cout << "Uint(" << u << ")" << endl; return true; } + bool Int64(int64_t i) { cout << "Int64(" << i << ")" << endl; return true; } + bool Uint64(uint64_t u) { cout << "Uint64(" << u << ")" << endl; return true; } + bool Double(double d) { cout << "Double(" << d << ")" << endl; return true; } + bool String(const char* str, SizeType length, bool copy) { + cout << "String(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl; + return true; + } + bool StartObject() { cout << "StartObject()" << endl; return true; } + bool Key(const char* str, SizeType length, bool copy) { + cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl; + return true; + } + bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; } + bool StartArray() { cout << "StartArray()" << endl; return true; } + bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; } +}; + +int main() { + const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; + + MyHandler handler; + Reader reader; + StringStream ss(json); + reader.Parse(ss, handler); + + return 0; +} diff --git a/neo/libs/rapidjson/example/simplewriter/simplewriter.cpp b/neo/libs/rapidjson/example/simplewriter/simplewriter.cpp new file mode 100644 index 00000000..f8891504 --- /dev/null +++ b/neo/libs/rapidjson/example/simplewriter/simplewriter.cpp @@ -0,0 +1,35 @@ +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include + +using namespace rapidjson; +using namespace std; + +int main() { + StringBuffer s; + Writer writer(s); + + writer.StartObject(); + writer.String("hello"); + writer.String("world"); + writer.String("t"); + writer.Bool(true); + writer.String("f"); + writer.Bool(false); + writer.String("n"); + writer.Null(); + writer.String("i"); + writer.Uint(123); + writer.String("pi"); + writer.Double(3.1416); + writer.String("a"); + writer.StartArray(); + for (unsigned i = 0; i < 4; i++) + writer.Uint(i); + writer.EndArray(); + writer.EndObject(); + + cout << s.GetString() << endl; + + return 0; +} diff --git a/neo/libs/rapidjson/example/tutorial/tutorial.cpp b/neo/libs/rapidjson/example/tutorial/tutorial.cpp new file mode 100644 index 00000000..7e3dbd28 --- /dev/null +++ b/neo/libs/rapidjson/example/tutorial/tutorial.cpp @@ -0,0 +1,151 @@ +// Hello World example +// This example shows basic usage of DOM-style API. + +#include "rapidjson/document.h" // rapidjson's DOM-style API +#include "rapidjson/prettywriter.h" // for stringify JSON +#include + +using namespace rapidjson; +using namespace std; + +int main(int, char*[]) { + //////////////////////////////////////////////////////////////////////////// + // 1. Parse a JSON text string to a document. + + const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; + printf("Original JSON:\n %s\n", json); + + Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator. + +#if 0 + // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream(). + if (document.Parse(json).HasParseError()) + return 1; +#else + // In-situ parsing, decode strings directly in the source string. Source must be string. + char buffer[sizeof(json)]; + memcpy(buffer, json, sizeof(json)); + if (document.ParseInsitu(buffer).HasParseError()) + return 1; +#endif + + printf("\nParsing to document succeeded.\n"); + + //////////////////////////////////////////////////////////////////////////// + // 2. Access values in document. + + printf("\nAccess values in document:\n"); + assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array. + + assert(document.HasMember("hello")); + assert(document["hello"].IsString()); + printf("hello = %s\n", document["hello"].GetString()); + + // Since version 0.2, you can use single lookup to check the existing of member and its value: + Value::MemberIterator hello = document.FindMember("hello"); + assert(hello != document.MemberEnd()); + assert(hello->value.IsString()); + assert(strcmp("world", hello->value.GetString()) == 0); + (void)hello; + + assert(document["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue(). + printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); + + assert(document["f"].IsBool()); + printf("f = %s\n", document["f"].GetBool() ? "true" : "false"); + + printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); + + assert(document["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type. + assert(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true. + printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"] + + assert(document["pi"].IsNumber()); + assert(document["pi"].IsDouble()); + printf("pi = %g\n", document["pi"].GetDouble()); + + { + const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster. + assert(a.IsArray()); + for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t. + printf("a[%d] = %d\n", i, a[i].GetInt()); + + int y = a[0].GetInt(); + (void)y; + + // Iterating array with iterators + printf("a = "); + for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) + printf("%d ", itr->GetInt()); + printf("\n"); + } + + // Iterating object members + static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" }; + for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) + printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]); + + //////////////////////////////////////////////////////////////////////////// + // 3. Modify values in document. + + // Change i to a bigger number + { + uint64_t f20 = 1; // compute factorial of 20 + for (uint64_t j = 1; j <= 20; j++) + f20 *= j; + document["i"] = f20; // Alternate form: document["i"].SetUint64(f20) + assert(!document["i"].IsInt()); // No longer can be cast as int or uint. + } + + // Adding values to array. + { + Value& a = document["a"]; // This time we uses non-const reference. + Document::AllocatorType& allocator = document.GetAllocator(); + for (int i = 5; i <= 10; i++) + a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's. + + // Fluent API + a.PushBack("Lua", allocator).PushBack("Mio", allocator); + } + + // Making string values. + + // This version of SetString() just store the pointer to the string. + // So it is for literal and string that exists within value's life-cycle. + { + document["hello"] = "rapidjson"; // This will invoke strlen() + // Faster version: + // document["hello"].SetString("rapidjson", 9); + } + + // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer. + Value author; + { + char buffer[10]; + int len = sprintf(buffer, "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string. + + author.SetString(buffer, static_cast(len), document.GetAllocator()); + // Shorter but slower version: + // document["hello"].SetString(buffer, document.GetAllocator()); + + // Constructor version: + // Value author(buffer, len, document.GetAllocator()); + // Value author(buffer, document.GetAllocator()); + memset(buffer, 0, sizeof(buffer)); // For demonstration purpose. + } + // Variable 'buffer' is unusable now but 'author' has already made a copy. + document.AddMember("author", author, document.GetAllocator()); + + assert(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null. + + //////////////////////////////////////////////////////////////////////////// + // 4. Stringify JSON + + printf("\nModified JSON with reformatting:\n"); + StringBuffer sb; + PrettyWriter writer(sb); + document.Accept(writer); // Accept() traverses the DOM and generates Handler events. + puts(sb.GetString()); + + return 0; +} diff --git a/neo/libs/rapidjson/include/rapidjson/allocators.h b/neo/libs/rapidjson/include/rapidjson/allocators.h new file mode 100644 index 00000000..d74a6715 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/allocators.h @@ -0,0 +1,261 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ALLOCATORS_H_ +#define RAPIDJSON_ALLOCATORS_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Allocator + +/*! \class rapidjson::Allocator + \brief Concept for allocating, resizing and freeing memory block. + + Note that Malloc() and Realloc() are non-static but Free() is static. + + So if an allocator need to support Free(), it needs to put its pointer in + the header of memory block. + +\code +concept Allocator { + static const bool kNeedFree; //!< Whether this allocator needs to call Free(). + + // Allocate a memory block. + // \param size of the memory block in bytes. + // \returns pointer to the memory block. + void* Malloc(size_t size); + + // Resize a memory block. + // \param originalPtr The pointer to current memory block. Null pointer is permitted. + // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) + // \param newSize the new size in bytes. + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); + + // Free a memory block. + // \param pointer to the memory block. Null pointer is permitted. + static void Free(void *ptr); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// CrtAllocator + +//! C-runtime library allocator. +/*! This class is just wrapper for standard C library memory routines. + \note implements Allocator concept +*/ +class CrtAllocator { +public: + static const bool kNeedFree = true; + void* Malloc(size_t size) { + if (size) // behavior of malloc(0) is implementation defined. + return std::malloc(size); + else + return NULL; // standardize to returning NULL. + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + (void)originalSize; + if (newSize == 0) { + std::free(originalPtr); + return NULL; + } + return std::realloc(originalPtr, newSize); + } + static void Free(void *ptr) { std::free(ptr); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// MemoryPoolAllocator + +//! Default memory allocator used by the parser and DOM. +/*! This allocator allocate memory blocks from pre-allocated memory chunks. + + It does not free memory blocks. And Realloc() only allocate new memory. + + The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. + + User may also supply a buffer as the first chunk. + + If the user-buffer is full then additional chunks are allocated by BaseAllocator. + + The user-buffer is not deallocated by this allocator. + + \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. + \note implements Allocator concept +*/ +template +class MemoryPoolAllocator { +public: + static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) + + //! Constructor with chunkSize. + /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + } + + //! Constructor with user-supplied buffer. + /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. + + The user buffer will not be deallocated when this allocator is destructed. + + \param buffer User supplied buffer. + \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). + \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + RAPIDJSON_ASSERT(buffer != 0); + RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); + chunkHead_ = reinterpret_cast(buffer); + chunkHead_->capacity = size - sizeof(ChunkHeader); + chunkHead_->size = 0; + chunkHead_->next = 0; + } + + //! Destructor. + /*! This deallocates all memory chunks, excluding the user-supplied buffer. + */ + ~MemoryPoolAllocator() { + Clear(); + RAPIDJSON_DELETE(ownBaseAllocator_); + } + + //! Deallocates all memory chunks, excluding the user-supplied buffer. + void Clear() { + while (chunkHead_ && chunkHead_ != userBuffer_) { + ChunkHeader* next = chunkHead_->next; + baseAllocator_->Free(chunkHead_); + chunkHead_ = next; + } + if (chunkHead_ && chunkHead_ == userBuffer_) + chunkHead_->size = 0; // Clear user buffer + } + + //! Computes the total capacity of allocated memory chunks. + /*! \return total capacity in bytes. + */ + size_t Capacity() const { + size_t capacity = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + capacity += c->capacity; + return capacity; + } + + //! Computes the memory blocks allocated. + /*! \return total used bytes. + */ + size_t Size() const { + size_t size = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + size += c->size; + return size; + } + + //! Allocates a memory block. (concept Allocator) + void* Malloc(size_t size) { + if (!size) + return NULL; + + size = RAPIDJSON_ALIGN(size); + if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) + AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size); + + void *buffer = reinterpret_cast(chunkHead_ + 1) + chunkHead_->size; + chunkHead_->size += size; + return buffer; + } + + //! Resizes a memory block (concept Allocator) + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + if (originalPtr == 0) + return Malloc(newSize); + + if (newSize == 0) + return NULL; + + // Do not shrink if new size is smaller than original + if (originalSize >= newSize) + return originalPtr; + + // Simply expand it if it is the last allocation and there is sufficient space + if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) { + size_t increment = static_cast(newSize - originalSize); + increment = RAPIDJSON_ALIGN(increment); + if (chunkHead_->size + increment <= chunkHead_->capacity) { + chunkHead_->size += increment; + return originalPtr; + } + } + + // Realloc process: allocate and copy memory, do not free original buffer. + void* newBuffer = Malloc(newSize); + RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly. + if (originalSize) + std::memcpy(newBuffer, originalPtr, originalSize); + return newBuffer; + } + + //! Frees a memory block (concept Allocator) + static void Free(void *ptr) { (void)ptr; } // Do nothing + +private: + //! Copy constructor is not permitted. + MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; + //! Copy assignment operator is not permitted. + MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; + + //! Creates a new chunk. + /*! \param capacity Capacity of the chunk in bytes. + */ + void AddChunk(size_t capacity) { + if (!baseAllocator_) + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); + ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity)); + chunk->capacity = capacity; + chunk->size = 0; + chunk->next = chunkHead_; + chunkHead_ = chunk; + } + + static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. + + //! Chunk header for perpending to each chunk. + /*! Chunks are stored as a singly linked list. + */ + struct ChunkHeader { + size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). + size_t size; //!< Current size of allocated memory in bytes. + ChunkHeader *next; //!< Next chunk in the linked list. + }; + + ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. + size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. + void *userBuffer_; //!< User supplied buffer. + BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. + BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/document.h b/neo/libs/rapidjson/include/rapidjson/document.h new file mode 100644 index 00000000..c6acbd90 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/document.h @@ -0,0 +1,2014 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_DOCUMENT_H_ +#define RAPIDJSON_DOCUMENT_H_ + +/*! \file document.h */ + +#include "reader.h" +#include "internal/meta.h" +#include "internal/strfunc.h" +#include // placement new + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +#elif defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_HAS_STDSTRING + +#ifndef RAPIDJSON_HAS_STDSTRING +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation +#else +#define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default +#endif +/*! \def RAPIDJSON_HAS_STDSTRING + \ingroup RAPIDJSON_CONFIG + \brief Enable RapidJSON support for \c std::string + + By defining this preprocessor symbol to \c 1, several convenience functions for using + \ref rapidjson::GenericValue with \c std::string are enabled, especially + for construction and comparison. + + \hideinitializer +*/ +#endif // !defined(RAPIDJSON_HAS_STDSTRING) + +#if RAPIDJSON_HAS_STDSTRING +#include +#endif // RAPIDJSON_HAS_STDSTRING + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS +#include // std::iterator, std::random_access_iterator_tag +#endif + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +// Forward declaration. +template +class GenericValue; + +//! Name-value pair in a JSON object value. +/*! + This class was internal to GenericValue. It used to be a inner struct. + But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct. + https://code.google.com/p/rapidjson/issues/detail?id=64 +*/ +template +struct GenericMember { + GenericValue name; //!< name of member (must be a string) + GenericValue value; //!< value of member. +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericMemberIterator + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS + +//! (Constant) member iterator for a JSON object value +/*! + \tparam Const Is this a constant iterator? + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. + + This class implements a Random Access Iterator for GenericMember elements + of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements]. + + \note This iterator implementation is mainly intended to avoid implicit + conversions from iterator values to \c NULL, + e.g. from GenericValue::FindMember. + + \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a + pointer-based implementation, if your platform doesn't provide + the C++ header. + + \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator + */ +template +class GenericMemberIterator + : public std::iterator >::Type> { + + friend class GenericValue; + template friend class GenericMemberIterator; + + typedef GenericMember PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef std::iterator BaseType; + +public: + //! Iterator type itself + typedef GenericMemberIterator Iterator; + //! Constant iterator type + typedef GenericMemberIterator ConstIterator; + //! Non-constant iterator type + typedef GenericMemberIterator NonConstIterator; + + //! Pointer to (const) GenericMember + typedef typename BaseType::pointer Pointer; + //! Reference to (const) GenericMember + typedef typename BaseType::reference Reference; + //! Signed integer type (e.g. \c ptrdiff_t) + typedef typename BaseType::difference_type DifferenceType; + + //! Default constructor (singular value) + /*! Creates an iterator pointing to no element. + \note All operations, except for comparisons, are undefined on such values. + */ + GenericMemberIterator() : ptr_() {} + + //! Iterator conversions to more const + /*! + \param it (Non-const) iterator to copy from + + Allows the creation of an iterator from another GenericMemberIterator + that is "less const". Especially, creating a non-constant iterator + from a constant iterator are disabled: + \li const -> non-const (not ok) + \li const -> const (ok) + \li non-const -> const (ok) + \li non-const -> non-const (ok) + + \note If the \c Const template parameter is already \c false, this + constructor effectively defines a regular copy-constructor. + Otherwise, the copy constructor is implicitly defined. + */ + GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {} + + //! @name stepping + //@{ + Iterator& operator++(){ ++ptr_; return *this; } + Iterator& operator--(){ --ptr_; return *this; } + Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; } + Iterator operator--(int){ Iterator old(*this); --ptr_; return old; } + //@} + + //! @name increment/decrement + //@{ + Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); } + Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); } + + Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; } + Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; } + //@} + + //! @name relations + //@{ + bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; } + bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; } + bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; } + bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; } + bool operator< (ConstIterator that) const { return ptr_ < that.ptr_; } + bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; } + //@} + + //! @name dereference + //@{ + Reference operator*() const { return *ptr_; } + Pointer operator->() const { return ptr_; } + Reference operator[](DifferenceType n) const { return ptr_[n]; } + //@} + + //! Distance + DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; } + +private: + //! Internal constructor from plain pointer + explicit GenericMemberIterator(Pointer p) : ptr_(p) {} + + Pointer ptr_; //!< raw pointer +}; + +#else // RAPIDJSON_NOMEMBERITERATORCLASS + +// class-based member iterator implementation disabled, use plain pointers + +template +struct GenericMemberIterator; + +//! non-const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain pointer as iterator type + typedef GenericMember* Iterator; +}; +//! const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain const pointer as iterator type + typedef const GenericMember* Iterator; +}; + +#endif // RAPIDJSON_NOMEMBERITERATORCLASS + +/////////////////////////////////////////////////////////////////////////////// +// GenericStringRef + +//! Reference to a constant string (not taking a copy) +/*! + \tparam CharType character type of the string + + This helper class is used to automatically infer constant string + references for string literals, especially from \c const \b (!) + character arrays. + + The main use is for creating JSON string values without copying the + source string via an \ref Allocator. This requires that the referenced + string pointers have a sufficient lifetime, which exceeds the lifetime + of the associated GenericValue. + + \b Example + \code + Value v("foo"); // ok, no need to copy & calculate length + const char foo[] = "foo"; + v.SetString(foo); // ok + + const char* bar = foo; + // Value x(bar); // not ok, can't rely on bar's lifetime + Value x(StringRef(bar)); // lifetime explicitly guaranteed by user + Value y(StringRef(bar, 3)); // ok, explicitly pass length + \endcode + + \see StringRef, GenericValue::SetString +*/ +template +struct GenericStringRef { + typedef CharType Ch; //!< character type of the string + + //! Create string reference from \c const character array + /*! + This constructor implicitly creates a constant string reference from + a \c const character array. It has better performance than + \ref StringRef(const CharType*) by inferring the string \ref length + from the array length, and also supports strings containing null + characters. + + \tparam N length of the string, automatically inferred + + \param str Constant character array, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note Constant complexity. + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ + template + GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT + : s(str), length(N-1) {} + + //! Explicitly create string reference from \c const character pointer + /*! + This constructor can be used to \b explicitly create a reference to + a constant string pointer. + + \see StringRef(const CharType*) + + \param str Constant character pointer, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ + explicit GenericStringRef(const CharType* str) + : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != NULL); } + + //! Create constant string reference from pointer and length + /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param len length of the string, excluding the trailing NULL terminator + + \post \ref s == str && \ref length == len + \note Constant complexity. + */ + GenericStringRef(const CharType* str, SizeType len) + : s(str), length(len) { RAPIDJSON_ASSERT(s != NULL); } + + //! implicit conversion to plain CharType pointer + operator const Ch *() const { return s; } + + const Ch* const s; //!< plain CharType pointer + const SizeType length; //!< length of the string (excluding the trailing NULL terminator) + +private: + //! Disallow copy-assignment + GenericStringRef operator=(const GenericStringRef&); + //! Disallow construction from non-const array + template + GenericStringRef(CharType (&str)[N]) /* = delete */; +}; + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + \tparam CharType Character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + + \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember +*/ +template +inline GenericStringRef StringRef(const CharType* str) { + return GenericStringRef(str, internal::StrLen(str)); +} + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + This version has better performance with supplied length, and also + supports string containing null characters. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param length The length of source string. + \return GenericStringRef string reference object + \relatesalso GenericStringRef +*/ +template +inline GenericStringRef StringRef(const CharType* str, size_t length) { + return GenericStringRef(str, SizeType(length)); +} + +#if RAPIDJSON_HAS_STDSTRING +//! Mark a string object as constant string +/*! Mark a string object (e.g. \c std::string) as a "string literal". + This function can be used to avoid copying a string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. +*/ +template +inline GenericStringRef StringRef(const std::basic_string& str) { + return GenericStringRef(str.data(), SizeType(str.size())); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue type traits +namespace internal { + +template +struct IsGenericValueImpl : FalseType {}; + +// select candidates according to nested encoding and allocator types +template struct IsGenericValueImpl::Type, typename Void::Type> + : IsBaseOf, T>::Type {}; + +// helper to match arbitrary GenericValue instantiations, including derived classes +template struct IsGenericValue : IsGenericValueImpl::Type {}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue + +//! Represents a JSON value. Use Value for UTF8 encoding and default allocator. +/*! + A JSON value can be one of 7 types. This class is a variant type supporting + these types. + + Use the Value if UTF8 and default allocator + + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. +*/ +template > +class GenericValue { +public: + //! Name-value pair in an object. + typedef GenericMember Member; + typedef Encoding EncodingType; //!< Encoding type from template parameter. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericStringRef StringRefType; //!< Reference to a constant string + typedef typename GenericMemberIterator::Iterator MemberIterator; //!< Member iterator for iterating in object. + typedef typename GenericMemberIterator::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object. + typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. + typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. + typedef GenericValue ValueType; //!< Value type of itself. + + //!@name Constructors and destructor. + //@{ + + //! Default constructor creates a null value. + GenericValue() RAPIDJSON_NOEXCEPT : data_(), flags_(kNullFlag) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_), flags_(rhs.flags_) { + rhs.flags_ = kNullFlag; // give up contents + } +#endif + +private: + //! Copy constructor is not permitted. + GenericValue(const GenericValue& rhs); + +public: + + //! Constructor with JSON value type. + /*! This creates a Value of specified type with default content. + \param type Type of the value. + \note Default content for number is zero. + */ + explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_(), flags_() { + static const unsigned defaultFlags[7] = { + kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, + kNumberAnyFlag + }; + RAPIDJSON_ASSERT(type <= kNumberType); + flags_ = defaultFlags[type]; + + // Use ShortString to store empty string. + if (type == kStringType) + data_.ss.SetLength(0); + } + + //! Explicit copy constructor (with allocator) + /*! Creates a copy of a Value by using the given Allocator + \tparam SourceAllocator allocator of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). + \see CopyFrom() + */ + template< typename SourceAllocator > + GenericValue(const GenericValue& rhs, Allocator & allocator); + + //! Constructor for boolean value. + /*! \param b Boolean value + \note This constructor is limited to \em real boolean values and rejects + implicitly converted types like arbitrary pointers. Use an explicit cast + to \c bool, if you want to construct a boolean JSON value in such cases. + */ +#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen + template + explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame))) RAPIDJSON_NOEXCEPT +#else + explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT +#endif + : data_(), flags_(b ? kTrueFlag : kFalseFlag) { + // safe-guard against failing SFINAE + RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value)); + } + + //! Constructor for int value. + explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberIntFlag) { + data_.n.i64 = i; + if (i >= 0) + flags_ |= kUintFlag | kUint64Flag; + } + + //! Constructor for unsigned value. + explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUintFlag) { + data_.n.u64 = u; + if (!(u & 0x80000000)) + flags_ |= kIntFlag | kInt64Flag; + } + + //! Constructor for int64_t value. + explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberInt64Flag) { + data_.n.i64 = i64; + if (i64 >= 0) { + flags_ |= kNumberUint64Flag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + flags_ |= kUintFlag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + flags_ |= kIntFlag; + } + else if (i64 >= static_cast(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + flags_ |= kIntFlag; + } + + //! Constructor for uint64_t value. + explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUint64Flag) { + data_.n.u64 = u64; + if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))) + flags_ |= kInt64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + flags_ |= kUintFlag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + flags_ |= kIntFlag; + } + + //! Constructor for double value. + explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; } + + //! Constructor for constant string (i.e. do not make a copy of string) + GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(StringRef(s, length)); } + + //! Constructor for constant string (i.e. do not make a copy of string) + explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(s); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s, length), allocator); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch*s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor for copy-string from a string object (i.e. do make a copy of string) + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue(const std::basic_string& s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); } +#endif + + //! Destructor. + /*! Need to destruct elements of array, members of object, or copy-string. + */ + ~GenericValue() { + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + switch(flags_) { + case kArrayFlag: + for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v) + v->~GenericValue(); + Allocator::Free(data_.a.elements); + break; + + case kObjectFlag: + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + Allocator::Free(data_.o.members); + break; + + case kCopyStringFlag: + Allocator::Free(const_cast(data_.s.str)); + break; + + default: + break; // Do nothing for other types. + } + } + } + + //@} + + //!@name Assignment operators + //@{ + + //! Assignment with move semantics. + /*! \param rhs Source of the assignment. It will become a null value after assignment. + */ + GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + RAPIDJSON_ASSERT(this != &rhs); + this->~GenericValue(); + RawAssign(rhs); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT { + return *this = rhs.Move(); + } +#endif + + //! Assignment of constant string reference (no copy) + /*! \param str Constant string reference to be assigned + \note This overload is needed to avoid clashes with the generic primitive type assignment overload below. + \see GenericStringRef, operator=(T) + */ + GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT { + GenericValue s(str); + return *this = s; + } + + //! Assignment with primitive types. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value The value to be assigned. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref SetString(const Ch*, Allocator&) (for copying) or + \ref StringRef() (to explicitly mark the pointer as constant) instead. + All other pointer types would implicitly convert to \c bool, + use \ref SetBool() instead. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue&)) + operator=(T value) { + GenericValue v(value); + return *this = v; + } + + //! Deep-copy assignment from Value + /*! Assigns a \b copy of the Value to the current Value object + \tparam SourceAllocator Allocator type of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator to use for copying + */ + template + GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator) { + RAPIDJSON_ASSERT((void*)this != (void const*)&rhs); + this->~GenericValue(); + new (this) GenericValue(rhs, allocator); + return *this; + } + + //! Exchange the contents of this value with those of other. + /*! + \param other Another value. + \note Constant complexity. + */ + GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT { + GenericValue temp; + temp.RawAssign(*this); + RawAssign(other); + other.RawAssign(temp); + return *this; + } + + //! Prepare Value for move semantics + /*! \return *this */ + GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } + //@} + + //!@name Equal-to and not-equal-to operators + //@{ + //! Equal-to operator + /*! + \note If an object contains duplicated named member, comparing equality with any object is always \c false. + \note Linear time complexity (number of all values in the subtree and total lengths of all strings). + */ + template + bool operator==(const GenericValue& rhs) const { + typedef GenericValue RhsType; + if (GetType() != rhs.GetType()) + return false; + + switch (GetType()) { + case kObjectType: // Warning: O(n^2) inner-loop + if (data_.o.size != rhs.data_.o.size) + return false; + for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) { + typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name); + if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value) + return false; + } + return true; + + case kArrayType: + if (data_.a.size != rhs.data_.a.size) + return false; + for (SizeType i = 0; i < data_.a.size; i++) + if ((*this)[i] != rhs[i]) + return false; + return true; + + case kStringType: + return StringEqual(rhs); + + case kNumberType: + if (IsDouble() || rhs.IsDouble()) { + double a = GetDouble(); // May convert from integer to double. + double b = rhs.GetDouble(); // Ditto + return a >= b && a <= b; // Prevent -Wfloat-equal + } + else + return data_.n.u64 == rhs.data_.n.u64; + + default: // kTrueType, kFalseType, kNullType + return true; + } + } + + //! Equal-to operator with const C-string pointer + bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); } + +#if RAPIDJSON_HAS_STDSTRING + //! Equal-to operator with string object + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + bool operator==(const std::basic_string& rhs) const { return *this == GenericValue(StringRef(rhs)); } +#endif + + //! Equal-to operator with primitive types + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } + + //! Not-equal-to operator + /*! \return !(*this == rhs) + */ + template + bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with const C-string pointer + bool operator!=(const Ch* rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with arbitrary types + /*! \return !(*this == rhs) + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } + + //! Equal-to operator with arbitrary types (symmetric version) + /*! \return (rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } + + //! Not-Equal-to operator with arbitrary types (symmetric version) + /*! \return !(rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } + //@} + + //!@name Type + //@{ + + Type GetType() const { return static_cast(flags_ & kTypeMask); } + bool IsNull() const { return flags_ == kNullFlag; } + bool IsFalse() const { return flags_ == kFalseFlag; } + bool IsTrue() const { return flags_ == kTrueFlag; } + bool IsBool() const { return (flags_ & kBoolFlag) != 0; } + bool IsObject() const { return flags_ == kObjectFlag; } + bool IsArray() const { return flags_ == kArrayFlag; } + bool IsNumber() const { return (flags_ & kNumberFlag) != 0; } + bool IsInt() const { return (flags_ & kIntFlag) != 0; } + bool IsUint() const { return (flags_ & kUintFlag) != 0; } + bool IsInt64() const { return (flags_ & kInt64Flag) != 0; } + bool IsUint64() const { return (flags_ & kUint64Flag) != 0; } + bool IsDouble() const { return (flags_ & kDoubleFlag) != 0; } + bool IsString() const { return (flags_ & kStringFlag) != 0; } + + //@} + + //!@name Null + //@{ + + GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; } + + //@} + + //!@name Bool + //@{ + + bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return flags_ == kTrueFlag; } + //!< Set boolean value + /*! \post IsBool() == true */ + GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; } + + //@} + + //!@name Object + //@{ + + //! Set this value as an empty object. + /*! \post IsObject() == true */ + GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } + + //! Get the number of members in the object. + SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } + + //! Check whether the object is empty. + bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType)) + \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. + Since 0.2, if the name is not correct, it will assert. + If user is unsure whether a member exists, user should use HasMember() first. + A better approach is to use FindMember(). + \note Linear time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(GenericValue&)) operator[](T* name) { + GenericValue n(StringRef(name)); + return (*this)[n]; + } + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast(*this)[name]; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam SourceAllocator Allocator of the \c name value + + \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen(). + And it can also handle strings with embedded null characters. + + \note Linear time complexity. + */ + template + GenericValue& operator[](const GenericValue& name) { + MemberIterator member = FindMember(name); + if (member != MemberEnd()) + return member->value; + else { + RAPIDJSON_ASSERT(false); // see above note + static GenericValue NullValue; + return NullValue; + } + } + template + const GenericValue& operator[](const GenericValue& name) const { return const_cast(*this)[name]; } + +#if RAPIDJSON_HAS_STDSTRING + //! Get a value from an object associated with name (string object). + GenericValue& operator[](const std::basic_string& name) { return (*this)[GenericValue(StringRef(name))]; } + const GenericValue& operator[](const std::basic_string& name) const { return (*this)[GenericValue(StringRef(name))]; } +#endif + + //! Const member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members); } + //! Const \em past-the-end member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members + data_.o.size); } + //! Member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members); } + //! \em Past-the-end member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members + data_.o.size); } + + //! Check whether a member exists in the object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); } + +#if RAPIDJSON_HAS_STDSTRING + //! Check whether a member exists in the object with string object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const std::basic_string& name) const { return FindMember(name) != MemberEnd(); } +#endif + + //! Check whether a member exists in the object with GenericValue name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + template + bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); } + + //! Find member by name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + MemberIterator FindMember(const Ch* name) { + GenericValue n(StringRef(name)); + return FindMember(n); + } + + ConstMemberIterator FindMember(const Ch* name) const { return const_cast(*this).FindMember(name); } + + //! Find member by name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + template + MemberIterator FindMember(const GenericValue& name) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + MemberIterator member = MemberBegin(); + for ( ; member != MemberEnd(); ++member) + if (name.StringEqual(member->name)) + break; + return member; + } + template ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast(*this).FindMember(name); } + +#if RAPIDJSON_HAS_STDSTRING + //! Find member by string object name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + */ + MemberIterator FindMember(const std::basic_string& name) { return FindMember(StringRef(name)); } + ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(StringRef(name)); } +#endif + + //! Add a member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c name and \c value will be transferred to this object on success. + \pre IsObject() && name.IsString() + \post name.IsNull() && value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + + Object& o = data_.o; + if (o.size >= o.capacity) { + if (o.capacity == 0) { + o.capacity = kDefaultObjectCapacity; + o.members = reinterpret_cast(allocator.Malloc(o.capacity * sizeof(Member))); + } + else { + SizeType oldCapacity = o.capacity; + o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5 + o.members = reinterpret_cast(allocator.Realloc(o.members, oldCapacity * sizeof(Member), o.capacity * sizeof(Member))); + } + } + o.members[o.size].name.RawAssign(name); + o.members[o.size].value.RawAssign(value); + o.size++; + return *this; + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Add a string object as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, std::basic_string& value, Allocator& allocator) { + GenericValue v(value, allocator); + return AddMember(name, v, allocator); + } +#endif + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A string value as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(GenericValue& name, T value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + + //! Add a member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this object on success. + \pre IsObject() + \post value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A constant string reference as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(StringRefType name, T value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Remove all members in the object. + /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void RemoveAllMembers() { + RAPIDJSON_ASSERT(IsObject()); + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + data_.o.size = 0; + } + + //! Remove a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Linear time complexity. + */ + bool RemoveMember(const Ch* name) { + GenericValue n(StringRef(name)); + return RemoveMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) { return RemoveMember(GenericValue(StringRef(name))); } +#endif + + template + bool RemoveMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + RemoveMember(m); + return true; + } + else + return false; + } + + //! Remove a member in object by iterator. + /*! \param m member iterator (obtained by FindMember() or MemberBegin()). + \return the new iterator after removal. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Constant time complexity. + */ + MemberIterator RemoveMember(MemberIterator m) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(data_.o.members != 0); + RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd()); + + MemberIterator last(data_.o.members + (data_.o.size - 1)); + if (data_.o.size > 1 && m != last) { + // Move the last one to this place + *m = *last; + } + else { + // Only one left, just destroy + m->~Member(); + } + --data_.o.size; + return m; + } + + //! Remove a member from an object by iterator. + /*! \param pos iterator to the member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd() + \return Iterator following the removed element. + If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned. + \note This function preserves the relative order of the remaining object + members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator). + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator pos) { + return EraseMember(pos, pos +1); + } + + //! Remove members in the range [first, last) from an object. + /*! \param first iterator to the first member to remove + \param last iterator following the last member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd() + \return Iterator following the last removed element. + \note This function preserves the relative order of the remaining object + members. + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(data_.o.members != 0); + RAPIDJSON_ASSERT(first >= MemberBegin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= MemberEnd()); + + MemberIterator pos = MemberBegin() + (first - MemberBegin()); + for (MemberIterator itr = pos; itr != last; ++itr) + itr->~Member(); + std::memmove(&*pos, &*last, (MemberEnd() - last) * sizeof(Member)); + data_.o.size -= (last - first); + return pos; + } + + //@} + + //!@name Array + //@{ + + //! Set this value as an empty array. + /*! \post IsArray == true */ + GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; } + + //! Get the number of elements in array. + SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; } + + //! Get the capacity of array. + SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; } + + //! Check whether the array is empty. + bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; } + + //! Remove all elements in the array. + /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void Clear() { + RAPIDJSON_ASSERT(IsArray()); + for (SizeType i = 0; i < data_.a.size; ++i) + data_.a.elements[i].~GenericValue(); + data_.a.size = 0; + } + + //! Get an element from array by index. + /*! \pre IsArray() == true + \param index Zero-based index of element. + \see operator[](T*) + */ + GenericValue& operator[](SizeType index) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(index < data_.a.size); + return data_.a.elements[index]; + } + const GenericValue& operator[](SizeType index) const { return const_cast(*this)[index]; } + + //! Element iterator + /*! \pre IsArray() == true */ + ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements; } + //! \em Past-the-end element iterator + /*! \pre IsArray() == true */ + ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements + data_.a.size; } + //! Constant element iterator + /*! \pre IsArray() == true */ + ConstValueIterator Begin() const { return const_cast(*this).Begin(); } + //! Constant \em past-the-end element iterator + /*! \pre IsArray() == true */ + ConstValueIterator End() const { return const_cast(*this).End(); } + + //! Request the array to have enough capacity to store elements. + /*! \param newCapacity The capacity that the array at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (newCapacity > data_.a.capacity) { + data_.a.elements = (GenericValue*)allocator.Realloc(data_.a.elements, data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue)); + data_.a.capacity = newCapacity; + } + return *this; + } + + //! Append a GenericValue at the end of the array. + /*! \param value Value to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \post value.IsNull() == true + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this array on success. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + */ + GenericValue& PushBack(GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (data_.a.size >= data_.a.capacity) + Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator); + data_.a.elements[data_.a.size++].RawAssign(value); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& PushBack(GenericValue&& value, Allocator& allocator) { + return PushBack(value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + //! Append a constant string reference at the end of the array. + /*! \param value Constant string reference to be appended. + \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + \see GenericStringRef + */ + GenericValue& PushBack(StringRefType value, Allocator& allocator) { + return (*this).template PushBack(value, allocator); + } + + //! Append a primitive value at the end of the array. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value Value of primitive type T to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref PushBack(GenericValue&, Allocator&) or \ref + PushBack(StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + PushBack(T value, Allocator& allocator) { + GenericValue v(value); + return PushBack(v, allocator); + } + + //! Remove the last element in the array. + /*! + \note Constant time complexity. + */ + GenericValue& PopBack() { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(!Empty()); + data_.a.elements[--data_.a.size].~GenericValue(); + return *this; + } + + //! Remove an element of array by iterator. + /*! + \param pos iterator to the element to remove + \pre IsArray() == true && \ref Begin() <= \c pos < \ref End() + \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator pos) { + return Erase(pos, pos + 1); + } + + //! Remove elements in the range [first, last) of the array. + /*! + \param first iterator to the first element to remove + \param last iterator following the last element to remove + \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End() + \return Iterator following the last removed element. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(data_.a.size > 0); + RAPIDJSON_ASSERT(data_.a.elements != 0); + RAPIDJSON_ASSERT(first >= Begin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= End()); + ValueIterator pos = Begin() + (first - Begin()); + for (ValueIterator itr = pos; itr != last; ++itr) + itr->~GenericValue(); + std::memmove(pos, last, (End() - last) * sizeof(GenericValue)); + data_.a.size -= (last - first); + return pos; + } + + //@} + + //!@name Number + //@{ + + int GetInt() const { RAPIDJSON_ASSERT(flags_ & kIntFlag); return data_.n.i.i; } + unsigned GetUint() const { RAPIDJSON_ASSERT(flags_ & kUintFlag); return data_.n.u.u; } + int64_t GetInt64() const { RAPIDJSON_ASSERT(flags_ & kInt64Flag); return data_.n.i64; } + uint64_t GetUint64() const { RAPIDJSON_ASSERT(flags_ & kUint64Flag); return data_.n.u64; } + + double GetDouble() const { + RAPIDJSON_ASSERT(IsNumber()); + if ((flags_ & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion. + if ((flags_ & kIntFlag) != 0) return data_.n.i.i; // int -> double + if ((flags_ & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double + if ((flags_ & kInt64Flag) != 0) return (double)data_.n.i64; // int64_t -> double (may lose precision) + RAPIDJSON_ASSERT((flags_ & kUint64Flag) != 0); return (double)data_.n.u64; // uint64_t -> double (may lose precision) + } + + GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; } + GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; } + GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } + GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } + GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } + + //@} + + //!@name String + //@{ + + const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return ((flags_ & kInlineStrFlag) ? data_.ss.str : data_.s.str); } + + //! Get the length of string. + /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength(). + */ + SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((flags_ & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); } + + //! Set this value as a string without copying source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string pointer. + \param length The length of source string, excluding the trailing null terminator. + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == length + \see SetString(StringRefType) + */ + GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); } + + //! Set this value as a string without copying source string. + /*! \param s source string reference + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == s.length + */ + GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; } + + //! Set this value as a string by copying from source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string. + \param length The length of source string, excluding the trailing null terminator. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; } + + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); } +#endif + + //@} + + //! Generate events of this value to a Handler. + /*! This function adopts the GoF visitor pattern. + Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. + It can also be used to deep clone this value via GenericDocument, which is also a Handler. + \tparam Handler type of handler. + \param handler An object implementing concept Handler. + */ + template + bool Accept(Handler& handler) const { + switch(GetType()) { + case kNullType: return handler.Null(); + case kFalseType: return handler.Bool(false); + case kTrueType: return handler.Bool(true); + + case kObjectType: + if (!handler.StartObject()) + return false; + for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { + RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator. + if (!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.flags_ & kCopyFlag) != 0)) + return false; + if (!m->value.Accept(handler)) + return false; + } + return handler.EndObject(data_.o.size); + + case kArrayType: + if (!handler.StartArray()) + return false; + for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v) + if (!v->Accept(handler)) + return false; + return handler.EndArray(data_.a.size); + + case kStringType: + return handler.String(GetString(), GetStringLength(), (flags_ & kCopyFlag) != 0); + + default: + RAPIDJSON_ASSERT(GetType() == kNumberType); + if (IsInt()) return handler.Int(data_.n.i.i); + else if (IsUint()) return handler.Uint(data_.n.u.u); + else if (IsInt64()) return handler.Int64(data_.n.i64); + else if (IsUint64()) return handler.Uint64(data_.n.u64); + else return handler.Double(data_.n.d); + } + } + +private: + template friend class GenericValue; + template friend class GenericDocument; + + enum { + kBoolFlag = 0x100, + kNumberFlag = 0x200, + kIntFlag = 0x400, + kUintFlag = 0x800, + kInt64Flag = 0x1000, + kUint64Flag = 0x2000, + kDoubleFlag = 0x4000, + kStringFlag = 0x100000, + kCopyFlag = 0x200000, + kInlineStrFlag = 0x400000, + + // Initial flags of different types. + kNullFlag = kNullType, + kTrueFlag = kTrueType | kBoolFlag, + kFalseFlag = kFalseType | kBoolFlag, + kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag, + kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag, + kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag, + kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag, + kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag, + kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag, + kConstStringFlag = kStringType | kStringFlag, + kCopyStringFlag = kStringType | kStringFlag | kCopyFlag, + kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag, + kObjectFlag = kObjectType, + kArrayFlag = kArrayType, + + kTypeMask = 0xFF // bitwise-and with mask of 0xFF can be optimized by compiler + }; + + static const SizeType kDefaultArrayCapacity = 16; + static const SizeType kDefaultObjectCapacity = 16; + + struct String { + const Ch* str; + SizeType length; + unsigned hashcode; //!< reserved + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars + // (excluding the terminating zero) and store a value to determine the length of the contained + // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string + // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as + // the string terminator as well. For getting the string length back from that value just use + // "MaxSize - str[LenPos]". + // This allows to store 11-chars strings in 32-bit mode and 15-chars strings in 64-bit mode + // inline (for `UTF8`-encoded strings). + struct ShortString { + enum { MaxChars = sizeof(String) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize }; + Ch str[MaxChars]; + + inline static bool Usable(SizeType len) { return (MaxSize >= len); } + inline void SetLength(SizeType len) { str[LenPos] = (Ch)(MaxSize - len); } + inline SizeType GetLength() const { return (SizeType)(MaxSize - str[LenPos]); } + }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // By using proper binary layout, retrieval of different integer types do not need conversions. + union Number { +#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN + struct I { + int i; + char padding[4]; + }i; + struct U { + unsigned u; + char padding2[4]; + }u; +#else + struct I { + char padding[4]; + int i; + }i; + struct U { + char padding2[4]; + unsigned u; + }u; +#endif + int64_t i64; + uint64_t u64; + double d; + }; // 8 bytes + + struct Object { + Member* members; + SizeType size; + SizeType capacity; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + struct Array { + GenericValue* elements; + SizeType size; + SizeType capacity; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + union Data { + String s; + ShortString ss; + Number n; + Object o; + Array a; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // Initialize this value as array with initial data, without calling destructor. + void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { + flags_ = kArrayFlag; + if (count) { + data_.a.elements = (GenericValue*)allocator.Malloc(count * sizeof(GenericValue)); + std::memcpy(data_.a.elements, values, count * sizeof(GenericValue)); + } + else + data_.a.elements = NULL; + data_.a.size = data_.a.capacity = count; + } + + //! Initialize this value as object with initial data, without calling destructor. + void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { + flags_ = kObjectFlag; + if (count) { + data_.o.members = (Member*)allocator.Malloc(count * sizeof(Member)); + std::memcpy(data_.o.members, members, count * sizeof(Member)); + } + else + data_.o.members = NULL; + data_.o.size = data_.o.capacity = count; + } + + //! Initialize this value as constant string, without calling destructor. + void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT { + flags_ = kConstStringFlag; + data_.s.str = s; + data_.s.length = s.length; + } + + //! Initialize this value as copy string with initial data, without calling destructor. + void SetStringRaw(StringRefType s, Allocator& allocator) { + Ch* str = NULL; + if(ShortString::Usable(s.length)) { + flags_ = kShortStringFlag; + data_.ss.SetLength(s.length); + str = data_.ss.str; + } else { + flags_ = kCopyStringFlag; + data_.s.length = s.length; + str = (Ch *)allocator.Malloc((s.length + 1) * sizeof(Ch)); + data_.s.str = str; + } + std::memcpy(str, s, s.length * sizeof(Ch)); + str[s.length] = '\0'; + } + + //! Assignment without calling destructor + void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + data_ = rhs.data_; + flags_ = rhs.flags_; + rhs.flags_ = kNullFlag; + } + + template + bool StringEqual(const GenericValue& rhs) const { + RAPIDJSON_ASSERT(IsString()); + RAPIDJSON_ASSERT(rhs.IsString()); + + const SizeType len1 = GetStringLength(); + const SizeType len2 = rhs.GetStringLength(); + if(len1 != len2) { return false; } + + const Ch* const str1 = GetString(); + const Ch* const str2 = rhs.GetString(); + if(str1 == str2) { return true; } // fast path for constant string + + return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0); + } + + Data data_; + unsigned flags_; +}; + +//! GenericValue with UTF8 encoding +typedef GenericValue > Value; + +/////////////////////////////////////////////////////////////////////////////// +// GenericDocument + +//! A document for parsing JSON text as DOM. +/*! + \note implements Handler concept + \tparam Encoding Encoding for both parsing and string storage. + \tparam Allocator Allocator for allocating memory for the DOM + \tparam StackAllocator Allocator for allocating memory for stack during parsing. + \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue. +*/ +template , typename StackAllocator = CrtAllocator> +class GenericDocument : public GenericValue { +public: + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericValue ValueType; //!< Value type of the document. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + + //! Constructor + /*! \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + : ValueType(std::move(rhs)), + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(std::move(rhs.stack_)), + parseResult_(rhs.parseResult_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + } +#endif + + ~GenericDocument() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + { + // The cast to ValueType is necessary here, because otherwise it would + // attempt to call GenericValue's templated assignment operator. + ValueType::operator=(std::forward(rhs)); + + // Calling the destructor here would prematurely call stack_'s destructor + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = std::move(rhs.stack_); + parseResult_ = rhs.parseResult_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + + return *this; + } +#endif + + //!@name Parse from stream + //!@{ + + //! Parse JSON text from an input stream (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam SourceEncoding Encoding of input stream + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + ValueType::SetNull(); // Remove existing root if exist + GenericReader reader(&stack_.GetAllocator()); + ClearStackOnExit scope(*this); + parseResult_ = reader.template Parse(is, *this); + if (parseResult_) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + this->RawAssign(*stack_.template Pop(1)); // Add this-> to prevent issue 13. + } + return *this; + } + + //! Parse JSON text from an input stream + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + + //! Parse JSON text from an input stream (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + //!@} + + //!@name Parse in-place from mutable string + //!@{ + + //! Parse JSON text from a mutable string + /*! \tparam parseFlags Combination of \ref ParseFlag. + \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseInsitu(Ch* str) { + GenericInsituStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags) + /*! \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + GenericDocument& ParseInsitu(Ch* str) { + return ParseInsitu(str); + } + //!@} + + //!@name Parse from read-only string + //!@{ + + //! Parse JSON text from a read-only string (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \tparam SourceEncoding Transcoding from input Encoding + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const Ch* str) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + GenericStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a read-only string + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags) + /*! \param str Read-only zero-terminated string to be parsed. + */ + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + //!@} + + //!@name Handling parse errors + //!@{ + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseError() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + + //!@} + + //! Get the allocator of this document. + Allocator& GetAllocator() { return *allocator_; } + + //! Get the capacity of stack in bytes. + size_t GetStackCapacity() const { return stack_.GetCapacity(); } + +private: + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericDocument& d) : d_(d) {} + ~ClearStackOnExit() { d_.ClearStack(); } + private: + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + GenericDocument& d_; + }; + + // callers of the following private Handler functions + template friend class GenericReader; // for parsing + template friend class GenericValue; // for deep copying + + // Implementation of Handler + bool Null() { new (stack_.template Push()) ValueType(); return true; } + bool Bool(bool b) { new (stack_.template Push()) ValueType(b); return true; } + bool Int(int i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint(unsigned i) { new (stack_.template Push()) ValueType(i); return true; } + bool Int64(int64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint64(uint64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Double(double d) { new (stack_.template Push()) ValueType(d); return true; } + + bool String(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool StartObject() { new (stack_.template Push()) ValueType(kObjectType); return true; } + + bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount) { + typename ValueType::Member* members = stack_.template Pop(memberCount); + stack_.template Top()->SetObjectRaw(members, (SizeType)memberCount, GetAllocator()); + return true; + } + + bool StartArray() { new (stack_.template Push()) ValueType(kArrayType); return true; } + + bool EndArray(SizeType elementCount) { + ValueType* elements = stack_.template Pop(elementCount); + stack_.template Top()->SetArrayRaw(elements, elementCount, GetAllocator()); + return true; + } + +private: + //! Prohibit copying + GenericDocument(const GenericDocument&); + //! Prohibit assignment + GenericDocument& operator=(const GenericDocument&); + + void ClearStack() { + if (Allocator::kNeedFree) + while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects) + (stack_.template Pop(1))->~ValueType(); + else + stack_.Clear(); + stack_.ShrinkToFit(); + } + + void Destroy() { + RAPIDJSON_DELETE(ownAllocator_); + } + + static const size_t kDefaultStackCapacity = 1024; + Allocator* allocator_; + Allocator* ownAllocator_; + internal::Stack stack_; + ParseResult parseResult_; +}; + +//! GenericDocument with UTF8 encoding +typedef GenericDocument > Document; + +// defined here due to the dependency on GenericDocument +template +template +inline +GenericValue::GenericValue(const GenericValue& rhs, Allocator& allocator) +{ + switch (rhs.GetType()) { + case kObjectType: + case kArrayType: { // perform deep copy via SAX Handler + GenericDocument d(&allocator); + rhs.Accept(d); + RawAssign(*d.stack_.template Pop(1)); + } + break; + case kStringType: + if (rhs.flags_ == kConstStringFlag) { + flags_ = rhs.flags_; + data_ = *reinterpret_cast(&rhs.data_); + } else { + SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); + } + break; + default: // kNumberType, kTrueType, kFalseType, kNullType + flags_ = rhs.flags_; + data_ = *reinterpret_cast(&rhs.data_); + } +} + +RAPIDJSON_NAMESPACE_END + +#if defined(_MSC_VER) || defined(__GNUC__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_DOCUMENT_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/encodedstream.h b/neo/libs/rapidjson/include/rapidjson/encodedstream.h new file mode 100644 index 00000000..7c8863fe --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/encodedstream.h @@ -0,0 +1,261 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODEDSTREAM_H_ +#define RAPIDJSON_ENCODEDSTREAM_H_ + +#include "rapidjson.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Input byte stream wrapper with a statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam InputByteStream Type of input byte stream. For example, FileReadStream. +*/ +template +class EncodedInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedInputStream(InputByteStream& is) : is_(is) { + current_ = Encoding::TakeBOM(is_); + } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); + + InputByteStream& is_; + Ch current_; +}; + +//! Output byte stream wrapper with statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam InputByteStream Type of input byte stream. For example, FileWriteStream. +*/ +template +class EncodedOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { + if (putBOM) + Encoding::PutBOM(os_); + } + + void Put(Ch c) { Encoding::Put(os_, c); } + void Flush() { os_.Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); } + Ch Take() { RAPIDJSON_ASSERT(false); } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedOutputStream(const EncodedOutputStream&); + EncodedOutputStream& operator=(const EncodedOutputStream&); + + OutputByteStream& os_; +}; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + +//! Input stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for reading. + \tparam InputByteStream type of input byte stream to be wrapped. +*/ +template +class AutoUTFInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param is input stream to be wrapped. + \param type UTF encoding type if it is not detected from the stream. + */ + AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + DetectType(); + static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; + takeFunc_ = f[type_]; + current_ = takeFunc_(*is_); + } + + UTFType GetType() const { return type_; } + bool HasBOM() const { return hasBOM_; } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } + size_t Tell() const { return is_->Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFInputStream(const AutoUTFInputStream&); + AutoUTFInputStream& operator=(const AutoUTFInputStream&); + + // Detect encoding type with BOM or RFC 4627 + void DetectType() { + // BOM (Byte Order Mark): + // 00 00 FE FF UTF-32BE + // FF FE 00 00 UTF-32LE + // FE FF UTF-16BE + // FF FE UTF-16LE + // EF BB BF UTF-8 + + const unsigned char* c = (const unsigned char *)is_->Peek4(); + if (!c) + return; + + unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24); + hasBOM_ = false; + if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } + + // RFC 4627: Section 3 + // "Since the first two characters of a JSON text will always be ASCII + // characters [RFC0020], it is possible to determine whether an octet + // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking + // at the pattern of nulls in the first four octets." + // 00 00 00 xx UTF-32BE + // 00 xx 00 xx UTF-16BE + // xx 00 00 00 UTF-32LE + // xx 00 xx 00 UTF-16LE + // xx xx xx xx UTF-8 + + if (!hasBOM_) { + unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); + switch (pattern) { + case 0x08: type_ = kUTF32BE; break; + case 0x0A: type_ = kUTF16BE; break; + case 0x01: type_ = kUTF32LE; break; + case 0x05: type_ = kUTF16LE; break; + case 0x0F: type_ = kUTF8; break; + default: break; // Use type defined by user. + } + } + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + } + + typedef Ch (*TakeFunc)(InputByteStream& is); + InputByteStream* is_; + UTFType type_; + Ch current_; + TakeFunc takeFunc_; + bool hasBOM_; +}; + +//! Output stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for writing. + \tparam InputByteStream type of output byte stream to be wrapped. +*/ +template +class AutoUTFOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param os output stream to be wrapped. + \param type UTF encoding type. + \param putBOM Whether to write BOM at the beginning of the stream. + */ + AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + + static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; + putFunc_ = f[type_]; + + if (putBOM) + PutBOM(); + } + + UTFType GetType() const { return type_; } + + void Put(Ch c) { putFunc_(*os_, c); } + void Flush() { os_->Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); } + Ch Take() { RAPIDJSON_ASSERT(false); } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFOutputStream(const AutoUTFOutputStream&); + AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); + + void PutBOM() { + typedef void (*PutBOMFunc)(OutputByteStream&); + static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; + f[type_](*os_); + } + + typedef void (*PutFunc)(OutputByteStream&, Ch); + + OutputByteStream* os_; + UTFType type_; + PutFunc putFunc_; +}; + +#undef RAPIDJSON_ENCODINGS_FUNC + +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/encodings.h b/neo/libs/rapidjson/include/rapidjson/encodings.h new file mode 100644 index 00000000..90b46ed3 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/encodings.h @@ -0,0 +1,625 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODINGS_H_ +#define RAPIDJSON_ENCODINGS_H_ + +#include "rapidjson.h" + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#elif defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(overflow) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Encoding + +/*! \class rapidjson::Encoding + \brief Concept for encoding of Unicode characters. + +\code +concept Encoding { + typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. + + enum { supportUnicode = 1 }; // or 0 if not supporting unicode + + //! \brief Encode a Unicode codepoint to an output stream. + //! \param os Output stream. + //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. + template + static void Encode(OutputStream& os, unsigned codepoint); + + //! \brief Decode a Unicode codepoint from an input stream. + //! \param is Input stream. + //! \param codepoint Output of the unicode codepoint. + //! \return true if a valid codepoint can be decoded from the stream. + template + static bool Decode(InputStream& is, unsigned* codepoint); + + //! \brief Validate one Unicode codepoint from an encoded stream. + //! \param is Input stream to obtain codepoint. + //! \param os Output for copying one codepoint. + //! \return true if it is valid. + //! \note This function just validating and copying the codepoint without actually decode it. + template + static bool Validate(InputStream& is, OutputStream& os); + + // The following functions are deal with byte streams. + + //! Take a character from input byte stream, skip BOM if exist. + template + static CharType TakeBOM(InputByteStream& is); + + //! Take a character from input byte stream. + template + static Ch Take(InputByteStream& is); + + //! Put BOM to output byte stream. + template + static void PutBOM(OutputByteStream& os); + + //! Put a character to output byte stream. + template + static void Put(OutputByteStream& os, Ch c); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// UTF8 + +//! UTF-8 encoding. +/*! http://en.wikipedia.org/wiki/UTF-8 + http://tools.ietf.org/html/rfc3629 + \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. + \note implements Encoding concept +*/ +template +struct UTF8 { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + os.Put(static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + os.Put(static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + os.Put(static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { +#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | ((unsigned char)c & 0x3Fu) +#define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + Ch c = is.Take(); + if (!(c & 0x80)) { + *codepoint = (unsigned char)c; + return true; + } + + unsigned char type = GetRange((unsigned char)c); + *codepoint = (0xFF >> type) & (unsigned char)c; + bool result = true; + switch (type) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + template + static bool Validate(InputStream& is, OutputStream& os) { +#define COPY() os.Put(c = is.Take()) +#define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + Ch c; + COPY(); + if (!(c & 0x80)) + return true; + + bool result = true; + switch (GetRange((unsigned char)c)) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + static unsigned char GetRange(unsigned char c) { + // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. + static const unsigned char type[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, + }; + return type[c]; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + Ch c = Take(is); + if ((unsigned char)c != 0xEFu) return c; + c = is.Take(); + if ((unsigned char)c != 0xBBu) return c; + c = is.Take(); + if ((unsigned char)c != 0xBFu) return c; + c = is.Take(); + return c; + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return is.Take(); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu); + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF16 + +//! UTF-16 encoding. +/*! http://en.wikipedia.org/wiki/UTF-16 + http://tools.ietf.org/html/rfc2781 + \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF16LE and UTF16BE, which handle endianness. +*/ +template +struct UTF16 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + os.Put(static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + os.Put(static_cast((v >> 10) | 0xD800)); + os.Put((v & 0x3FF) | 0xDC00); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + Ch c = is.Take(); + if (c < 0xD800 || c > 0xDFFF) { + *codepoint = c; + return true; + } + else if (c <= 0xDBFF) { + *codepoint = (c & 0x3FF) << 10; + c = is.Take(); + *codepoint |= (c & 0x3FF); + *codepoint += 0x10000; + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + Ch c; + os.Put(c = is.Take()); + if (c < 0xD800 || c > 0xDFFF) + return true; + else if (c <= 0xDBFF) { + os.Put(c = is.Take()); + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } +}; + +//! UTF-16 little endian encoding. +template +struct UTF16LE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return (unsigned short)c == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = (unsigned char)is.Take(); + c |= (unsigned char)is.Take() << 8; + return c; + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(0xFFu); os.Put(0xFEu); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(c & 0xFFu); + os.Put((c >> 8) & 0xFFu); + } +}; + +//! UTF-16 big endian encoding. +template +struct UTF16BE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return (unsigned short)c == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = (unsigned char)is.Take() << 8; + c |= (unsigned char)is.Take(); + return c; + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(0xFEu); os.Put(0xFFu); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put((c >> 8) & 0xFFu); + os.Put(c & 0xFFu); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF32 + +//! UTF-32 encoding. +/*! http://en.wikipedia.org/wiki/UTF-32 + \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF32LE and UTF32BE, which handle endianness. +*/ +template +struct UTF32 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(codepoint); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c = is.Take(); + *codepoint = c; + return c <= 0x10FFFF; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c; + os.Put(c = is.Take()); + return c <= 0x10FFFF; + } +}; + +//! UTF-32 little endian enocoding. +template +struct UTF32LE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return (unsigned)c == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = (unsigned char)is.Take(); + c |= (unsigned char)is.Take() << 8; + c |= (unsigned char)is.Take() << 16; + c |= (unsigned char)is.Take() << 24; + return c; + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(c & 0xFFu); + os.Put((c >> 8) & 0xFFu); + os.Put((c >> 16) & 0xFFu); + os.Put((c >> 24) & 0xFFu); + } +}; + +//! UTF-32 big endian encoding. +template +struct UTF32BE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return (unsigned)c == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = (unsigned char)is.Take() << 24; + c |= (unsigned char)is.Take() << 16; + c |= (unsigned char)is.Take() << 8; + c |= (unsigned char)is.Take(); + return c; + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put((c >> 24) & 0xFFu); + os.Put((c >> 16) & 0xFFu); + os.Put((c >> 8) & 0xFFu); + os.Put(c & 0xFFu); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// ASCII + +//! ASCII encoding. +/*! http://en.wikipedia.org/wiki/ASCII + \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. + \note implements Encoding concept +*/ +template +struct ASCII { + typedef CharType Ch; + + enum { supportUnicode = 0 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + os.Put(static_cast(codepoint & 0xFF)); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + unsigned char c = static_cast(is.Take()); + *codepoint = c; + return c <= 0X7F; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + unsigned char c = is.Take(); + os.Put(c); + return c <= 0x7F; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + Ch c = Take(is); + return c; + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return is.Take(); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + (void)os; + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// AutoUTF + +//! Runtime-specified UTF encoding type of a stream. +enum UTFType { + kUTF8 = 0, //!< UTF-8. + kUTF16LE = 1, //!< UTF-16 little endian. + kUTF16BE = 2, //!< UTF-16 big endian. + kUTF32LE = 3, //!< UTF-32 little endian. + kUTF32BE = 4 //!< UTF-32 big endian. +}; + +//! Dynamically select encoding according to stream's runtime-specified UTF encoding type. +/*! \note This class can be used with AutoUTFInputtStream and AutoUTFOutputStream, which provides GetType(). +*/ +template +struct AutoUTF { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + + template + RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { + typedef bool (*DecodeFunc)(InputStream&, unsigned*); + static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; + return (*f[is.GetType()])(is, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + typedef bool (*ValidateFunc)(InputStream&, OutputStream&); + static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; + return (*f[is.GetType()])(is, os); + } + +#undef RAPIDJSON_ENCODINGS_FUNC +}; + +/////////////////////////////////////////////////////////////////////////////// +// Transcoder + +//! Encoding conversion. +template +struct Transcoder { + //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::Encode(os, codepoint); + return true; + } + + //! Validate one Unicode codepoint from an encoded stream. + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Transcode(is, os); // Since source/target encoding is different, must transcode. + } +}; + +//! Specialization of Transcoder with same source and target encoding. +template +struct Transcoder { + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Encoding::Validate(is, os); // source/target encoding are the same + } +}; + +RAPIDJSON_NAMESPACE_END + +#if defined(__GNUC__) || defined(_MSV_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/error/en.h b/neo/libs/rapidjson/include/rapidjson/error/en.h new file mode 100644 index 00000000..d5f9caab --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/error/en.h @@ -0,0 +1,65 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_EN_H__ +#define RAPIDJSON_ERROR_EN_H__ + +#include "error.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Maps error code of parsing into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param parseErrorCode Error code obtained in parsing. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { + switch (parseErrorCode) { + case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); + case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values."); + + case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); + + case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); + case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); + case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); + + case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); + + case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); + case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); + case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); + case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); + case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); + + case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); + case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); + case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); + + case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); + case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); + + default: + return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ERROR_EN_H__ diff --git a/neo/libs/rapidjson/include/rapidjson/error/error.h b/neo/libs/rapidjson/include/rapidjson/error/error.h new file mode 100644 index 00000000..f9094fb9 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/error/error.h @@ -0,0 +1,146 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_ERROR_H__ +#define RAPIDJSON_ERROR_ERROR_H__ + +#include "../rapidjson.h" + +/*! \file error.h */ + +/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_CHARTYPE + +//! Character type of error messages. +/*! \ingroup RAPIDJSON_ERRORS + The default character type is \c char. + On Windows, user can define this macro as \c TCHAR for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_CHARTYPE +#define RAPIDJSON_ERROR_CHARTYPE char +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_STRING + +//! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. +/*! \ingroup RAPIDJSON_ERRORS + By default this conversion macro does nothing. + On Windows, user can define this macro as \c _T(x) for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_STRING +#define RAPIDJSON_ERROR_STRING(x) x +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseErrorCode + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericReader::Parse, GenericReader::GetParseErrorCode +*/ +enum ParseErrorCode { + kParseErrorNone = 0, //!< No error. + + kParseErrorDocumentEmpty, //!< The document is empty. + kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. + + kParseErrorValueInvalid, //!< Invalid value. + + kParseErrorObjectMissName, //!< Missing a name for object member. + kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. + kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. + + kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. + + kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. + kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. + kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. + kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. + kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. + + kParseErrorNumberTooBig, //!< Number too big to be stored in double. + kParseErrorNumberMissFraction, //!< Miss fraction part in number. + kParseErrorNumberMissExponent, //!< Miss exponent in number. + + kParseErrorTermination, //!< Parsing was terminated. + kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. +}; + +//! Result of parsing (wraps ParseErrorCode) +/*! + \ingroup RAPIDJSON_ERRORS + \code + Document doc; + ParseResult ok = doc.Parse("[42]"); + if (!ok) { + fprintf(stderr, "JSON parse error: %s (%u)", + GetParseError_En(ok.Code()), ok.Offset()); + exit(EXIT_FAILURE); + } + \endcode + \see GenericReader::Parse, GenericDocument::Parse +*/ +struct ParseResult { + + //! Default constructor, no error. + ParseResult() : code_(kParseErrorNone), offset_(0) {} + //! Constructor to set an error. + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} + + //! Get the error code. + ParseErrorCode Code() const { return code_; } + //! Get the error offset, if \ref IsError(), 0 otherwise. + size_t Offset() const { return offset_; } + + //! Conversion to \c bool, returns \c true, iff !\ref IsError(). + operator bool() const { return !IsError(); } + //! Whether the result is an error. + bool IsError() const { return code_ != kParseErrorNone; } + + bool operator==(const ParseResult& that) const { return code_ == that.code_; } + bool operator==(ParseErrorCode code) const { return code_ == code; } + friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } + + //! Reset error code. + void Clear() { Set(kParseErrorNone); } + //! Update error code and offset. + void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } + +private: + ParseErrorCode code_; + size_t offset_; +}; + +//! Function pointer type of GetParseError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetParseError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetParseErrorFunc GetParseError = GetParseError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ERROR_ERROR_H__ diff --git a/neo/libs/rapidjson/include/rapidjson/filereadstream.h b/neo/libs/rapidjson/include/rapidjson/filereadstream.h new file mode 100644 index 00000000..3913eb74 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/filereadstream.h @@ -0,0 +1,88 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEREADSTREAM_H_ +#define RAPIDJSON_FILEREADSTREAM_H_ + +#include "rapidjson.h" +#include + +RAPIDJSON_NAMESPACE_BEGIN + +//! File byte stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileReadStream { +public: + typedef char Ch; //!< Character type (byte). + + //! Constructor. + /*! + \param fp File pointer opened for read. + \param buffer user-supplied buffer. + \param bufferSize size of buffer in bytes. Must >=4 bytes. + */ + FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + RAPIDJSON_ASSERT(fp_ != 0); + RAPIDJSON_ASSERT(bufferSize >= 4); + Read(); + } + + Ch Peek() const { return *current_; } + Ch Take() { Ch c = *current_; Read(); return c; } + size_t Tell() const { return count_ + static_cast(current_ - buffer_); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return (current_ + 4 <= bufferLast_) ? current_ : 0; + } + +private: + void Read() { + if (current_ < bufferLast_) + ++current_; + else if (!eof_) { + count_ += readCount_; + readCount_ = fread(buffer_, 1, bufferSize_, fp_); + bufferLast_ = buffer_ + readCount_ - 1; + current_ = buffer_; + + if (readCount_ < bufferSize_) { + buffer_[readCount_] = '\0'; + ++bufferLast_; + eof_ = true; + } + } + } + + std::FILE* fp_; + Ch *buffer_; + size_t bufferSize_; + Ch *bufferLast_; + Ch *current_; + size_t readCount_; + size_t count_; //!< Number of characters read + bool eof_; +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/filewritestream.h b/neo/libs/rapidjson/include/rapidjson/filewritestream.h new file mode 100644 index 00000000..dfb9cbd0 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/filewritestream.h @@ -0,0 +1,91 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEWRITESTREAM_H_ +#define RAPIDJSON_FILEWRITESTREAM_H_ + +#include "rapidjson.h" +#include + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of C file stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileWriteStream { +public: + typedef char Ch; //!< Character type. Only support char. + + FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { + RAPIDJSON_ASSERT(fp_ != 0); + } + + void Put(char c) { + if (current_ >= bufferEnd_) + Flush(); + + *current_++ = c; + } + + void PutN(char c, size_t n) { + size_t avail = static_cast(bufferEnd_ - current_); + while (n > avail) { + std::memset(current_, c, avail); + current_ += avail; + Flush(); + n -= avail; + avail = static_cast(bufferEnd_ - current_); + } + + if (n > 0) { + std::memset(current_, c, n); + current_ += n; + } + } + + void Flush() { + if (current_ != buffer_) { + fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); + current_ = buffer_; + } + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + // Prohibit copy constructor & assignment operator. + FileWriteStream(const FileWriteStream&); + FileWriteStream& operator=(const FileWriteStream&); + + std::FILE* fp_; + char *buffer_; + char *bufferEnd_; + char *current_; +}; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(FileWriteStream& stream, char c, size_t n) { + stream.PutN(c, n); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/biginteger.h b/neo/libs/rapidjson/include/rapidjson/internal/biginteger.h new file mode 100644 index 00000000..99a30acf --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/biginteger.h @@ -0,0 +1,280 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_BIGINTEGER_H_ +#define RAPIDJSON_BIGINTEGER_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include // for _umul128 +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class BigInteger { +public: + typedef uint64_t Type; + + BigInteger(const BigInteger& rhs) : count_(rhs.count_) { + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + + explicit BigInteger(uint64_t u) : count_(1) { + digits_[0] = u; + } + + BigInteger(const char* decimals, size_t length) : count_(1) { + RAPIDJSON_ASSERT(length > 0); + digits_[0] = 0; + size_t i = 0; + const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 + while (length >= kMaxDigitPerIteration) { + AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); + length -= kMaxDigitPerIteration; + i += kMaxDigitPerIteration; + } + + if (length > 0) + AppendDecimal64(decimals + i, decimals + i + length); + } + + BigInteger& operator=(uint64_t u) { + digits_[0] = u; + count_ = 1; + return *this; + } + + BigInteger& operator+=(uint64_t u) { + Type backup = digits_[0]; + digits_[0] += u; + for (size_t i = 0; i < count_ - 1; i++) { + if (digits_[i] >= backup) + return *this; // no carry + backup = digits_[i + 1]; + digits_[i + 1] += 1; + } + + // Last carry + if (digits_[count_ - 1] < backup) + PushBack(1); + + return *this; + } + + BigInteger& operator*=(uint64_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + uint64_t hi; + digits_[i] = MulAdd64(digits_[i], u, k, &hi); + k = hi; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator*=(uint32_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + const uint64_t c = digits_[i] >> 32; + const uint64_t d = digits_[i] & 0xFFFFFFFF; + const uint64_t uc = u * c; + const uint64_t ud = u * d; + const uint64_t p0 = ud + k; + const uint64_t p1 = uc + (p0 >> 32); + digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); + k = p1 >> 32; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator<<=(size_t shift) { + if (IsZero() || shift == 0) return *this; + + size_t offset = shift / kTypeBit; + size_t interShift = shift % kTypeBit; + RAPIDJSON_ASSERT(count_ + offset <= kCapacity); + + if (interShift == 0) { + std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type)); + count_ += offset; + } + else { + digits_[count_] = 0; + for (size_t i = count_; i > 0; i--) + digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); + digits_[offset] = digits_[0] << interShift; + count_ += offset; + if (digits_[count_]) + count_++; + } + + std::memset(digits_, 0, offset * sizeof(Type)); + + return *this; + } + + bool operator==(const BigInteger& rhs) const { + return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; + } + + bool operator==(const Type rhs) const { + return count_ == 1 && digits_[0] == rhs; + } + + BigInteger& MultiplyPow5(unsigned exp) { + static const uint32_t kPow5[12] = { + 5, + 5 * 5, + 5 * 5 * 5, + 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 + }; + if (exp == 0) return *this; + for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 + for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 + if (exp > 0) *this *= kPow5[exp - 1]; + return *this; + } + + // Compute absolute difference of this and rhs. + // Assume this != rhs + bool Difference(const BigInteger& rhs, BigInteger* out) const { + int cmp = Compare(rhs); + RAPIDJSON_ASSERT(cmp != 0); + const BigInteger *a, *b; // Makes a > b + bool ret; + if (cmp < 0) { a = &rhs; b = this; ret = true; } + else { a = this; b = &rhs; ret = false; } + + Type borrow = 0; + for (size_t i = 0; i < a->count_; i++) { + Type d = a->digits_[i] - borrow; + if (i < b->count_) + d -= b->digits_[i]; + borrow = (d > a->digits_[i]) ? 1 : 0; + out->digits_[i] = d; + if (d != 0) + out->count_ = i + 1; + } + + return ret; + } + + int Compare(const BigInteger& rhs) const { + if (count_ != rhs.count_) + return count_ < rhs.count_ ? -1 : 1; + + for (size_t i = count_; i-- > 0;) + if (digits_[i] != rhs.digits_[i]) + return digits_[i] < rhs.digits_[i] ? -1 : 1; + + return 0; + } + + size_t GetCount() const { return count_; } + Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } + bool IsZero() const { return count_ == 1 && digits_[0] == 0; } + +private: + void AppendDecimal64(const char* begin, const char* end) { + uint64_t u = ParseUint64(begin, end); + if (IsZero()) + *this = u; + else { + unsigned exp = static_cast(end - begin); + (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u + } + } + + void PushBack(Type digit) { + RAPIDJSON_ASSERT(count_ < kCapacity); + digits_[count_++] = digit; + } + + static uint64_t ParseUint64(const char* begin, const char* end) { + uint64_t r = 0; + for (const char* p = begin; p != end; ++p) { + RAPIDJSON_ASSERT(*p >= '0' && *p <= '9'); + r = r * 10 + (*p - '0'); + } + return r; + } + + // Assume a * b + k < 2^128 + static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t low = _umul128(a, b, outHigh) + k; + if (low < k) + (*outHigh)++; + return low; +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(a) * static_cast(b); + p += k; + *outHigh = static_cast(p >> 64); + return static_cast(p); +#else + const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; + uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; + x1 += (x0 >> 32); // can't give carry + x1 += x2; + if (x1 < x2) + x3 += (static_cast(1) << 32); + uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); + uint64_t hi = x3 + (x1 >> 32); + + lo += k; + if (lo < k) + hi++; + *outHigh = hi; + return lo; +#endif + } + + static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 + static const size_t kCapacity = kBitCount / sizeof(Type); + static const size_t kTypeBit = sizeof(Type) * 8; + + Type digits_[kCapacity]; + size_t count_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_BIGINTEGER_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/diyfp.h b/neo/libs/rapidjson/include/rapidjson/internal/diyfp.h new file mode 100644 index 00000000..3b6c4238 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/diyfp.h @@ -0,0 +1,247 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DIYFP_H_ +#define RAPIDJSON_DIYFP_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include +#pragma intrinsic(_BitScanReverse64) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +struct DiyFp { + DiyFp() {} + + DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} + + explicit DiyFp(double d) { + union { + double d; + uint64_t u64; + } u = { d }; + + int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); + uint64_t significand = (u.u64 & kDpSignificandMask); + if (biased_e != 0) { + f = significand + kDpHiddenBit; + e = biased_e - kDpExponentBias; + } + else { + f = significand; + e = kDpMinExponent + 1; + } + } + + DiyFp operator-(const DiyFp& rhs) const { + return DiyFp(f - rhs.f, e); + } + + DiyFp operator*(const DiyFp& rhs) const { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t h; + uint64_t l = _umul128(f, rhs.f, &h); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(f) * static_cast(rhs.f); + uint64_t h = static_cast(p >> 64); + uint64_t l = static_cast(p); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#else + const uint64_t M32 = 0xFFFFFFFF; + const uint64_t a = f >> 32; + const uint64_t b = f & M32; + const uint64_t c = rhs.f >> 32; + const uint64_t d = rhs.f & M32; + const uint64_t ac = a * c; + const uint64_t bc = b * c; + const uint64_t ad = a * d; + const uint64_t bd = b * d; + uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); + tmp += 1U << 31; /// mult_round + return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); +#endif + } + + DiyFp Normalize() const { +#if defined(_MSC_VER) && defined(_M_AMD64) + unsigned long index; + _BitScanReverse64(&index, f); + return DiyFp(f << (63 - index), e - (63 - index)); +#elif defined(__GNUC__) && __GNUC__ >= 4 + int s = __builtin_clzll(f); + return DiyFp(f << s, e - s); +#else + DiyFp res = *this; + while (!(res.f & (static_cast(1) << 63))) { + res.f <<= 1; + res.e--; + } + return res; +#endif + } + + DiyFp NormalizeBoundary() const { + DiyFp res = *this; + while (!(res.f & (kDpHiddenBit << 1))) { + res.f <<= 1; + res.e--; + } + res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); + res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); + return res; + } + + void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { + DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); + DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); + mi.f <<= mi.e - pl.e; + mi.e = pl.e; + *plus = pl; + *minus = mi; + } + + double ToDouble() const { + union { + double d; + uint64_t u64; + }u; + const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : + static_cast(e + kDpExponentBias); + u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); + return u.d; + } + + static const int kDiySignificandSize = 64; + static const int kDpSignificandSize = 52; + static const int kDpExponentBias = 0x3FF + kDpSignificandSize; + static const int kDpMaxExponent = 0x7FF - kDpExponentBias; + static const int kDpMinExponent = -kDpExponentBias; + static const int kDpDenormalExponent = -kDpExponentBias + 1; + static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + uint64_t f; + int e; +}; + +inline DiyFp GetCachedPowerByIndex(size_t index) { + // 10^-348, 10^-340, ..., 10^340 + static const uint64_t kCachedPowers_F[] = { + RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), + RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), + RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), + RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), + RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), + RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), + RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), + RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), + RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), + RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), + RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), + RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), + RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), + RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), + RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), + RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), + RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), + RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), + RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), + RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), + RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), + RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), + RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), + RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), + RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), + RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), + RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), + RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), + RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), + RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), + RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), + RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), + RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), + RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), + RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), + RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), + RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), + RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), + RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), + RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), + RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), + RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), + RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), + RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) + }; + static const int16_t kCachedPowers_E[] = { + -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, + -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, + -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, + -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, + -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, + 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, + 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, + 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, + 907, 933, 960, 986, 1013, 1039, 1066 + }; + return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); +} + +inline DiyFp GetCachedPower(int e, int* K) { + + //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; + double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive + int k = static_cast(dk); + if (dk - k > 0.0) + k++; + + unsigned index = static_cast((k >> 3) + 1); + *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table + + return GetCachedPowerByIndex(index); +} + +inline DiyFp GetCachedPower10(int exp, int *outExp) { + unsigned index = (exp + 348) / 8; + *outExp = -348 + index * 8; + return GetCachedPowerByIndex(index); + } + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DIYFP_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/dtoa.h b/neo/libs/rapidjson/include/rapidjson/internal/dtoa.h new file mode 100644 index 00000000..2d8d2e46 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/dtoa.h @@ -0,0 +1,217 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DTOA_ +#define RAPIDJSON_DTOA_ + +#include "itoa.h" // GetDigitsLut() +#include "diyfp.h" +#include "ieee754.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { + while (rest < wp_w && delta - rest >= ten_kappa && + (rest + ten_kappa < wp_w || /// closer + wp_w - rest > rest + ten_kappa - wp_w)) { + buffer[len - 1]--; + rest += ten_kappa; + } +} + +inline unsigned CountDecimalDigit32(uint32_t n) { + // Simple pure C++ implementation was faster than __builtin_clz version in this situation. + if (n < 10) return 1; + if (n < 100) return 2; + if (n < 1000) return 3; + if (n < 10000) return 4; + if (n < 100000) return 5; + if (n < 1000000) return 6; + if (n < 10000000) return 7; + if (n < 100000000) return 8; + // Will not reach 10 digits in DigitGen() + //if (n < 1000000000) return 9; + //return 10; + return 9; +} + +inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { + static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); + const DiyFp wp_w = Mp - W; + uint32_t p1 = static_cast(Mp.f >> -one.e); + uint64_t p2 = Mp.f & (one.f - 1); + int kappa = CountDecimalDigit32(p1); // kappa in [0, 9] + *len = 0; + + while (kappa > 0) { + uint32_t d = 0; + switch (kappa) { + case 9: d = p1 / 100000000; p1 %= 100000000; break; + case 8: d = p1 / 10000000; p1 %= 10000000; break; + case 7: d = p1 / 1000000; p1 %= 1000000; break; + case 6: d = p1 / 100000; p1 %= 100000; break; + case 5: d = p1 / 10000; p1 %= 10000; break; + case 4: d = p1 / 1000; p1 %= 1000; break; + case 3: d = p1 / 100; p1 %= 100; break; + case 2: d = p1 / 10; p1 %= 10; break; + case 1: d = p1; p1 = 0; break; + default:; + } + if (d || *len) + buffer[(*len)++] = static_cast('0' + static_cast(d)); + kappa--; + uint64_t tmp = (static_cast(p1) << -one.e) + p2; + if (tmp <= delta) { + *K += kappa; + GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f); + return; + } + } + + // kappa = 0 + for (;;) { + p2 *= 10; + delta *= 10; + char d = static_cast(p2 >> -one.e); + if (d || *len) + buffer[(*len)++] = static_cast('0' + d); + p2 &= one.f - 1; + kappa--; + if (p2 < delta) { + *K += kappa; + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]); + return; + } + } +} + +inline void Grisu2(double value, char* buffer, int* length, int* K) { + const DiyFp v(value); + DiyFp w_m, w_p; + v.NormalizedBoundaries(&w_m, &w_p); + + const DiyFp c_mk = GetCachedPower(w_p.e, K); + const DiyFp W = v.Normalize() * c_mk; + DiyFp Wp = w_p * c_mk; + DiyFp Wm = w_m * c_mk; + Wm.f++; + Wp.f--; + DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); +} + +inline char* WriteExponent(int K, char* buffer) { + if (K < 0) { + *buffer++ = '-'; + K = -K; + } + + if (K >= 100) { + *buffer++ = static_cast('0' + static_cast(K / 100)); + K %= 100; + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else if (K >= 10) { + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else + *buffer++ = static_cast('0' + static_cast(K)); + + return buffer; +} + +inline char* Prettify(char* buffer, int length, int k) { + const int kk = length + k; // 10^(kk-1) <= v < 10^kk + + if (length <= kk && kk <= 21) { + // 1234e7 -> 12340000000 + for (int i = length; i < kk; i++) + buffer[i] = '0'; + buffer[kk] = '.'; + buffer[kk + 1] = '0'; + return &buffer[kk + 2]; + } + else if (0 < kk && kk <= 21) { + // 1234e-2 -> 12.34 + std::memmove(&buffer[kk + 1], &buffer[kk], length - kk); + buffer[kk] = '.'; + return &buffer[length + 1]; + } + else if (-6 < kk && kk <= 0) { + // 1234e-6 -> 0.001234 + const int offset = 2 - kk; + std::memmove(&buffer[offset], &buffer[0], length); + buffer[0] = '0'; + buffer[1] = '.'; + for (int i = 2; i < offset; i++) + buffer[i] = '0'; + return &buffer[length + offset]; + } + else if (length == 1) { + // 1e30 + buffer[1] = 'e'; + return WriteExponent(kk - 1, &buffer[2]); + } + else { + // 1234e30 -> 1.234e33 + std::memmove(&buffer[2], &buffer[1], length - 1); + buffer[1] = '.'; + buffer[length + 1] = 'e'; + return WriteExponent(kk - 1, &buffer[0 + length + 2]); + } +} + +inline char* dtoa(double value, char* buffer) { + Double d(value); + if (d.IsZero()) { + if (d.Sign()) + *buffer++ = '-'; // -0.0, Issue #289 + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else { + if (value < 0) { + *buffer++ = '-'; + value = -value; + } + int length, K; + Grisu2(value, buffer, &length, &K); + return Prettify(buffer, length, K); + } +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DTOA_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/ieee754.h b/neo/libs/rapidjson/include/rapidjson/internal/ieee754.h new file mode 100644 index 00000000..e3f03364 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/ieee754.h @@ -0,0 +1,77 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_IEEE754_ +#define RAPIDJSON_IEEE754_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class Double { +public: + Double() {} + Double(double d) : d_(d) {} + Double(uint64_t u) : u_(u) {} + + double Value() const { return d_; } + uint64_t Uint64Value() const { return u_; } + + double NextPositiveDouble() const { + RAPIDJSON_ASSERT(!Sign()); + return Double(u_ + 1).Value(); + } + + bool Sign() const { return (u_ & kSignMask) != 0; } + uint64_t Significand() const { return u_ & kSignificandMask; } + int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } + + bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } + bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } + bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } + bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } + + uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } + int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } + uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } + + static unsigned EffectiveSignificandSize(int order) { + if (order >= -1021) + return 53; + else if (order <= -1074) + return 0; + else + return order + 1074; + } + +private: + static const int kSignificandSize = 52; + static const int kExponentBias = 0x3FF; + static const int kDenormalExponent = 1 - kExponentBias; + static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); + static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + union { + double d_; + uint64_t u_; + }; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_IEEE754_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/itoa.h b/neo/libs/rapidjson/include/rapidjson/internal/itoa.h new file mode 100644 index 00000000..01a4e7e7 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/itoa.h @@ -0,0 +1,304 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ITOA_ +#define RAPIDJSON_ITOA_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline const char* GetDigitsLut() { + static const char cDigitsLut[200] = { + '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', + '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', + '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', + '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', + '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', + '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', + '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', + '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', + '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', + '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' + }; + return cDigitsLut; +} + +inline char* u32toa(uint32_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + + if (value < 10000) { + const uint32_t d1 = (value / 100) << 1; + const uint32_t d2 = (value % 100) << 1; + + if (value >= 1000) + *buffer++ = cDigitsLut[d1]; + if (value >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else if (value < 100000000) { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + else { + // value = aabbbbcccc in decimal + + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a >= 10) { + const unsigned i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + *buffer++ = static_cast('0' + static_cast(a)); + + const uint32_t b = value / 10000; // 0 to 9999 + const uint32_t c = value % 10000; // 0 to 9999 + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + return buffer; +} + +inline char* i32toa(int32_t value, char* buffer) { + uint32_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u32toa(u, buffer); +} + +inline char* u64toa(uint64_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + const uint64_t kTen8 = 100000000; + const uint64_t kTen9 = kTen8 * 10; + const uint64_t kTen10 = kTen8 * 100; + const uint64_t kTen11 = kTen8 * 1000; + const uint64_t kTen12 = kTen8 * 10000; + const uint64_t kTen13 = kTen8 * 100000; + const uint64_t kTen14 = kTen8 * 1000000; + const uint64_t kTen15 = kTen8 * 10000000; + const uint64_t kTen16 = kTen8 * kTen8; + + if (value < kTen8) { + uint32_t v = static_cast(value); + if (v < 10000) { + const uint32_t d1 = (v / 100) << 1; + const uint32_t d2 = (v % 100) << 1; + + if (v >= 1000) + *buffer++ = cDigitsLut[d1]; + if (v >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (v >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + } + else if (value < kTen16) { + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + if (value >= kTen15) + *buffer++ = cDigitsLut[d1]; + if (value >= kTen14) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= kTen13) + *buffer++ = cDigitsLut[d2]; + if (value >= kTen12) + *buffer++ = cDigitsLut[d2 + 1]; + if (value >= kTen11) + *buffer++ = cDigitsLut[d3]; + if (value >= kTen10) + *buffer++ = cDigitsLut[d3 + 1]; + if (value >= kTen9) + *buffer++ = cDigitsLut[d4]; + if (value >= kTen8) + *buffer++ = cDigitsLut[d4 + 1]; + + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + else { + const uint32_t a = static_cast(value / kTen16); // 1 to 1844 + value %= kTen16; + + if (a < 10) + *buffer++ = static_cast('0' + static_cast(a)); + else if (a < 100) { + const uint32_t i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else if (a < 1000) { + *buffer++ = static_cast('0' + static_cast(a / 100)); + + const uint32_t i = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else { + const uint32_t i = (a / 100) << 1; + const uint32_t j = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + *buffer++ = cDigitsLut[j]; + *buffer++ = cDigitsLut[j + 1]; + } + + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + + return buffer; +} + +inline char* i64toa(int64_t value, char* buffer) { + uint64_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u64toa(u, buffer); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ITOA_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/meta.h b/neo/libs/rapidjson/include/rapidjson/internal/meta.h new file mode 100644 index 00000000..5a9aaa42 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/meta.h @@ -0,0 +1,181 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_META_H_ +#define RAPIDJSON_INTERNAL_META_H_ + +#include "../rapidjson.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif +#if defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(6334) +#endif + +#if RAPIDJSON_HAS_CXX11_TYPETRAITS +#include +#endif + +//@cond RAPIDJSON_INTERNAL +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching +template struct Void { typedef void Type; }; + +/////////////////////////////////////////////////////////////////////////////// +// BoolType, TrueType, FalseType +// +template struct BoolType { + static const bool Value = Cond; + typedef BoolType Type; +}; +typedef BoolType TrueType; +typedef BoolType FalseType; + + +/////////////////////////////////////////////////////////////////////////////// +// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr +// + +template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; +template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; +template struct SelectIfCond : SelectIfImpl::template Apply {}; +template struct SelectIf : SelectIfCond {}; + +template struct AndExprCond : FalseType {}; +template <> struct AndExprCond : TrueType {}; +template struct OrExprCond : TrueType {}; +template <> struct OrExprCond : FalseType {}; + +template struct BoolExpr : SelectIf::Type {}; +template struct NotExpr : SelectIf::Type {}; +template struct AndExpr : AndExprCond::Type {}; +template struct OrExpr : OrExprCond::Type {}; + + +/////////////////////////////////////////////////////////////////////////////// +// AddConst, MaybeAddConst, RemoveConst +template struct AddConst { typedef const T Type; }; +template struct MaybeAddConst : SelectIfCond {}; +template struct RemoveConst { typedef T Type; }; +template struct RemoveConst { typedef T Type; }; + + +/////////////////////////////////////////////////////////////////////////////// +// IsSame, IsConst, IsMoreConst, IsPointer +// +template struct IsSame : FalseType {}; +template struct IsSame : TrueType {}; + +template struct IsConst : FalseType {}; +template struct IsConst : TrueType {}; + +template +struct IsMoreConst + : AndExpr::Type, typename RemoveConst::Type>, + BoolType::Value >= IsConst::Value> >::Type {}; + +template struct IsPointer : FalseType {}; +template struct IsPointer : TrueType {}; + +/////////////////////////////////////////////////////////////////////////////// +// IsBaseOf +// +#if RAPIDJSON_HAS_CXX11_TYPETRAITS + +template struct IsBaseOf + : BoolType< ::std::is_base_of::value> {}; + +#else // simplified version adopted from Boost + +template struct IsBaseOfImpl { + RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); + RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); + + typedef char (&Yes)[1]; + typedef char (&No) [2]; + + template + static Yes Check(const D*, T); + static No Check(const B*, int); + + struct Host { + operator const B*() const; + operator const D*(); + }; + + enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; +}; + +template struct IsBaseOf + : OrExpr, BoolExpr > >::Type {}; + +#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS + + +////////////////////////////////////////////////////////////////////////// +// EnableIf / DisableIf +// +template struct EnableIfCond { typedef T Type; }; +template struct EnableIfCond { /* empty */ }; + +template struct DisableIfCond { typedef T Type; }; +template struct DisableIfCond { /* empty */ }; + +template +struct EnableIf : EnableIfCond {}; + +template +struct DisableIf : DisableIfCond {}; + +// SFINAE helpers +struct SfinaeTag {}; +template struct RemoveSfinaeTag; +template struct RemoveSfinaeTag { typedef T Type; }; + +#define RAPIDJSON_REMOVEFPTR_(type) \ + typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ + < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type + +#define RAPIDJSON_ENABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type * = NULL + +#define RAPIDJSON_DISABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type * = NULL + +#define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type + +#define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type + +} // namespace internal +RAPIDJSON_NAMESPACE_END +//@endcond + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_META_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/pow10.h b/neo/libs/rapidjson/include/rapidjson/internal/pow10.h new file mode 100644 index 00000000..02f475d7 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/pow10.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POW10_ +#define RAPIDJSON_POW10_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Computes integer powers of 10 in double (10.0^n). +/*! This function uses lookup table for fast and accurate results. + \param n non-negative exponent. Must <= 308. + \return 10.0^n +*/ +inline double Pow10(int n) { + static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes + 1e+0, + 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, + 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, + 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, + 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, + 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, + 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, + 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, + 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, + 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, + 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, + 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, + 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, + 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, + 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, + 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, + 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 + }; + RAPIDJSON_ASSERT(n >= 0 && n <= 308); + return e[n]; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_POW10_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/stack.h b/neo/libs/rapidjson/include/rapidjson/internal/stack.h new file mode 100644 index 00000000..722d5692 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/stack.h @@ -0,0 +1,179 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STACK_H_ +#define RAPIDJSON_INTERNAL_STACK_H_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// Stack + +//! A type-unsafe stack for storing different types of data. +/*! \tparam Allocator Allocator for allocating stack memory. +*/ +template +class Stack { +public: + // Optimization note: Do not allocate memory for stack_ in constructor. + // Do it lazily when first Push() -> Expand() -> Resize(). + Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { + RAPIDJSON_ASSERT(stackCapacity > 0); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack(Stack&& rhs) + : allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(rhs.stack_), + stackTop_(rhs.stackTop_), + stackEnd_(rhs.stackEnd_), + initialCapacity_(rhs.initialCapacity_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } +#endif + + ~Stack() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack& operator=(Stack&& rhs) { + if (&rhs != this) + { + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = rhs.stack_; + stackTop_ = rhs.stackTop_; + stackEnd_ = rhs.stackEnd_; + initialCapacity_ = rhs.initialCapacity_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } + return *this; + } +#endif + + void Clear() { stackTop_ = stack_; } + + void ShrinkToFit() { + if (Empty()) { + // If the stack is empty, completely deallocate the memory. + Allocator::Free(stack_); + stack_ = 0; + stackTop_ = 0; + stackEnd_ = 0; + } + else + Resize(GetSize()); + } + + // Optimization note: try to minimize the size of this function for force inline. + // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. + template + RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { + // Expand the stack if needed + if (stackTop_ + sizeof(T) * count >= stackEnd_) + Expand(count); + + T* ret = reinterpret_cast(stackTop_); + stackTop_ += sizeof(T) * count; + return ret; + } + + template + T* Pop(size_t count) { + RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); + stackTop_ -= count * sizeof(T); + return reinterpret_cast(stackTop_); + } + + template + T* Top() { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + T* Bottom() { return (T*)stack_; } + + Allocator& GetAllocator() { return *allocator_; } + bool Empty() const { return stackTop_ == stack_; } + size_t GetSize() const { return static_cast(stackTop_ - stack_); } + size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } + +private: + template + void Expand(size_t count) { + // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. + size_t newCapacity; + if (stack_ == 0) { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + newCapacity = initialCapacity_; + } else { + newCapacity = GetCapacity(); + newCapacity += (newCapacity + 1) / 2; + } + size_t newSize = GetSize() + sizeof(T) * count; + if (newCapacity < newSize) + newCapacity = newSize; + + Resize(newCapacity); + } + + void Resize(size_t newCapacity) { + const size_t size = GetSize(); // Backup the current size + stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity); + stackTop_ = stack_ + size; + stackEnd_ = stack_ + newCapacity; + } + + void Destroy() { + Allocator::Free(stack_); + RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack + } + + // Prohibit copy constructor & assignment operator. + Stack(const Stack&); + Stack& operator=(const Stack&); + + Allocator* allocator_; + Allocator* ownAllocator_; + char *stack_; + char *stackTop_; + char *stackEnd_; + size_t initialCapacity_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STACK_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/strfunc.h b/neo/libs/rapidjson/include/rapidjson/internal/strfunc.h new file mode 100644 index 00000000..84405065 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/strfunc.h @@ -0,0 +1,39 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ +#define RAPIDJSON_INTERNAL_STRFUNC_H_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom strlen() which works on different character types. +/*! \tparam Ch Character type (e.g. char, wchar_t, short) + \param s Null-terminated input string. + \return Number of characters in the string. + \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. +*/ +template +inline SizeType StrLen(const Ch* s) { + const Ch* p = s; + while (*p) ++p; + return SizeType(p - s); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_INTERNAL_STRFUNC_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/internal/strtod.h b/neo/libs/rapidjson/include/rapidjson/internal/strtod.h new file mode 100644 index 00000000..ace65f67 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/internal/strtod.h @@ -0,0 +1,270 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRTOD_ +#define RAPIDJSON_STRTOD_ + +#include "../rapidjson.h" +#include "ieee754.h" +#include "biginteger.h" +#include "diyfp.h" +#include "pow10.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline double FastPath(double significand, int exp) { + if (exp < -308) + return 0.0; + else if (exp >= 0) + return significand * internal::Pow10(exp); + else + return significand / internal::Pow10(-exp); +} + +inline double StrtodNormalPrecision(double d, int p) { + if (p < -308) { + // Prevent expSum < -308, making Pow10(p) = 0 + d = FastPath(d, -308); + d = FastPath(d, p + 308); + } + else + d = FastPath(d, p); + return d; +} + +template +inline T Min3(T a, T b, T c) { + T m = a; + if (m > b) m = b; + if (m > c) m = c; + return m; +} + +inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { + const Double db(b); + const uint64_t bInt = db.IntegerSignificand(); + const int bExp = db.IntegerExponent(); + const int hExp = bExp - 1; + + int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; + + // Adjust for decimal exponent + if (dExp >= 0) { + dS_Exp2 += dExp; + dS_Exp5 += dExp; + } + else { + bS_Exp2 -= dExp; + bS_Exp5 -= dExp; + hS_Exp2 -= dExp; + hS_Exp5 -= dExp; + } + + // Adjust for binary exponent + if (bExp >= 0) + bS_Exp2 += bExp; + else { + dS_Exp2 -= bExp; + hS_Exp2 -= bExp; + } + + // Adjust for half ulp exponent + if (hExp >= 0) + hS_Exp2 += hExp; + else { + dS_Exp2 -= hExp; + bS_Exp2 -= hExp; + } + + // Remove common power of two factor from all three scaled values + int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); + dS_Exp2 -= common_Exp2; + bS_Exp2 -= common_Exp2; + hS_Exp2 -= common_Exp2; + + BigInteger dS = d; + dS.MultiplyPow5(dS_Exp5) <<= dS_Exp2; + + BigInteger bS(bInt); + bS.MultiplyPow5(bS_Exp5) <<= bS_Exp2; + + BigInteger hS(1); + hS.MultiplyPow5(hS_Exp5) <<= hS_Exp2; + + BigInteger delta(0); + dS.Difference(bS, &delta); + + return delta.Compare(hS); +} + +inline bool StrtodFast(double d, int p, double* result) { + // Use fast path for string-to-double conversion if possible + // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ + if (p > 22 && p < 22 + 16) { + // Fast Path Cases In Disguise + d *= internal::Pow10(p - 22); + p = 22; + } + + if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 + *result = FastPath(d, p); + return true; + } + else + return false; +} + +// Compute an approximation and see if it is within 1/2 ULP +inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) { + uint64_t significand = 0; + size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 + for (; i < length; i++) { + if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || + (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5')) + break; + significand = significand * 10 + (decimals[i] - '0'); + } + + if (i < length && decimals[i] >= '5') // Rounding + significand++; + + size_t remaining = length - i; + const unsigned kUlpShift = 3; + const unsigned kUlp = 1 << kUlpShift; + int error = (remaining == 0) ? 0 : kUlp / 2; + + DiyFp v(significand, 0); + v = v.Normalize(); + error <<= -v.e; + + const int dExp = (int)decimalPosition - (int)i + exp; + + int actualExp; + DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); + if (actualExp != dExp) { + static const DiyFp kPow10[] = { + DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 + DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 + DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 + DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 + DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 + DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 + DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 + }; + int adjustment = dExp - actualExp - 1; + RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); + v = v * kPow10[adjustment]; + if (length + adjustment > 19) // has more digits than decimal digits in 64-bit + error += kUlp / 2; + } + + v = v * cachedPower; + + error += kUlp + (error == 0 ? 0 : 1); + + const int oldExp = v.e; + v = v.Normalize(); + error <<= oldExp - v.e; + + const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); + unsigned precisionSize = 64 - effectiveSignificandSize; + if (precisionSize + kUlpShift >= 64) { + unsigned scaleExp = (precisionSize + kUlpShift) - 63; + v.f >>= scaleExp; + v.e += scaleExp; + error = (error >> scaleExp) + 1 + kUlp; + precisionSize -= scaleExp; + } + + DiyFp rounded(v.f >> precisionSize, v.e + precisionSize); + const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; + const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; + if (precisionBits >= halfWay + error) { + rounded.f++; + if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) + rounded.f >>= 1; + rounded.e++; + } + } + + *result = rounded.ToDouble(); + + return halfWay - error >= precisionBits || precisionBits >= halfWay + error; +} + +inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) { + const BigInteger dInt(decimals, length); + const int dExp = (int)decimalPosition - (int)length + exp; + Double a(approx); + int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); + if (cmp < 0) + return a.Value(); // within half ULP + else if (cmp == 0) { + // Round towards even + if (a.Significand() & 1) + return a.NextPositiveDouble(); + else + return a.Value(); + } + else // adjustment + return a.NextPositiveDouble(); +} + +inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) { + RAPIDJSON_ASSERT(d >= 0.0); + RAPIDJSON_ASSERT(length >= 1); + + double result; + if (StrtodFast(d, p, &result)) + return result; + + // Trim leading zeros + while (*decimals == '0' && length > 1) { + length--; + decimals++; + decimalPosition--; + } + + // Trim trailing zeros + while (decimals[length - 1] == '0' && length > 1) { + length--; + decimalPosition--; + exp++; + } + + // Trim right-most digits + const int kMaxDecimalDigit = 780; + if ((int)length > kMaxDecimalDigit) { + int delta = (int(length) - kMaxDecimalDigit); + exp += delta; + decimalPosition -= delta; + length = kMaxDecimalDigit; + } + + // If too small, underflow to zero + if (int(length) + exp < -324) + return 0.0; + + if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) + return result; + + // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison + return StrtodBigInteger(result, decimals, length, decimalPosition, exp); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STRTOD_ diff --git a/neo/libs/rapidjson/include/rapidjson/memorybuffer.h b/neo/libs/rapidjson/include/rapidjson/memorybuffer.h new file mode 100644 index 00000000..2484b218 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/memorybuffer.h @@ -0,0 +1,70 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYBUFFER_H_ +#define RAPIDJSON_MEMORYBUFFER_H_ + +#include "rapidjson.h" +#include "internal/stack.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output byte stream. +/*! + This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. + + It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. + + Differences between MemoryBuffer and StringBuffer: + 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. + 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. + + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +struct GenericMemoryBuffer { + typedef char Ch; // byte + + GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + + void Put(Ch c) { *stack_.template Push() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { stack_.ShrinkToFit(); } + Ch* Push(size_t count) { return stack_.template Push(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetBuffer() const { + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; +}; + +typedef GenericMemoryBuffer<> MemoryBuffer; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { + std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/memorystream.h b/neo/libs/rapidjson/include/rapidjson/memorystream.h new file mode 100644 index 00000000..99feae5d --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/memorystream.h @@ -0,0 +1,61 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYSTREAM_H_ +#define RAPIDJSON_MEMORYSTREAM_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory input byte stream. +/*! + This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. + + It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. + + Differences between MemoryStream and StringStream: + 1. StringStream has encoding but MemoryStream is a byte stream. + 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. + 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). + \note implements Stream concept +*/ +struct MemoryStream { + typedef char Ch; // byte + + MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} + + Ch Peek() const { return (src_ == end_) ? '\0' : *src_; } + Ch Take() { return (src_ == end_) ? '\0' : *src_++; } + size_t Tell() const { return static_cast(src_ - begin_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return Tell() + 4 <= size_ ? src_ : 0; + } + + const Ch* src_; //!< Current read position. + const Ch* begin_; //!< Original head of the string. + const Ch* end_; //!< End of stream. + size_t size_; //!< Size of the stream. +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/msinttypes/inttypes.h b/neo/libs/rapidjson/include/rapidjson/msinttypes/inttypes.h new file mode 100644 index 00000000..18111286 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/msinttypes/inttypes.h @@ -0,0 +1,316 @@ +// ISO C9x compliant inttypes.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +// The above software in this distribution may have been modified by +// THL A29 Limited ("Tencent Modifications"). +// All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_INTTYPES_H_ // [ +#define _MSC_INTTYPES_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +#include "stdint.h" + +// miloyip: VC supports inttypes.h since VC2013 +#if _MSC_VER >= 1800 +#include +#else + +// 7.8 Format conversion of integer types + +typedef struct { + intmax_t quot; + intmax_t rem; +} imaxdiv_t; + +// 7.8.1 Macros for format specifiers + +#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198 + +// The fprintf macros for signed integers are: +#define PRId8 "d" +#define PRIi8 "i" +#define PRIdLEAST8 "d" +#define PRIiLEAST8 "i" +#define PRIdFAST8 "d" +#define PRIiFAST8 "i" + +#define PRId16 "hd" +#define PRIi16 "hi" +#define PRIdLEAST16 "hd" +#define PRIiLEAST16 "hi" +#define PRIdFAST16 "hd" +#define PRIiFAST16 "hi" + +#define PRId32 "I32d" +#define PRIi32 "I32i" +#define PRIdLEAST32 "I32d" +#define PRIiLEAST32 "I32i" +#define PRIdFAST32 "I32d" +#define PRIiFAST32 "I32i" + +#define PRId64 "I64d" +#define PRIi64 "I64i" +#define PRIdLEAST64 "I64d" +#define PRIiLEAST64 "I64i" +#define PRIdFAST64 "I64d" +#define PRIiFAST64 "I64i" + +#define PRIdMAX "I64d" +#define PRIiMAX "I64i" + +#define PRIdPTR "Id" +#define PRIiPTR "Ii" + +// The fprintf macros for unsigned integers are: +#define PRIo8 "o" +#define PRIu8 "u" +#define PRIx8 "x" +#define PRIX8 "X" +#define PRIoLEAST8 "o" +#define PRIuLEAST8 "u" +#define PRIxLEAST8 "x" +#define PRIXLEAST8 "X" +#define PRIoFAST8 "o" +#define PRIuFAST8 "u" +#define PRIxFAST8 "x" +#define PRIXFAST8 "X" + +#define PRIo16 "ho" +#define PRIu16 "hu" +#define PRIx16 "hx" +#define PRIX16 "hX" +#define PRIoLEAST16 "ho" +#define PRIuLEAST16 "hu" +#define PRIxLEAST16 "hx" +#define PRIXLEAST16 "hX" +#define PRIoFAST16 "ho" +#define PRIuFAST16 "hu" +#define PRIxFAST16 "hx" +#define PRIXFAST16 "hX" + +#define PRIo32 "I32o" +#define PRIu32 "I32u" +#define PRIx32 "I32x" +#define PRIX32 "I32X" +#define PRIoLEAST32 "I32o" +#define PRIuLEAST32 "I32u" +#define PRIxLEAST32 "I32x" +#define PRIXLEAST32 "I32X" +#define PRIoFAST32 "I32o" +#define PRIuFAST32 "I32u" +#define PRIxFAST32 "I32x" +#define PRIXFAST32 "I32X" + +#define PRIo64 "I64o" +#define PRIu64 "I64u" +#define PRIx64 "I64x" +#define PRIX64 "I64X" +#define PRIoLEAST64 "I64o" +#define PRIuLEAST64 "I64u" +#define PRIxLEAST64 "I64x" +#define PRIXLEAST64 "I64X" +#define PRIoFAST64 "I64o" +#define PRIuFAST64 "I64u" +#define PRIxFAST64 "I64x" +#define PRIXFAST64 "I64X" + +#define PRIoMAX "I64o" +#define PRIuMAX "I64u" +#define PRIxMAX "I64x" +#define PRIXMAX "I64X" + +#define PRIoPTR "Io" +#define PRIuPTR "Iu" +#define PRIxPTR "Ix" +#define PRIXPTR "IX" + +// The fscanf macros for signed integers are: +#define SCNd8 "d" +#define SCNi8 "i" +#define SCNdLEAST8 "d" +#define SCNiLEAST8 "i" +#define SCNdFAST8 "d" +#define SCNiFAST8 "i" + +#define SCNd16 "hd" +#define SCNi16 "hi" +#define SCNdLEAST16 "hd" +#define SCNiLEAST16 "hi" +#define SCNdFAST16 "hd" +#define SCNiFAST16 "hi" + +#define SCNd32 "ld" +#define SCNi32 "li" +#define SCNdLEAST32 "ld" +#define SCNiLEAST32 "li" +#define SCNdFAST32 "ld" +#define SCNiFAST32 "li" + +#define SCNd64 "I64d" +#define SCNi64 "I64i" +#define SCNdLEAST64 "I64d" +#define SCNiLEAST64 "I64i" +#define SCNdFAST64 "I64d" +#define SCNiFAST64 "I64i" + +#define SCNdMAX "I64d" +#define SCNiMAX "I64i" + +#ifdef _WIN64 // [ +# define SCNdPTR "I64d" +# define SCNiPTR "I64i" +#else // _WIN64 ][ +# define SCNdPTR "ld" +# define SCNiPTR "li" +#endif // _WIN64 ] + +// The fscanf macros for unsigned integers are: +#define SCNo8 "o" +#define SCNu8 "u" +#define SCNx8 "x" +#define SCNX8 "X" +#define SCNoLEAST8 "o" +#define SCNuLEAST8 "u" +#define SCNxLEAST8 "x" +#define SCNXLEAST8 "X" +#define SCNoFAST8 "o" +#define SCNuFAST8 "u" +#define SCNxFAST8 "x" +#define SCNXFAST8 "X" + +#define SCNo16 "ho" +#define SCNu16 "hu" +#define SCNx16 "hx" +#define SCNX16 "hX" +#define SCNoLEAST16 "ho" +#define SCNuLEAST16 "hu" +#define SCNxLEAST16 "hx" +#define SCNXLEAST16 "hX" +#define SCNoFAST16 "ho" +#define SCNuFAST16 "hu" +#define SCNxFAST16 "hx" +#define SCNXFAST16 "hX" + +#define SCNo32 "lo" +#define SCNu32 "lu" +#define SCNx32 "lx" +#define SCNX32 "lX" +#define SCNoLEAST32 "lo" +#define SCNuLEAST32 "lu" +#define SCNxLEAST32 "lx" +#define SCNXLEAST32 "lX" +#define SCNoFAST32 "lo" +#define SCNuFAST32 "lu" +#define SCNxFAST32 "lx" +#define SCNXFAST32 "lX" + +#define SCNo64 "I64o" +#define SCNu64 "I64u" +#define SCNx64 "I64x" +#define SCNX64 "I64X" +#define SCNoLEAST64 "I64o" +#define SCNuLEAST64 "I64u" +#define SCNxLEAST64 "I64x" +#define SCNXLEAST64 "I64X" +#define SCNoFAST64 "I64o" +#define SCNuFAST64 "I64u" +#define SCNxFAST64 "I64x" +#define SCNXFAST64 "I64X" + +#define SCNoMAX "I64o" +#define SCNuMAX "I64u" +#define SCNxMAX "I64x" +#define SCNXMAX "I64X" + +#ifdef _WIN64 // [ +# define SCNoPTR "I64o" +# define SCNuPTR "I64u" +# define SCNxPTR "I64x" +# define SCNXPTR "I64X" +#else // _WIN64 ][ +# define SCNoPTR "lo" +# define SCNuPTR "lu" +# define SCNxPTR "lx" +# define SCNXPTR "lX" +#endif // _WIN64 ] + +#endif // __STDC_FORMAT_MACROS ] + +// 7.8.2 Functions for greatest-width integer types + +// 7.8.2.1 The imaxabs function +#define imaxabs _abs64 + +// 7.8.2.2 The imaxdiv function + +// This is modified version of div() function from Microsoft's div.c found +// in %MSVC.NET%\crt\src\div.c +#ifdef STATIC_IMAXDIV // [ +static +#else // STATIC_IMAXDIV ][ +_inline +#endif // STATIC_IMAXDIV ] +imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) +{ + imaxdiv_t result; + + result.quot = numer / denom; + result.rem = numer % denom; + + if (numer < 0 && result.rem > 0) { + // did division wrong; must fix up + ++result.quot; + result.rem -= denom; + } + + return result; +} + +// 7.8.2.3 The strtoimax and strtoumax functions +#define strtoimax _strtoi64 +#define strtoumax _strtoui64 + +// 7.8.2.4 The wcstoimax and wcstoumax functions +#define wcstoimax _wcstoi64 +#define wcstoumax _wcstoui64 + +#endif // _MSC_VER >= 1800 + +#endif // _MSC_INTTYPES_H_ ] diff --git a/neo/libs/rapidjson/include/rapidjson/msinttypes/stdint.h b/neo/libs/rapidjson/include/rapidjson/msinttypes/stdint.h new file mode 100644 index 00000000..a26fff4b --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/msinttypes/stdint.h @@ -0,0 +1,300 @@ +// ISO C9x compliant stdint.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2013 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. Neither the name of the product nor the names of its contributors may +// be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +// The above software in this distribution may have been modified by +// THL A29 Limited ("Tencent Modifications"). +// All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. + +#ifndef _MSC_VER // [ +#error "Use this header only with Microsoft Visual C++ compilers!" +#endif // _MSC_VER ] + +#ifndef _MSC_STDINT_H_ // [ +#define _MSC_STDINT_H_ + +#if _MSC_VER > 1000 +#pragma once +#endif + +// miloyip: Originally Visual Studio 2010 uses its own stdint.h. However it generates warning with INT64_C(), so change to use this file for vs2010. +#if _MSC_VER >= 1600 // [ +#include + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +#undef INT8_C +#undef INT16_C +#undef INT32_C +#undef INT64_C +#undef UINT8_C +#undef UINT16_C +#undef UINT32_C +#undef UINT64_C + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#else // ] _MSC_VER >= 1700 [ + +#include + +// For Visual Studio 6 in C++ mode and for many Visual Studio versions when +// compiling for ARM we should wrap include with 'extern "C++" {}' +// or compiler give many errors like this: +// error C2733: second C linkage of overloaded function 'wmemchr' not allowed +#ifdef __cplusplus +extern "C" { +#endif +# include +#ifdef __cplusplus +} +#endif + +// Define _W64 macros to mark types changing their size, like intptr_t. +#ifndef _W64 +# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 +# define _W64 __w64 +# else +# define _W64 +# endif +#endif + + +// 7.18.1 Integer types + +// 7.18.1.1 Exact-width integer types + +// Visual Studio 6 and Embedded Visual C++ 4 doesn't +// realize that, e.g. char has the same size as __int8 +// so we give up on __intX for them. +#if (_MSC_VER < 1300) + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; +#else + typedef signed __int8 int8_t; + typedef signed __int16 int16_t; + typedef signed __int32 int32_t; + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; +#endif +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; + + +// 7.18.1.2 Minimum-width integer types +typedef int8_t int_least8_t; +typedef int16_t int_least16_t; +typedef int32_t int_least32_t; +typedef int64_t int_least64_t; +typedef uint8_t uint_least8_t; +typedef uint16_t uint_least16_t; +typedef uint32_t uint_least32_t; +typedef uint64_t uint_least64_t; + +// 7.18.1.3 Fastest minimum-width integer types +typedef int8_t int_fast8_t; +typedef int16_t int_fast16_t; +typedef int32_t int_fast32_t; +typedef int64_t int_fast64_t; +typedef uint8_t uint_fast8_t; +typedef uint16_t uint_fast16_t; +typedef uint32_t uint_fast32_t; +typedef uint64_t uint_fast64_t; + +// 7.18.1.4 Integer types capable of holding object pointers +#ifdef _WIN64 // [ + typedef signed __int64 intptr_t; + typedef unsigned __int64 uintptr_t; +#else // _WIN64 ][ + typedef _W64 signed int intptr_t; + typedef _W64 unsigned int uintptr_t; +#endif // _WIN64 ] + +// 7.18.1.5 Greatest-width integer types +typedef int64_t intmax_t; +typedef uint64_t uintmax_t; + + +// 7.18.2 Limits of specified-width integer types + +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 + +// 7.18.2.1 Limits of exact-width integer types +#define INT8_MIN ((int8_t)_I8_MIN) +#define INT8_MAX _I8_MAX +#define INT16_MIN ((int16_t)_I16_MIN) +#define INT16_MAX _I16_MAX +#define INT32_MIN ((int32_t)_I32_MIN) +#define INT32_MAX _I32_MAX +#define INT64_MIN ((int64_t)_I64_MIN) +#define INT64_MAX _I64_MAX +#define UINT8_MAX _UI8_MAX +#define UINT16_MAX _UI16_MAX +#define UINT32_MAX _UI32_MAX +#define UINT64_MAX _UI64_MAX + +// 7.18.2.2 Limits of minimum-width integer types +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MIN INT64_MIN +#define INT_LEAST64_MAX INT64_MAX +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +// 7.18.2.3 Limits of fastest minimum-width integer types +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST16_MAX INT16_MAX +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MIN INT64_MIN +#define INT_FAST64_MAX INT64_MAX +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT16_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +// 7.18.2.4 Limits of integer types capable of holding object pointers +#ifdef _WIN64 // [ +# define INTPTR_MIN INT64_MIN +# define INTPTR_MAX INT64_MAX +# define UINTPTR_MAX UINT64_MAX +#else // _WIN64 ][ +# define INTPTR_MIN INT32_MIN +# define INTPTR_MAX INT32_MAX +# define UINTPTR_MAX UINT32_MAX +#endif // _WIN64 ] + +// 7.18.2.5 Limits of greatest-width integer types +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +// 7.18.3 Limits of other integer types + +#ifdef _WIN64 // [ +# define PTRDIFF_MIN _I64_MIN +# define PTRDIFF_MAX _I64_MAX +#else // _WIN64 ][ +# define PTRDIFF_MIN _I32_MIN +# define PTRDIFF_MAX _I32_MAX +#endif // _WIN64 ] + +#define SIG_ATOMIC_MIN INT_MIN +#define SIG_ATOMIC_MAX INT_MAX + +#ifndef SIZE_MAX // [ +# ifdef _WIN64 // [ +# define SIZE_MAX _UI64_MAX +# else // _WIN64 ][ +# define SIZE_MAX _UI32_MAX +# endif // _WIN64 ] +#endif // SIZE_MAX ] + +// WCHAR_MIN and WCHAR_MAX are also defined in +#ifndef WCHAR_MIN // [ +# define WCHAR_MIN 0 +#endif // WCHAR_MIN ] +#ifndef WCHAR_MAX // [ +# define WCHAR_MAX _UI16_MAX +#endif // WCHAR_MAX ] + +#define WINT_MIN 0 +#define WINT_MAX _UI16_MAX + +#endif // __STDC_LIMIT_MACROS ] + + +// 7.18.4 Limits of other integer types + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 + +// 7.18.4.1 Macros for minimum-width integer constants + +#define INT8_C(val) val##i8 +#define INT16_C(val) val##i16 +#define INT32_C(val) val##i32 +#define INT64_C(val) val##i64 + +#define UINT8_C(val) val##ui8 +#define UINT16_C(val) val##ui16 +#define UINT32_C(val) val##ui32 +#define UINT64_C(val) val##ui64 + +// 7.18.4.2 Macros for greatest-width integer constants +// These #ifndef's are needed to prevent collisions with . +// Check out Issue 9 for the details. +#ifndef INTMAX_C // [ +# define INTMAX_C INT64_C +#endif // INTMAX_C ] +#ifndef UINTMAX_C // [ +# define UINTMAX_C UINT64_C +#endif // UINTMAX_C ] + +#endif // __STDC_CONSTANT_MACROS ] + +#endif // _MSC_VER >= 1600 ] + +#endif // _MSC_STDINT_H_ ] diff --git a/neo/libs/rapidjson/include/rapidjson/prettywriter.h b/neo/libs/rapidjson/include/rapidjson/prettywriter.h new file mode 100644 index 00000000..416dd492 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/prettywriter.h @@ -0,0 +1,207 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_PRETTYWRITER_H_ +#define RAPIDJSON_PRETTYWRITER_H_ + +#include "writer.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Writer with indentation and spacing. +/*! + \tparam OutputStream Type of ouptut os. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator> +class PrettyWriter : public Writer { +public: + typedef Writer Base; + typedef typename Base::Ch Ch; + + //! Constructor + /*! \param os Output stream. + \param allocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} + + //! Set custom indentation. + /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). + \param indentCharCount Number of indent characters for each indentation level. + \note The default indentation is 4 spaces. + */ + PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { + RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); + indentChar_ = indentChar; + indentCharCount_ = indentCharCount; + return *this; + } + + /*! @name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } + bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } + bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } + bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } + bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } + bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } + bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kStringType); + return Base::WriteString(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + PrettyPrefix(kObjectType); + new (Base::level_stack_.template Push()) typename Base::Level(false); + return Base::WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndObject(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + bool StartArray() { + PrettyPrefix(kArrayType); + new (Base::level_stack_.template Push()) typename Base::Level(true); + return Base::WriteStartArray(); + } + + bool EndArray(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndArray(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} +protected: + void PrettyPrefix(Type type) { + (void)type; + if (Base::level_stack_.GetSize() != 0) { // this value is not at root + typename Base::Level* level = Base::level_stack_.template Top(); + + if (level->inArray) { + if (level->valueCount > 0) { + Base::os_->Put(','); // add comma if it is not the first element in array + Base::os_->Put('\n'); + } + else + Base::os_->Put('\n'); + WriteIndent(); + } + else { // in object + if (level->valueCount > 0) { + if (level->valueCount % 2 == 0) { + Base::os_->Put(','); + Base::os_->Put('\n'); + } + else { + Base::os_->Put(':'); + Base::os_->Put(' '); + } + } + else + Base::os_->Put('\n'); + + if (level->valueCount % 2 == 0) + WriteIndent(); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. + Base::hasRoot_ = true; + } + } + + void WriteIndent() { + size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; + PutN(*Base::os_, indentChar_, count); + } + + Ch indentChar_; + unsigned indentCharCount_; + +private: + // Prohibit copy constructor & assignment operator. + PrettyWriter(const PrettyWriter&); + PrettyWriter& operator=(const PrettyWriter&); +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/rapidjson.h b/neo/libs/rapidjson/include/rapidjson/rapidjson.h new file mode 100644 index 00000000..f22130d3 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/rapidjson.h @@ -0,0 +1,654 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_RAPIDJSON_H_ +#define RAPIDJSON_RAPIDJSON_H_ + +/*!\file rapidjson.h + \brief common definitions and configuration + + \see RAPIDJSON_CONFIG + */ + +/*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration + \brief Configuration macros for library features + + Some RapidJSON features are configurable to adapt the library to a wide + variety of platforms, environments and usage scenarios. Most of the + features can be configured in terms of overriden or predefined + preprocessor macros at compile-time. + + Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs. + + \note These macros should be given on the compiler command-line + (where applicable) to avoid inconsistent values when compiling + different translation units of a single application. + */ + +#include // malloc(), realloc(), free(), size_t +#include // memset(), memcpy(), memmove(), memcmp() + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_VERSION_STRING +// +// ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt. +// + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +// token stringification +#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) +#define RAPIDJSON_DO_STRINGIFY(x) #x +//!@endcond + +/*! \def RAPIDJSON_MAJOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Major version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_MINOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Minor version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_PATCH_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Patch version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_VERSION_STRING + \ingroup RAPIDJSON_CONFIG + \brief Version of RapidJSON in ".." string format. +*/ +#define RAPIDJSON_MAJOR_VERSION 1 +#define RAPIDJSON_MINOR_VERSION 0 +#define RAPIDJSON_PATCH_VERSION 2 +#define RAPIDJSON_VERSION_STRING \ + RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NAMESPACE_(BEGIN|END) +/*! \def RAPIDJSON_NAMESPACE + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace + + In order to avoid symbol clashes and/or "One Definition Rule" errors + between multiple inclusions of (different versions of) RapidJSON in + a single binary, users can customize the name of the main RapidJSON + namespace. + + In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE + to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple + levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref + RAPIDJSON_NAMESPACE_END need to be defined as well: + + \code + // in some .cpp file + #define RAPIDJSON_NAMESPACE my::rapidjson + #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson { + #define RAPIDJSON_NAMESPACE_END } } + #include "rapidjson/..." + \endcode + + \see rapidjson + */ +/*! \def RAPIDJSON_NAMESPACE_BEGIN + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (opening expression) + \see RAPIDJSON_NAMESPACE +*/ +/*! \def RAPIDJSON_NAMESPACE_END + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (closing expression) + \see RAPIDJSON_NAMESPACE +*/ +#ifndef RAPIDJSON_NAMESPACE +#define RAPIDJSON_NAMESPACE rapidjson +#endif +#ifndef RAPIDJSON_NAMESPACE_BEGIN +#define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE { +#endif +#ifndef RAPIDJSON_NAMESPACE_END +#define RAPIDJSON_NAMESPACE_END } +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_INT64DEFINE + +/*! \def RAPIDJSON_NO_INT64DEFINE + \ingroup RAPIDJSON_CONFIG + \brief Use external 64-bit integer types. + + RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types + to be available at global scope. + + If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to + prevent RapidJSON from defining its own types. +*/ +#ifndef RAPIDJSON_NO_INT64DEFINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#ifdef _MSC_VER +#include "msinttypes/stdint.h" +#include "msinttypes/inttypes.h" +#else +// Other compilers should have this. +#include +#include +#endif +//!@endcond +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_INT64DEFINE +#endif +#endif // RAPIDJSON_NO_INT64TYPEDEF + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_FORCEINLINE + +#ifndef RAPIDJSON_FORCEINLINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && !defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __forceinline +#elif defined(__GNUC__) && __GNUC__ >= 4 && !defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __attribute__((always_inline)) +#else +#define RAPIDJSON_FORCEINLINE +#endif +//!@endcond +#endif // RAPIDJSON_FORCEINLINE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ENDIAN +#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine +#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine + +//! Endianness of the machine. +/*! + \def RAPIDJSON_ENDIAN + \ingroup RAPIDJSON_CONFIG + + GCC 4.6 provided macro for detecting endianness of the target machine. But other + compilers may not have this. User can define RAPIDJSON_ENDIAN to either + \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN. + + Default detection implemented with reference to + \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html + \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp +*/ +#ifndef RAPIDJSON_ENDIAN +// Detect with GCC 4.6's macro +# ifdef __BYTE_ORDER__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __BYTE_ORDER__ +// Detect with GLIBC's endian.h +# elif defined(__GLIBC__) +# include +# if (__BYTE_ORDER == __LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif (__BYTE_ORDER == __BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __GLIBC__ +// Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro +# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +// Detect with architecture macros +# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(RAPIDJSON_DOXYGEN_RUNNING) +# define RAPIDJSON_ENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif +#endif // RAPIDJSON_ENDIAN + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_64BIT + +//! Whether using 64-bit architecture +#ifndef RAPIDJSON_64BIT +#if defined(__LP64__) || defined(_WIN64) +#define RAPIDJSON_64BIT 1 +#else +#define RAPIDJSON_64BIT 0 +#endif +#endif // RAPIDJSON_64BIT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ALIGN + +//! Data alignment of the machine. +/*! \ingroup RAPIDJSON_CONFIG + \param x pointer to align + + Some machines require strict data alignment. Currently the default uses 4 bytes + alignment. User can customize by defining the RAPIDJSON_ALIGN function macro., +*/ +#ifndef RAPIDJSON_ALIGN +#if RAPIDJSON_64BIT == 1 +#define RAPIDJSON_ALIGN(x) ((x + 7u) & ~7u) +#else +#define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_UINT64_C2 + +//! Construct a 64-bit literal by a pair of 32-bit integer. +/*! + 64-bit literal with or without ULL suffix is prone to compiler warnings. + UINT64_C() is C macro which cause compilation problems. + Use this macro to define 64-bit constants by a pair of 32-bit integer. +*/ +#ifndef RAPIDJSON_UINT64_C2 +#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast(high32) << 32) | static_cast(low32)) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD + +/*! \def RAPIDJSON_SIMD + \ingroup RAPIDJSON_CONFIG + \brief Enable SSE2/SSE4.2 optimization. + + RapidJSON supports optimized implementations for some parsing operations + based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible + processors. + + To enable these optimizations, two different symbols can be defined; + \code + // Enable SSE2 optimization. + #define RAPIDJSON_SSE2 + + // Enable SSE4.2 optimization. + #define RAPIDJSON_SSE42 + \endcode + + \c RAPIDJSON_SSE42 takes precedence, if both are defined. + + If any of these symbols is defined, RapidJSON defines the macro + \c RAPIDJSON_SIMD to indicate the availability of the optimized code. +*/ +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ + || defined(RAPIDJSON_DOXYGEN_RUNNING) +#define RAPIDJSON_SIMD +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_SIZETYPEDEFINE + +#ifndef RAPIDJSON_NO_SIZETYPEDEFINE +/*! \def RAPIDJSON_NO_SIZETYPEDEFINE + \ingroup RAPIDJSON_CONFIG + \brief User-provided \c SizeType definition. + + In order to avoid using 32-bit size types for indexing strings and arrays, + define this preprocessor symbol and provide the type rapidjson::SizeType + before including RapidJSON: + \code + #define RAPIDJSON_NO_SIZETYPEDEFINE + namespace rapidjson { typedef ::std::size_t SizeType; } + #include "rapidjson/..." + \endcode + + \see rapidjson::SizeType +*/ +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_SIZETYPEDEFINE +#endif +RAPIDJSON_NAMESPACE_BEGIN +//! Size type (for string lengths, array sizes, etc.) +/*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms, + instead of using \c size_t. Users may override the SizeType by defining + \ref RAPIDJSON_NO_SIZETYPEDEFINE. +*/ +typedef unsigned SizeType; +RAPIDJSON_NAMESPACE_END +#endif + +// always import std::size_t to rapidjson namespace +RAPIDJSON_NAMESPACE_BEGIN +using std::size_t; +RAPIDJSON_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ASSERT + +//! Assertion. +/*! \ingroup RAPIDJSON_CONFIG + By default, rapidjson uses C \c assert() for internal assertions. + User can override it by defining RAPIDJSON_ASSERT(x) macro. + + \note Parsing errors are handled and can be customized by the + \ref RAPIDJSON_ERRORS APIs. +*/ +#ifndef RAPIDJSON_ASSERT +#include +#define RAPIDJSON_ASSERT(x) assert(x) +#endif // RAPIDJSON_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_STATIC_ASSERT + +// Adopt from boost +#ifndef RAPIDJSON_STATIC_ASSERT +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +RAPIDJSON_NAMESPACE_BEGIN +template struct STATIC_ASSERTION_FAILURE; +template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; +template struct StaticAssertTest {}; +RAPIDJSON_NAMESPACE_END + +#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) +#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) +#define RAPIDJSON_DO_JOIN2(X, Y) X##Y + +#if defined(__GNUC__) +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) +#else +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif +//!@endcond + +/*! \def RAPIDJSON_STATIC_ASSERT + \brief (Internal) macro to check for conditions at compile-time + \param x compile-time condition + \hideinitializer + */ +#define RAPIDJSON_STATIC_ASSERT(x) \ + typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ + sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ + RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Helpers + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#define RAPIDJSON_MULTILINEMACRO_BEGIN do { +#define RAPIDJSON_MULTILINEMACRO_END \ +} while((void)0, 0) + +// adopted from Boost +#define RAPIDJSON_VERSION_CODE(x,y,z) \ + (((x)*100000) + ((y)*100) + (z)) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF + +#if defined(__GNUC__) +#define RAPIDJSON_GNUC \ + RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) +#endif + +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0)) + +#define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x)) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x) +#define RAPIDJSON_DIAG_OFF(x) \ + RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))) + +// push/pop support in Clang and GCC>=4.6 +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) +#else // GCC >= 4.2, < 4.6 +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ +#endif + +#elif defined(_MSC_VER) + +// pragma (MSVC specific) +#define RAPIDJSON_PRAGMA(x) __pragma(x) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x)) + +#define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) + +#else + +#define RAPIDJSON_DIAG_OFF(x) /* ignored */ +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ + +#endif // RAPIDJSON_DIAG_* + +/////////////////////////////////////////////////////////////////////////////// +// C++11 features + +#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \ + (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1600) + +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + +#ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) +// (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 +#else +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 0 +#endif +#endif +#if RAPIDJSON_HAS_CXX11_NOEXCEPT +#define RAPIDJSON_NOEXCEPT noexcept +#else +#define RAPIDJSON_NOEXCEPT /* noexcept */ +#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT + +// no automatic detection, yet +#ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS +#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0 +#endif + +//!@endcond + +/////////////////////////////////////////////////////////////////////////////// +// new/delete + +#ifndef RAPIDJSON_NEW +///! customization point for global \c new +#define RAPIDJSON_NEW(x) new x +#endif +#ifndef RAPIDJSON_DELETE +///! customization point for global \c delete +#define RAPIDJSON_DELETE(x) delete x +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Allocators and Encodings + +#include "allocators.h" +#include "encodings.h" + +/*! \namespace rapidjson + \brief main RapidJSON namespace + \see RAPIDJSON_NAMESPACE +*/ +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Stream + +/*! \class rapidjson::Stream + \brief Concept for reading and writing characters. + + For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). + + For write-only stream, only need to implement Put() and Flush(). + +\code +concept Stream { + typename Ch; //!< Character type of the stream. + + //! Read the current character from stream without moving the read cursor. + Ch Peek() const; + + //! Read the current character from stream and moving the read cursor to next character. + Ch Take(); + + //! Get the current read cursor. + //! \return Number of characters read from start. + size_t Tell(); + + //! Begin writing operation at the current read pointer. + //! \return The begin writer pointer. + Ch* PutBegin(); + + //! Write a character. + void Put(Ch c); + + //! Flush the buffer. + void Flush(); + + //! End the writing operation. + //! \param begin The begin write pointer returned by PutBegin(). + //! \return Number of characters written. + size_t PutEnd(Ch* begin); +} +\endcode +*/ + +//! Provides additional information for stream. +/*! + By using traits pattern, this type provides a default configuration for stream. + For custom stream, this type can be specialized for other configuration. + See TEST(Reader, CustomStringStream) in readertest.cpp for example. +*/ +template +struct StreamTraits { + //! Whether to make local copy of stream for optimization during parsing. + /*! + By default, for safety, streams do not use local copy optimization. + Stream that can be copied fast should specialize this, like StreamTraits. + */ + enum { copyOptimization = 0 }; +}; + +//! Put N copies of a character to a stream. +template +inline void PutN(Stream& stream, Ch c, size_t n) { + for (size_t i = 0; i < n; i++) + stream.Put(c); +} + +/////////////////////////////////////////////////////////////////////////////// +// StringStream + +//! Read-only string stream. +/*! \note implements Stream concept +*/ +template +struct GenericStringStream { + typedef typename Encoding::Ch Ch; + + GenericStringStream(const Ch *src) : src_(src), head_(src) {} + + Ch Peek() const { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() const { return static_cast(src_ - head_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! String stream with UTF8 encoding. +typedef GenericStringStream > StringStream; + +/////////////////////////////////////////////////////////////////////////////// +// InsituStringStream + +//! A read-write string stream. +/*! This string stream is particularly designed for in-situ parsing. + \note implements Stream concept +*/ +template +struct GenericInsituStringStream { + typedef typename Encoding::Ch Ch; + + GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} + + // Read + Ch Peek() { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() { return static_cast(src_ - head_); } + + // Write + void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } + + Ch* PutBegin() { return dst_ = src_; } + size_t PutEnd(Ch* begin) { return static_cast(dst_ - begin); } + void Flush() {} + + Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } + void Pop(size_t count) { dst_ -= count; } + + Ch* src_; + Ch* dst_; + Ch* head_; +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! Insitu string stream with UTF8 encoding. +typedef GenericInsituStringStream > InsituStringStream; + +/////////////////////////////////////////////////////////////////////////////// +// Type + +//! Type of JSON value +enum Type { + kNullType = 0, //!< null + kFalseType = 1, //!< false + kTrueType = 2, //!< true + kObjectType = 3, //!< object + kArrayType = 4, //!< array + kStringType = 5, //!< string + kNumberType = 6 //!< number +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/reader.h b/neo/libs/rapidjson/include/rapidjson/reader.h new file mode 100644 index 00000000..c5ecf4be --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/reader.h @@ -0,0 +1,1452 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_READER_H_ +#define RAPIDJSON_READER_H_ + +/*! \file reader.h */ + +#include "rapidjson.h" +#include "encodings.h" +#include "internal/meta.h" +#include "internal/stack.h" +#include "internal/strtod.h" + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define RAPIDJSON_NOTHING /* deliberately empty */ +#ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + if (HasParseError()) { return value; } \ + RAPIDJSON_MULTILINEMACRO_END +#endif +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING) +//!@endcond + +/*! \def RAPIDJSON_PARSE_ERROR_NORETURN + \ingroup RAPIDJSON_ERRORS + \brief Macro to indicate a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + This macros can be used as a customization point for the internal + error handling mechanism of RapidJSON. + + A common usage model is to throw an exception instead of requiring the + caller to explicitly check the \ref rapidjson::GenericReader::Parse's + return value: + + \code + #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \ + throw ParseException(parseErrorCode, #parseErrorCode, offset) + + #include // std::runtime_error + #include "rapidjson/error/error.h" // rapidjson::ParseResult + + struct ParseException : std::runtime_error, rapidjson::ParseResult { + ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset) + : std::runtime_error(msg), ParseResult(code, offset) {} + }; + + #include "rapidjson/reader.h" + \endcode + + \see RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse + */ +#ifndef RAPIDJSON_PARSE_ERROR_NORETURN +#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \ + SetParseError(parseErrorCode, offset); \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +/*! \def RAPIDJSON_PARSE_ERROR + \ingroup RAPIDJSON_ERRORS + \brief (Internal) macro to indicate and handle a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing. + + \see RAPIDJSON_PARSE_ERROR_NORETURN + \hideinitializer + */ +#ifndef RAPIDJSON_PARSE_ERROR +#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +#include "error/error.h" // ParseErrorCode, ParseResult + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseFlag + +/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kParseDefaultFlags definition. + + User can define this as any \c ParseFlag combinations. +*/ +#ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS +#define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags +#endif + +//! Combination of parseFlags +/*! \see Reader::Parse, Document::Parse, Document::ParseInsitu, Document::ParseStream + */ +enum ParseFlag { + kParseNoFlags = 0, //!< No flags are set. + kParseInsituFlag = 1, //!< In-situ(destructive) parsing. + kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings. + kParseIterativeFlag = 4, //!< Iterative(constant complexity in terms of function call stack size) parsing. + kParseStopWhenDoneFlag = 8, //!< After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error. + kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower). + kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS +}; + +/////////////////////////////////////////////////////////////////////////////// +// Handler + +/*! \class rapidjson::Handler + \brief Concept for receiving events from GenericReader upon parsing. + The functions return true if no error occurs. If they return false, + the event publisher should terminate the process. +\code +concept Handler { + typename Ch; + + bool Null(); + bool Bool(bool b); + bool Int(int i); + bool Uint(unsigned i); + bool Int64(int64_t i); + bool Uint64(uint64_t i); + bool Double(double d); + bool String(const Ch* str, SizeType length, bool copy); + bool StartObject(); + bool Key(const Ch* str, SizeType length, bool copy); + bool EndObject(SizeType memberCount); + bool StartArray(); + bool EndArray(SizeType elementCount); +}; +\endcode +*/ +/////////////////////////////////////////////////////////////////////////////// +// BaseReaderHandler + +//! Default implementation of Handler. +/*! This can be used as base class of any reader handler. + \note implements Handler concept +*/ +template, typename Derived = void> +struct BaseReaderHandler { + typedef typename Encoding::Ch Ch; + + typedef typename internal::SelectIf, BaseReaderHandler, Derived>::Type Override; + + bool Default() { return true; } + bool Null() { return static_cast(*this).Default(); } + bool Bool(bool) { return static_cast(*this).Default(); } + bool Int(int) { return static_cast(*this).Default(); } + bool Uint(unsigned) { return static_cast(*this).Default(); } + bool Int64(int64_t) { return static_cast(*this).Default(); } + bool Uint64(uint64_t) { return static_cast(*this).Default(); } + bool Double(double) { return static_cast(*this).Default(); } + bool String(const Ch*, SizeType, bool) { return static_cast(*this).Default(); } + bool StartObject() { return static_cast(*this).Default(); } + bool Key(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool EndObject(SizeType) { return static_cast(*this).Default(); } + bool StartArray() { return static_cast(*this).Default(); } + bool EndArray(SizeType) { return static_cast(*this).Default(); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// StreamLocalCopy + +namespace internal { + +template::copyOptimization> +class StreamLocalCopy; + +//! Do copy optimization. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original), original_(original) {} + ~StreamLocalCopy() { original_ = s; } + + Stream s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; + + Stream& original_; +}; + +//! Keep reference. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original) {} + + Stream& s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// SkipWhitespace + +//! Skip the JSON white spaces in a stream. +/*! \param is A input stream for skipping white spaces. + \note This function has SSE2/SSE4.2 specialization. +*/ +template +void SkipWhitespace(InputStream& is) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t') + s.Take(); +} + +#ifdef RAPIDJSON_SSE42 +//! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & ~15); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_load_si128((const __m128i *)&whitespace[0]); + + for (;; p += 16) { + const __m128i s = _mm_load_si128((const __m128i *)p); + const unsigned r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +#elif defined(RAPIDJSON_SSE2) + +//! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & ~15); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string + static const char whitespaces[4][17] = { + " ", + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", + "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r", + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"}; + + const __m128i w0 = _mm_loadu_si128((const __m128i *)&whitespaces[0][0]); + const __m128i w1 = _mm_loadu_si128((const __m128i *)&whitespaces[1][0]); + const __m128i w2 = _mm_loadu_si128((const __m128i *)&whitespaces[2][0]); + const __m128i w3 = _mm_loadu_si128((const __m128i *)&whitespaces[3][0]); + + for (;; p += 16) { + const __m128i s = _mm_load_si128((const __m128i *)p); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = (unsigned short)~_mm_movemask_epi8(x); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +#endif // RAPIDJSON_SSE2 + +#ifdef RAPIDJSON_SIMD +//! Template function specialization for InsituStringStream +template<> inline void SkipWhitespace(InsituStringStream& is) { + is.src_ = const_cast(SkipWhitespace_SIMD(is.src_)); +} + +//! Template function specialization for StringStream +template<> inline void SkipWhitespace(StringStream& is) { + is.src_ = SkipWhitespace_SIMD(is.src_); +} +#endif // RAPIDJSON_SIMD + +/////////////////////////////////////////////////////////////////////////////// +// GenericReader + +//! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator. +/*! GenericReader parses JSON text from a stream, and send events synchronously to an + object implementing Handler concept. + + It needs to allocate a stack for storing a single decoded string during + non-destructive parsing. + + For in-situ parsing, the decoded string is directly written to the source + text string, no temporary buffer is required. + + A GenericReader object can be reused for parsing multiple JSON text. + + \tparam SourceEncoding Encoding of the input stream. + \tparam TargetEncoding Encoding of the parse output. + \tparam StackAllocator Allocator type for stack. +*/ +template +class GenericReader { +public: + typedef typename SourceEncoding::Ch Ch; //!< SourceEncoding character type + + //! Constructor. + /*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) + \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing) + */ + GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {} + + //! Parse JSON text. + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + if (parseFlags & kParseIterativeFlag) + return IterativeParse(is, handler); + + parseResult_.Clear(); + + ClearStackOnExit scope(*this); + + SkipWhitespace(is); + + if (is.Peek() == '\0') { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentEmpty, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + else { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (!(parseFlags & kParseStopWhenDoneFlag)) { + SkipWhitespace(is); + + if (is.Peek() != '\0') { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + } + } + + return parseResult_; + } + + //! Parse JSON text (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + return Parse(is, handler); + } + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + +protected: + void SetParseError(ParseErrorCode code, size_t offset) { parseResult_.Set(code, offset); } + +private: + // Prohibit copy constructor & assignment operator. + GenericReader(const GenericReader&); + GenericReader& operator=(const GenericReader&); + + void ClearStack() { stack_.Clear(); } + + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericReader& r) : r_(r) {} + ~ClearStackOnExit() { r_.ClearStack(); } + private: + GenericReader& r_; + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + }; + + // Parse object: { string : value, ... } + template + void ParseObject(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '{'); + is.Take(); // Skip '{' + + if (!handler.StartObject()) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespace(is); + + if (is.Peek() == '}') { + is.Take(); + if (!handler.EndObject(0)) // empty object + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType memberCount = 0;;) { + if (is.Peek() != '"') + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); + + ParseString(is, handler, true); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespace(is); + + if (is.Take() != ':') + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); + + SkipWhitespace(is); + + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespace(is); + + ++memberCount; + + switch (is.Take()) { + case ',': SkipWhitespace(is); break; + case '}': + if (!handler.EndObject(memberCount)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + default: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); + } + } + } + + // Parse array: [ value, ... ] + template + void ParseArray(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '['); + is.Take(); // Skip '[' + + if (!handler.StartArray()) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespace(is); + + if (is.Peek() == ']') { + is.Take(); + if (!handler.EndArray(0)) // empty array + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType elementCount = 0;;) { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++elementCount; + SkipWhitespace(is); + + switch (is.Take()) { + case ',': SkipWhitespace(is); break; + case ']': + if (!handler.EndArray(elementCount)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + default: RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + } + } + } + + template + void ParseNull(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'n'); + is.Take(); + + if (is.Take() == 'u' && is.Take() == 'l' && is.Take() == 'l') { + if (!handler.Null()) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell() - 1); + } + + template + void ParseTrue(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 't'); + is.Take(); + + if (is.Take() == 'r' && is.Take() == 'u' && is.Take() == 'e') { + if (!handler.Bool(true)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell() - 1); + } + + template + void ParseFalse(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'f'); + is.Take(); + + if (is.Take() == 'a' && is.Take() == 'l' && is.Take() == 's' && is.Take() == 'e') { + if (!handler.Bool(false)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell() - 1); + } + + // Helper function to parse four hexidecimal digits in \uXXXX in ParseString(). + template + unsigned ParseHex4(InputStream& is) { + unsigned codepoint = 0; + for (int i = 0; i < 4; i++) { + Ch c = is.Take(); + codepoint <<= 4; + codepoint += static_cast(c); + if (c >= '0' && c <= '9') + codepoint -= '0'; + else if (c >= 'A' && c <= 'F') + codepoint -= 'A' - 10; + else if (c >= 'a' && c <= 'f') + codepoint -= 'a' - 10; + else { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorStringUnicodeEscapeInvalidHex, is.Tell() - 1); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0); + } + } + return codepoint; + } + + template + class StackStream { + public: + typedef CharType Ch; + + StackStream(internal::Stack& stack) : stack_(stack), length_(0) {} + RAPIDJSON_FORCEINLINE void Put(Ch c) { + *stack_.template Push() = c; + ++length_; + } + size_t Length() const { return length_; } + Ch* Pop() { + return stack_.template Pop(length_); + } + + private: + StackStream(const StackStream&); + StackStream& operator=(const StackStream&); + + internal::Stack& stack_; + SizeType length_; + }; + + // Parse string and generate String event. Different code paths for kParseInsituFlag. + template + void ParseString(InputStream& is, Handler& handler, bool isKey = false) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + bool success = false; + if (parseFlags & kParseInsituFlag) { + typename InputStream::Ch *head = s.PutBegin(); + ParseStringToStream(s, s); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + size_t length = s.PutEnd(head) - 1; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + const typename TargetEncoding::Ch* const str = (typename TargetEncoding::Ch*)head; + success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false)); + } + else { + StackStream stackStream(stack_); + ParseStringToStream(s, stackStream); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + SizeType length = static_cast(stackStream.Length()) - 1; + const typename TargetEncoding::Ch* const str = stackStream.Pop(); + success = (isKey ? handler.Key(str, length, true) : handler.String(str, length, true)); + } + if (!success) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); + } + + // Parse string to an output is + // This function handles the prefix/suffix double quotes, escaping, and optional encoding validation. + template + RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream& is, OutputStream& os) { +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + static const char escape[256] = { + Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/', + Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, + 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, + 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 + }; +#undef Z16 +//!@endcond + + RAPIDJSON_ASSERT(is.Peek() == '\"'); + is.Take(); // Skip '\"' + + for (;;) { + Ch c = is.Peek(); + if (c == '\\') { // Escape + is.Take(); + Ch e = is.Take(); + if ((sizeof(Ch) == 1 || unsigned(e) < 256) && escape[(unsigned char)e]) { + os.Put(escape[(unsigned char)e]); + } + else if (e == 'u') { // Unicode + unsigned codepoint = ParseHex4(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (codepoint >= 0xD800 && codepoint <= 0xDBFF) { + // Handle UTF-16 surrogate pair + if (is.Take() != '\\' || is.Take() != 'u') + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, is.Tell() - 2); + unsigned codepoint2 = ParseHex4(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (codepoint2 < 0xDC00 || codepoint2 > 0xDFFF) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, is.Tell() - 2); + codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000; + } + TEncoding::Encode(os, codepoint); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell() - 1); + } + else if (c == '"') { // Closing double quote + is.Take(); + os.Put('\0'); // null-terminate the string + return; + } + else if (c == '\0') + RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell() - 1); + else if ((unsigned)c < 0x20) // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell() - 1); + else { + if (parseFlags & kParseValidateEncodingFlag ? + !Transcoder::Validate(is, os) : + !Transcoder::Transcode(is, os)) + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, is.Tell()); + } + } + } + + template + class NumberStream; + + template + class NumberStream { + public: + NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; } + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); } + RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); } + RAPIDJSON_FORCEINLINE Ch Take() { return is.Take(); } + size_t Tell() { return is.Tell(); } + size_t Length() { return 0; } + const char* Pop() { return 0; } + + protected: + NumberStream& operator=(const NumberStream&); + + InputStream& is; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : NumberStream(reader, is), stackStream(reader.stack_) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch TakePush() { + stackStream.Put((char)Base::is.Peek()); + return Base::is.Take(); + } + + size_t Length() { return stackStream.Length(); } + + const char* Pop() { + stackStream.Put('\0'); + return stackStream.Pop(); + } + + private: + StackStream stackStream; + }; + + template + void ParseNumber(InputStream& is, Handler& handler) { + internal::StreamLocalCopy copy(is); + NumberStream s(*this, copy.s); + + // Parse minus + bool minus = false; + if (s.Peek() == '-') { + minus = true; + s.Take(); + } + + // Parse int: zero / ( digit1-9 *DIGIT ) + unsigned i = 0; + uint64_t i64 = 0; + bool use64bit = false; + int significandDigit = 0; + if (s.Peek() == '0') { + i = 0; + s.TakePush(); + } + else if (s.Peek() >= '1' && s.Peek() <= '9') { + i = static_cast(s.TakePush() - '0'); + + if (minus) + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (i >= 214748364) { // 2^31 = 2147483648 + if (i != 214748364 || s.Peek() > '8') { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (i >= 429496729) { // 2^32 - 1 = 4294967295 + if (i != 429496729 || s.Peek() > '5') { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + + // Parse 64bit int + bool useDouble = false; + double d = 0.0; + if (use64bit) { + if (minus) + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC)) // 2^63 = 9223372036854775808 + if (i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8') { + d = i64; + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999)) // 2^64 - 1 = 18446744073709551615 + if (i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5') { + d = i64; + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + + // Force double for big integer + if (useDouble) { + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (d >= 1.7976931348623157e307) // DBL_MAX / 10.0 + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, s.Tell()); + d = d * 10 + (s.TakePush() - '0'); + } + } + + // Parse frac = decimal-point 1*DIGIT + int expFrac = 0; + size_t decimalPosition; + if (s.Peek() == '.') { + s.Take(); + decimalPosition = s.Length(); + + if (!(s.Peek() >= '0' && s.Peek() <= '9')) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissFraction, s.Tell()); + + if (!useDouble) { +#if RAPIDJSON_64BIT + // Use i64 to store significand in 64-bit architecture + if (!use64bit) + i64 = i; + + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path + break; + else { + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + --expFrac; + if (i64 != 0) + significandDigit++; + } + } + + d = (double)i64; +#else + // Use double to store significand in 32-bit architecture + d = use64bit ? (double)i64 : (double)i; +#endif + useDouble = true; + } + + while (s.Peek() >= '0' && s.Peek() <= '9') { + if (significandDigit < 17) { + d = d * 10.0 + (s.TakePush() - '0'); + --expFrac; + if (d > 0.0) + significandDigit++; + } + else + s.TakePush(); + } + } + else + decimalPosition = s.Length(); // decimal position at the end of integer. + + // Parse exp = e [ minus / plus ] 1*DIGIT + int exp = 0; + if (s.Peek() == 'e' || s.Peek() == 'E') { + if (!useDouble) { + d = use64bit ? i64 : i; + useDouble = true; + } + s.Take(); + + bool expMinus = false; + if (s.Peek() == '+') + s.Take(); + else if (s.Peek() == '-') { + s.Take(); + expMinus = true; + } + + if (s.Peek() >= '0' && s.Peek() <= '9') { + exp = s.Take() - '0'; + if (expMinus) { + while (s.Peek() >= '0' && s.Peek() <= '9') { + exp = exp * 10 + (s.Take() - '0'); + if (exp >= 214748364) { // Issue #313: prevent overflow exponent + while (s.Peek() >= '0' && s.Peek() <= '9') // Consume the rest of exponent + s.Take(); + } + } + } + else { // positive exp + int maxExp = 308 - expFrac; + while (s.Peek() >= '0' && s.Peek() <= '9') { + exp = exp * 10 + (s.Take() - '0'); + if (exp > maxExp) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, s.Tell()); + } + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissExponent, s.Tell()); + + if (expMinus) + exp = -exp; + } + + // Finish parsing, call event according to the type of number. + bool cont = true; + size_t length = s.Length(); + const char* decimal = s.Pop(); // Pop stack no matter if it will be used or not. + + if (useDouble) { + int p = exp + expFrac; + if (parseFlags & kParseFullPrecisionFlag) + d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp); + else + d = internal::StrtodNormalPrecision(d, p); + + cont = handler.Double(minus ? -d : d); + } + else { + if (use64bit) { + if (minus) + cont = handler.Int64(-(int64_t)i64); + else + cont = handler.Uint64(i64); + } + else { + if (minus) + cont = handler.Int(-(int)i); + else + cont = handler.Uint(i); + } + } + if (!cont) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); + } + + // Parse any JSON value + template + void ParseValue(InputStream& is, Handler& handler) { + switch (is.Peek()) { + case 'n': ParseNull (is, handler); break; + case 't': ParseTrue (is, handler); break; + case 'f': ParseFalse (is, handler); break; + case '"': ParseString(is, handler); break; + case '{': ParseObject(is, handler); break; + case '[': ParseArray (is, handler); break; + default : ParseNumber(is, handler); + } + } + + // Iterative Parsing + + // States + enum IterativeParsingState { + IterativeParsingStartState = 0, + IterativeParsingFinishState, + IterativeParsingErrorState, + + // Object states + IterativeParsingObjectInitialState, + IterativeParsingMemberKeyState, + IterativeParsingKeyValueDelimiterState, + IterativeParsingMemberValueState, + IterativeParsingMemberDelimiterState, + IterativeParsingObjectFinishState, + + // Array states + IterativeParsingArrayInitialState, + IterativeParsingElementState, + IterativeParsingElementDelimiterState, + IterativeParsingArrayFinishState, + + // Single value state + IterativeParsingValueState, + + cIterativeParsingStateCount + }; + + // Tokens + enum Token { + LeftBracketToken = 0, + RightBracketToken, + + LeftCurlyBracketToken, + RightCurlyBracketToken, + + CommaToken, + ColonToken, + + StringToken, + FalseToken, + TrueToken, + NullToken, + NumberToken, + + kTokenCount + }; + + RAPIDJSON_FORCEINLINE Token Tokenize(Ch c) { + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define N NumberToken +#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N + // Maps from ASCII to Token + static const unsigned char tokenMap[256] = { + N16, // 00~0F + N16, // 10~1F + N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N, // 20~2F + N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N, // 30~3F + N16, // 40~4F + N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N, // 50~5F + N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N, // 60~6F + N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N, // 70~7F + N16, N16, N16, N16, N16, N16, N16, N16 // 80~FF + }; +#undef N +#undef N16 +//!@endcond + + if (sizeof(Ch) == 1 || unsigned(c) < 256) + return (Token)tokenMap[(unsigned char)c]; + else + return NumberToken; + } + + RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) { + // current state x one lookahead token -> new state + static const char G[cIterativeParsingStateCount][kTokenCount] = { + // Start + { + IterativeParsingArrayInitialState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingValueState, // String + IterativeParsingValueState, // False + IterativeParsingValueState, // True + IterativeParsingValueState, // Null + IterativeParsingValueState // Number + }, + // Finish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Error(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ObjectInitial + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberKey + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingKeyValueDelimiterState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // KeyValueDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberValueState, // String + IterativeParsingMemberValueState, // False + IterativeParsingMemberValueState, // True + IterativeParsingMemberValueState, // Null + IterativeParsingMemberValueState // Number + }, + // MemberValue + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingMemberDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberDelimiter + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ObjectFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ArrayInitial + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // Element + { + IterativeParsingErrorState, // Left bracket + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingElementDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ElementDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // ArrayFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Single Value (sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + } + }; // End of G + + return (IterativeParsingState)G[state][token]; + } + + // Make an advance in the token stream and state based on the candidate destination state which was returned by Transit(). + // May return a new state on state pop. + template + RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) { + (void)token; + + switch (dst) { + case IterativeParsingErrorState: + return dst; + + case IterativeParsingObjectInitialState: + case IterativeParsingArrayInitialState: + { + // Push the state(Element or MemeberValue) if we are nested in another array or value of member. + // In this way we can get the correct state on ObjectFinish or ArrayFinish by frame pop. + IterativeParsingState n = src; + if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState) + n = IterativeParsingElementState; + else if (src == IterativeParsingKeyValueDelimiterState) + n = IterativeParsingMemberValueState; + // Push current state. + *stack_.template Push(1) = n; + // Initialize and push the member/element count. + *stack_.template Push(1) = 0; + // Call handler + bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray(); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return dst; + } + } + + case IterativeParsingMemberKeyState: + ParseString(is, handler, true); + if (HasParseError()) + return IterativeParsingErrorState; + else + return dst; + + case IterativeParsingKeyValueDelimiterState: + RAPIDJSON_ASSERT(token == ColonToken); + is.Take(); + return dst; + + case IterativeParsingMemberValueState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingElementState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingMemberDelimiterState: + case IterativeParsingElementDelimiterState: + is.Take(); + // Update member/element count. + *stack_.template Top() = *stack_.template Top() + 1; + return dst; + + case IterativeParsingObjectFinishState: + { + // Get member count. + SizeType c = *stack_.template Pop(1); + // If the object is not empty, count the last member. + if (src == IterativeParsingMemberValueState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndObject(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + case IterativeParsingArrayFinishState: + { + // Get element count. + SizeType c = *stack_.template Pop(1); + // If the array is not empty, count the last element. + if (src == IterativeParsingElementState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndArray(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + default: + // This branch is for IterativeParsingValueState actually. + // Use `default:` rather than + // `case IterativeParsingValueState:` is for code coverage. + + // The IterativeParsingStartState is not enumerated in this switch-case. + // It is impossible for that case. And it can be caught by following assertion. + + // The IterativeParsingFinishState is not enumerated in this switch-case either. + // It is a "derivative" state which cannot triggered from Predict() directly. + // Therefore it cannot happen here. And it can be caught by following assertion. + RAPIDJSON_ASSERT(dst == IterativeParsingValueState); + + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return IterativeParsingFinishState; + } + } + + template + void HandleError(IterativeParsingState src, InputStream& is) { + if (HasParseError()) { + // Error flag has been set. + return; + } + + switch (src) { + case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); + case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); + case IterativeParsingObjectInitialState: + case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); + case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); + case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); + case IterativeParsingElementState: RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + default: RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + } + } + + template + ParseResult IterativeParse(InputStream& is, Handler& handler) { + parseResult_.Clear(); + ClearStackOnExit scope(*this); + IterativeParsingState state = IterativeParsingStartState; + + SkipWhitespace(is); + while (is.Peek() != '\0') { + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state, t); + IterativeParsingState d = Transit(state, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state, is); + break; + } + + state = d; + + // Do not further consume streams if a root JSON has been parsed. + if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) + break; + + SkipWhitespace(is); + } + + // Handle the end of file. + if (state != IterativeParsingFinishState) + HandleError(state, is); + + return parseResult_; + } + + static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string. + internal::Stack stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing. + ParseResult parseResult_; +}; // class GenericReader + +//! Reader with UTF8 encoding and default allocator. +typedef GenericReader, UTF8<> > Reader; + +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_READER_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/stringbuffer.h b/neo/libs/rapidjson/include/rapidjson/stringbuffer.h new file mode 100644 index 00000000..1c9c80b7 --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/stringbuffer.h @@ -0,0 +1,93 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRINGBUFFER_H_ +#define RAPIDJSON_STRINGBUFFER_H_ + +#include "rapidjson.h" + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +#include "internal/stack.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output stream. +/*! + \tparam Encoding Encoding of the stream. + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +class GenericStringBuffer { +public: + typedef typename Encoding::Ch Ch; + + GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} + GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { + if (&rhs != this) + stack_ = std::move(rhs.stack_); + return *this; + } +#endif + + void Put(Ch c) { *stack_.template Push() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.ShrinkToFit(); + stack_.template Pop(1); + } + Ch* Push(size_t count) { return stack_.template Push(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetString() const { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.template Pop(1); + + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; + +private: + // Prohibit copy constructor & assignment operator. + GenericStringBuffer(const GenericStringBuffer&); + GenericStringBuffer& operator=(const GenericStringBuffer&); +}; + +//! String buffer with UTF8 encoding +typedef GenericStringBuffer > StringBuffer; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { + std::memset(stream.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STRINGBUFFER_H_ diff --git a/neo/libs/rapidjson/include/rapidjson/writer.h b/neo/libs/rapidjson/include/rapidjson/writer.h new file mode 100644 index 00000000..e1eea38b --- /dev/null +++ b/neo/libs/rapidjson/include/rapidjson/writer.h @@ -0,0 +1,395 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_WRITER_H_ +#define RAPIDJSON_WRITER_H_ + +#include "rapidjson.h" +#include "internal/stack.h" +#include "internal/strfunc.h" +#include "internal/dtoa.h" +#include "internal/itoa.h" +#include "stringbuffer.h" +#include // placement new + +#if RAPIDJSON_HAS_STDSTRING +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! JSON writer +/*! Writer implements the concept Handler. + It generates JSON text by events to an output os. + + User may programmatically calls the functions of a writer to generate JSON text. + + On the other side, a writer can also be passed to objects that generates events, + + for example Reader::Parse() and Document::Accept(). + + \tparam OutputStream Type of output stream. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. + \note implements Handler concept +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator> +class Writer { +public: + typedef typename SourceEncoding::Ch Ch; + + //! Constructor + /*! \param os Output stream. + \param stackAllocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit + Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), hasRoot_(false) {} + + explicit + Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {} + + //! Reset the writer with a new stream. + /*! + This function reset the writer with a new stream and default settings, + in order to make a Writer object reusable for output multiple JSONs. + + \param os New output stream. + \code + Writer writer(os1); + writer.StartObject(); + // ... + writer.EndObject(); + + writer.Reset(os2); + writer.StartObject(); + // ... + writer.EndObject(); + \endcode + */ + void Reset(OutputStream& os) { + os_ = &os; + hasRoot_ = false; + level_stack_.Clear(); + } + + //! Checks whether the output is a complete JSON. + /*! + A complete JSON has a complete root object or array. + */ + bool IsComplete() const { + return hasRoot_ && level_stack_.Empty(); + } + + /*!@name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { Prefix(kNullType); return WriteNull(); } + bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); } + bool Int(int i) { Prefix(kNumberType); return WriteInt(i); } + bool Uint(unsigned u) { Prefix(kNumberType); return WriteUint(u); } + bool Int64(int64_t i64) { Prefix(kNumberType); return WriteInt64(i64); } + bool Uint64(uint64_t u64) { Prefix(kNumberType); return WriteUint64(u64); } + + //! Writes the given \c double value to the stream + /*! + \param d The value to be written. + \return Whether it is succeed. + */ + bool Double(double d) { Prefix(kNumberType); return WriteDouble(d); } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kStringType); + return WriteString(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + Prefix(kObjectType); + new (level_stack_.template Push()) Level(false); + return WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + bool ret = WriteEndObject(); + if (level_stack_.Empty()) // end of json text + os_->Flush(); + return ret; + } + + bool StartArray() { + Prefix(kArrayType); + new (level_stack_.template Push()) Level(true); + return WriteStartArray(); + } + + bool EndArray(SizeType elementCount = 0) { + (void)elementCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + bool ret = WriteEndArray(); + if (level_stack_.Empty()) // end of json text + os_->Flush(); + return ret; + } + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + +protected: + //! Information for each nested level + struct Level { + Level(bool inArray_) : valueCount(0), inArray(inArray_) {} + size_t valueCount; //!< number of values in this level + bool inArray; //!< true if in array, otherwise in object + }; + + static const size_t kDefaultLevelDepth = 32; + + bool WriteNull() { + os_->Put('n'); os_->Put('u'); os_->Put('l'); os_->Put('l'); return true; + } + + bool WriteBool(bool b) { + if (b) { + os_->Put('t'); os_->Put('r'); os_->Put('u'); os_->Put('e'); + } + else { + os_->Put('f'); os_->Put('a'); os_->Put('l'); os_->Put('s'); os_->Put('e'); + } + return true; + } + + bool WriteInt(int i) { + char buffer[11]; + const char* end = internal::i32toa(i, buffer); + for (const char* p = buffer; p != end; ++p) + os_->Put(*p); + return true; + } + + bool WriteUint(unsigned u) { + char buffer[10]; + const char* end = internal::u32toa(u, buffer); + for (const char* p = buffer; p != end; ++p) + os_->Put(*p); + return true; + } + + bool WriteInt64(int64_t i64) { + char buffer[21]; + const char* end = internal::i64toa(i64, buffer); + for (const char* p = buffer; p != end; ++p) + os_->Put(*p); + return true; + } + + bool WriteUint64(uint64_t u64) { + char buffer[20]; + char* end = internal::u64toa(u64, buffer); + for (char* p = buffer; p != end; ++p) + os_->Put(*p); + return true; + } + + bool WriteDouble(double d) { + char buffer[25]; + char* end = internal::dtoa(d, buffer); + for (char* p = buffer; p != end; ++p) + os_->Put(*p); + return true; + } + + bool WriteString(const Ch* str, SizeType length) { + static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const char escape[256] = { +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + //0 1 2 3 4 5 6 7 8 9 A B C D E F + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 + 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 + Z16, Z16, // 30~4F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF +#undef Z16 + }; + + os_->Put('\"'); + GenericStringStream is(str); + while (is.Tell() < length) { + const Ch c = is.Peek(); + if (!TargetEncoding::supportUnicode && (unsigned)c >= 0x80) { + // Unicode escaping + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + os_->Put('\\'); + os_->Put('u'); + if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { + os_->Put(hexDigits[(codepoint >> 12) & 15]); + os_->Put(hexDigits[(codepoint >> 8) & 15]); + os_->Put(hexDigits[(codepoint >> 4) & 15]); + os_->Put(hexDigits[(codepoint ) & 15]); + } + else { + RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); + // Surrogate pair + unsigned s = codepoint - 0x010000; + unsigned lead = (s >> 10) + 0xD800; + unsigned trail = (s & 0x3FF) + 0xDC00; + os_->Put(hexDigits[(lead >> 12) & 15]); + os_->Put(hexDigits[(lead >> 8) & 15]); + os_->Put(hexDigits[(lead >> 4) & 15]); + os_->Put(hexDigits[(lead ) & 15]); + os_->Put('\\'); + os_->Put('u'); + os_->Put(hexDigits[(trail >> 12) & 15]); + os_->Put(hexDigits[(trail >> 8) & 15]); + os_->Put(hexDigits[(trail >> 4) & 15]); + os_->Put(hexDigits[(trail ) & 15]); + } + } + else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) { + is.Take(); + os_->Put('\\'); + os_->Put(escape[(unsigned char)c]); + if (escape[(unsigned char)c] == 'u') { + os_->Put('0'); + os_->Put('0'); + os_->Put(hexDigits[(unsigned char)c >> 4]); + os_->Put(hexDigits[(unsigned char)c & 0xF]); + } + } + else + if (!Transcoder::Transcode(is, *os_)) + return false; + } + os_->Put('\"'); + return true; + } + + bool WriteStartObject() { os_->Put('{'); return true; } + bool WriteEndObject() { os_->Put('}'); return true; } + bool WriteStartArray() { os_->Put('['); return true; } + bool WriteEndArray() { os_->Put(']'); return true; } + + void Prefix(Type type) { + (void)type; + if (level_stack_.GetSize() != 0) { // this value is not at root + Level* level = level_stack_.template Top(); + if (level->valueCount > 0) { + if (level->inArray) + os_->Put(','); // add comma if it is not the first element in array + else // in object + os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. + hasRoot_ = true; + } + } + + OutputStream* os_; + internal::Stack level_stack_; + bool hasRoot_; + +private: + // Prohibit copy constructor & assignment operator. + Writer(const Writer&); + Writer& operator=(const Writer&); +}; + +// Full specialization for StringStream to prevent memory copying + +template<> +inline bool Writer::WriteInt(int i) { + char *buffer = os_->Push(11); + const char* end = internal::i32toa(i, buffer); + os_->Pop(11 - (end - buffer)); + return true; +} + +template<> +inline bool Writer::WriteUint(unsigned u) { + char *buffer = os_->Push(10); + const char* end = internal::u32toa(u, buffer); + os_->Pop(10 - (end - buffer)); + return true; +} + +template<> +inline bool Writer::WriteInt64(int64_t i64) { + char *buffer = os_->Push(21); + const char* end = internal::i64toa(i64, buffer); + os_->Pop(21 - (end - buffer)); + return true; +} + +template<> +inline bool Writer::WriteUint64(uint64_t u) { + char *buffer = os_->Push(20); + const char* end = internal::u64toa(u, buffer); + os_->Pop(20 - (end - buffer)); + return true; +} + +template<> +inline bool Writer::WriteDouble(double d) { + char *buffer = os_->Push(25); + char* end = internal::dtoa(d, buffer); + os_->Pop(25 - (end - buffer)); + return true; +} + +RAPIDJSON_NAMESPACE_END + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/neo/libs/rapidjson/license.txt b/neo/libs/rapidjson/license.txt new file mode 100644 index 00000000..879293af --- /dev/null +++ b/neo/libs/rapidjson/license.txt @@ -0,0 +1,57 @@ +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License. +If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. +A copy of the MIT License is included in this file. + +Other dependencies and licenses: + +Open Source Software Licensed Under the BSD License: +-------------------------------------------------------------------- + +The msinttypes r29 +Copyright (c) 2006-2013 Alexander Chemeris +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Open Source Software Licensed Under the JSON License: +-------------------------------------------------------------------- + +json.org +Copyright (c) 2002 JSON.org +All Rights Reserved. + +JSON_checker +Copyright (c) 2002 JSON.org +All Rights Reserved. + + +Terms of the JSON License: +--------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +Terms of the MIT License: +-------------------------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/neo/libs/rapidjson/readme.md b/neo/libs/rapidjson/readme.md new file mode 100644 index 00000000..19da3866 --- /dev/null +++ b/neo/libs/rapidjson/readme.md @@ -0,0 +1,129 @@ +![](doc/logo/rapidjson.png) + +![](https://img.shields.io/badge/release-v1.0.2-blue.png) + +## A fast JSON parser/generator for C++ with both SAX/DOM style API + +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +* [RapidJSON GitHub](https://github.com/miloyip/rapidjson/) +* RapidJSON Documentation + * [English](http://miloyip.github.io/rapidjson/) + * [简体中文](http://miloyip.github.io/rapidjson/zh-cn/) + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) with downloadable PDF/EPUB/MOBI, without API reference. + +## Build status + +| [Linux][lin-link] | [Windows][win-link] | [Coveralls][cov-link] | +| :---------------: | :-----------------: | :-------------------: | +| ![lin-badge] | ![win-badge] | ![cov-badge] | + +[lin-badge]: https://travis-ci.org/miloyip/rapidjson.png?branch=master "Travis build status" +[lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/u658dcuwxo14a8m9/branch/master "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status" +[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.png?branch=master +[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master + +## Introduction + +RapidJSON is a JSON parser and generator for C++. It was inspired by [RapidXml](http://rapidxml.sourceforge.net/). + +* RapidJSON is small but complete. It supports both SAX and DOM style API. The SAX parser is only a half thousand lines of code. + +* RapidJSON is fast. Its performance can be comparable to `strlen()`. It also optionally supports SSE2/SSE4.2 for acceleration. + +* RapidJSON is self-contained. It does not depend on external libraries such as BOOST. It even does not depend on STL. + +* RapidJSON is memory friendly. Each JSON value occupies exactly 16/20 bytes for most 32/64-bit machines (excluding text string). By default it uses a fast memory allocator, and the parser allocates memory compactly during parsing. + +* RapidJSON is Unicode friendly. It supports UTF-8, UTF-16, UTF-32 (LE & BE), and their detection, validation and transcoding internally. For example, you can read a UTF-8 file and let RapidJSON transcode the JSON strings into UTF-16 in the DOM. It also supports surrogates and "\u0000" (null character). + +More features can be read [here](doc/features.md). + +JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in fully compliance with RFC7159/ECMA-404. More information about JSON can be obtained at +* [Introducing JSON](http://json.org/) +* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt) +* [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm) + +## Compatibility + +RapidJSON is cross-platform. Some platform/compiler combinations which have been tested are shown as follows. +* Visual C++ 2008/2010/2013 on Windows (32/64-bit) +* GNU C++ 3.8.x on Cygwin +* Clang 3.4 on Mac OS X (32/64-bit) and iOS +* Clang 3.4 on Android NDK + +Users can build and run the unit tests on their platform/compiler. + +## Installation + +RapidJSON is a header-only C++ library. Just copy the `include/rapidjson` folder to system or project's include path. + +RapidJSON uses following software as its dependencies: +* [CMake](http://www.cmake.org) as a general build tool +* (optional)[Doxygen](http://www.doxygen.org) to build documentation +* (optional)[googletest](https://code.google.com/p/googletest/) for unit and performance testing + +To generate user documentation and run tests please proceed with the steps below: + +1. Execute `git submodule update --init` to get the files of thirdparty submodules (google test). +2. Create directory called `build` in rapidjson source directory. +3. Change to `build` directory and run `cmake ..` command to configure your build. Windows users can do the same with cmake-gui application. +4. On Windows, build the solution found in the build directory. On Linux, run `make` from the build directory. + +On successfull build you will find compiled test and example binaries in `bin` +directory. The generated documentation will be available in `doc/html` +directory of the build tree. To run tests after finished build please run `make +test` or `ctest` from your build tree. You can get detailed output using `ctest +-V` command. + +It is possible to install library system-wide by running `make install` command +from the build tree with administrative privileges. This will install all files +according to system preferences. Once RapidJSON is installed, it is possible +to use it from other CMake projects by adding `find_package(RapidJSON)` line to +your CMakeLists.txt. + +## Usage at a glance + +This simple example parses a JSON string into a document (DOM), make a simple modification of the DOM, and finally stringify the DOM to a JSON string. + +~~~~~~~~~~cpp +// rapidjson/example/simpledom/simpledom.cpp` +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include + +using namespace rapidjson; + +int main() { + // 1. Parse a JSON string into DOM. + const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; + Document d; + d.Parse(json); + + // 2. Modify it by DOM. + Value& s = d["stars"]; + s.SetInt(s.GetInt() + 1); + + // 3. Stringify the DOM + StringBuffer buffer; + Writer writer(buffer); + d.Accept(writer); + + // Output {"project":"rapidjson","stars":11} + std::cout << buffer.GetString() << std::endl; + return 0; +} +~~~~~~~~~~ + +Note that this example did not handle potential errors. + +The following diagram shows the process. + +![simpledom](doc/diagram/simpledom.png) + +More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are available. From cdb204790040113f0e49f9b7ef3b1746e087f95d Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Wed, 8 Jul 2015 00:19:32 +0200 Subject: [PATCH 30/43] RapidJSON Hello World --- neo/CMakeLists.txt | 5 + neo/libs/rapidjson/include/rapidjson/reader.h | 4 +- neo/swf/SWF_Load.cpp | 321 ++++++++++++++++++ neo/swf/SWF_Main.cpp | 26 +- 4 files changed, 349 insertions(+), 7 deletions(-) diff --git a/neo/CMakeLists.txt b/neo/CMakeLists.txt index 625fbf15..8a1a0d01 100644 --- a/neo/CMakeLists.txt +++ b/neo/CMakeLists.txt @@ -595,6 +595,8 @@ set(OGGVORBIS_SOURCES libs/oggvorbis/vorbissrc/bitrate.c libs/oggvorbis/vorbissrc/vorbisfile.c) +file(GLOB RAPIDJSON_INCLUDES libs/rapidjson/include/rapidjson/*.h) + file(GLOB SYS_INCLUDES sys/*.h) file(GLOB SYS_SOURCES sys/*.cpp) @@ -985,6 +987,8 @@ source_group("libs\\zlib\\minizip" FILES ${MINIZIP_SOURCES}) source_group("libs\\glew" FILES ${GLEW_INCLUDES}) source_group("libs\\glew" FILES ${GLEW_SOURCES}) +source_group("libs\\rapidjson" FILES ${RAPIDJSON_INCLUDES}) + source_group("libs\\freetype" FILES ${FREETYPE_SOURCES}) source_group("sound" FILES ${SOUND_INCLUDES}) @@ -1135,6 +1139,7 @@ set(RBDOOM3_INCLUDES ${ZLIB_INCLUDES} ${MINIZIP_INCLUDES} ${GLEW_INCLUDES} + ${RAPIDJSON_INCLUDES} #${FREETYPE_SOURCES} ${SOUND_INCLUDES} #${OGGVORBIS_INCLUDES} diff --git a/neo/libs/rapidjson/include/rapidjson/reader.h b/neo/libs/rapidjson/include/rapidjson/reader.h index c5ecf4be..de79f8f1 100644 --- a/neo/libs/rapidjson/include/rapidjson/reader.h +++ b/neo/libs/rapidjson/include/rapidjson/reader.h @@ -412,8 +412,8 @@ public: SkipWhitespace(is); if (is.Peek() != '\0') { - RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); - RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + //RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); + //RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); } } } diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 05a8554e..988db949 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -30,6 +30,9 @@ If you have questions concerning this license or the applicable additional terms #include "precompiled.h" #include "../renderer/Font.h" #include "../renderer/Image.h" +#include "../../libs/rapidjson/include/rapidjson/document.h" + +using namespace rapidjson; #pragma warning(disable: 4355) // 'this' : used in base member initializer list @@ -824,6 +827,324 @@ void idSWF::SetBackgroundColor( idSWFBitStream& bitstream ) // RB begin +struct MyHandler +{ + bool Null() + { + idLib::Printf( "Null()\n" ); + return true; + } + bool Bool( bool b ) + { + idLib::Printf( "Bool( %i )\n", b ); + return true; + } + bool Int( int i ) + { + idLib::Printf( "Int( %i )\n", i ) ; + return true; + } + bool Uint( unsigned u ) + { + idLib::Printf( "Uint( %d )\n", u ); + return true; + } + bool Int64( int64_t i ) + { + idLib::Printf( "Int64( %i )\n", i ); + return true; + } + bool Uint64( uint64_t u ) + { + idLib::Printf( "Uint64( %d )\n", u ); + return true; + } + bool Double( double d ) + { + idLib::Printf( "Double( %f )\n", d ); + return true; + } + + bool String( const char* str, SizeType length, bool copy ) + { + idLib::Printf( "String( %s, %i, %i )\n", str, length, copy ); + return true; + } + bool StartObject() + { + idLib::Printf( "StartObject()\n" ); + return true; + } + + bool Key( const char* str, SizeType length, bool copy ) + { + idLib::Printf( "Key( %s, %i, %i )\n", str, length, copy ); + return true; + } + bool EndObject( SizeType memberCount ) + { + idLib::Printf( "EndObject( %i )\n", memberCount ); + return true; + } + bool StartArray() + { + idLib::Printf( "StartArray()\n" ); + return true; + } + bool EndArray( SizeType elementCount ) + { + idLib::Printf( "EndArray( %i )\n", elementCount ); + return true; + } +}; + +/* +=================== +idSWF::LoadJSON +=================== +*/ +bool idSWF::LoadJSON( const char* bfilename ) +{ + idFile* f = fileSystem->OpenFileReadMemory( bfilename ); + if( f == NULL || f->Length() <= 0 ) + { + return false; + } + + + + int fileLength = f->Length(); + const char* fileData = ( const char* )Mem_Alloc( fileLength, TAG_SWF ); + size_t fileSize = f->Read( ( byte* ) fileData, fileLength ); + delete f; + +#if 0 + Reader reader; + StringStream ss( fileData ); + + MyHandler handler; + reader.Parse( ss, handler ); +#endif + + rapidjson::Document d; + d.Parse( fileData ); + + assert( d.IsObject() ); + + if( d.HasMember( "version" ) ) + { + Value& s = d["version"]; + int version = s.GetInt(); + + idLib::Printf( "version = %i", version ); + } + + //d.Begin + //Value& s = d["version"]; + + //ID_TIME_T btimestamp = s.GetInt64(); + //f->ReadBig( btimestamp ); + +#if 0 + f->ReadBig( frameWidth ); + f->ReadBig( frameHeight ); + f->ReadBig( frameRate ); + + if( mouseX == -1 ) + { + mouseX = ( frameWidth / 2 ); + } + + if( mouseY == -1 ) + { + mouseY = ( frameHeight / 2 ); + } + + mainsprite->Read( f ); + + int num = 0; + f->ReadBig( num ); + dictionary.SetNum( num ); + for( int i = 0; i < dictionary.Num(); i++ ) + { + f->ReadBig( dictionary[i].type ); + switch( dictionary[i].type ) + { + case SWF_DICT_IMAGE: + { + idStr imageName; + f->ReadString( imageName ); + if( imageName[0] == '.' ) + { + // internal image in the atlas + dictionary[i].material = NULL; + } + else + { + dictionary[i].material = declManager->FindMaterial( imageName ); + } + for( int j = 0 ; j < 2 ; j++ ) + { + f->ReadBig( dictionary[i].imageSize[j] ); + f->ReadBig( dictionary[i].imageAtlasOffset[j] ); + } + for( int j = 0 ; j < 4 ; j++ ) + { + f->ReadBig( dictionary[i].channelScale[j] ); + } + break; + } + case SWF_DICT_MORPH: + case SWF_DICT_SHAPE: + { + dictionary[i].shape = new( TAG_SWF ) idSWFShape; + idSWFShape* shape = dictionary[i].shape; + f->ReadBig( shape->startBounds.tl ); + f->ReadBig( shape->startBounds.br ); + f->ReadBig( shape->endBounds.tl ); + f->ReadBig( shape->endBounds.br ); + f->ReadBig( num ); + shape->fillDraws.SetNum( num ); + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + f->ReadBig( fillDraw.style.type ); + f->ReadBig( fillDraw.style.subType ); + f->Read( &fillDraw.style.startColor, 4 ); + f->Read( &fillDraw.style.endColor, 4 ); + f->ReadBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); + f->ReadBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); + f->ReadBig( fillDraw.style.gradient.numGradients ); + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + f->ReadBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); + f->ReadBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); + f->Read( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); + f->Read( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); + } + f->ReadBig( fillDraw.style.focalPoint ); + f->ReadBig( fillDraw.style.bitmapID ); + f->ReadBig( num ); + fillDraw.startVerts.SetNum( num ); + f->ReadBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); + f->ReadBig( num ); + fillDraw.endVerts.SetNum( num ); + f->ReadBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); + f->ReadBig( num ); + fillDraw.indices.SetNum( num ); + f->ReadBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() ); + } + f->ReadBig( num ); + shape->lineDraws.SetNum( num ); + for( int d = 0; d < shape->lineDraws.Num(); d++ ) + { + idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + f->ReadBig( lineDraw.style.startWidth ); + f->ReadBig( lineDraw.style.endWidth ); + f->Read( &lineDraw.style.startColor, 4 ); + f->Read( &lineDraw.style.endColor, 4 ); + f->ReadBig( num ); + lineDraw.startVerts.SetNum( num ); + f->ReadBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); + f->ReadBig( num ); + lineDraw.endVerts.SetNum( num ); + f->ReadBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); + f->ReadBig( num ); + lineDraw.indices.SetNum( num ); + f->ReadBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); + } + break; + } + case SWF_DICT_SPRITE: + { + dictionary[i].sprite = new( TAG_SWF ) idSWFSprite( this ); + dictionary[i].sprite->Read( f ); + break; + } + case SWF_DICT_FONT: + { + dictionary[i].font = new( TAG_SWF ) idSWFFont; + idSWFFont* font = dictionary[i].font; + idStr fontName; + f->ReadString( fontName ); + font->fontID = renderSystem->RegisterFont( fontName ); + f->ReadBig( font->ascent ); + f->ReadBig( font->descent ); + f->ReadBig( font->leading ); + f->ReadBig( num ); + font->glyphs.SetNum( num ); + for( int g = 0; g < font->glyphs.Num(); g++ ) + { + f->ReadBig( font->glyphs[g].code ); + f->ReadBig( font->glyphs[g].advance ); + f->ReadBig( num ); + font->glyphs[g].verts.SetNum( num ); + f->ReadBigArray( font->glyphs[g].verts.Ptr(), font->glyphs[g].verts.Num() ); + f->ReadBig( num ); + font->glyphs[g].indices.SetNum( num ); + f->ReadBigArray( font->glyphs[g].indices.Ptr(), font->glyphs[g].indices.Num() ); + } + break; + } + case SWF_DICT_TEXT: + { + dictionary[i].text = new( TAG_SWF ) idSWFText; + idSWFText* text = dictionary[i].text; + f->ReadBig( text->bounds.tl ); + f->ReadBig( text->bounds.br ); + f->ReadBigArray( ( float* )&text->matrix, 6 ); + f->ReadBig( num ); + text->textRecords.SetNum( num ); + for( int t = 0; t < text->textRecords.Num(); t++ ) + { + idSWFTextRecord& textRecord = text->textRecords[t]; + f->ReadBig( textRecord.fontID ); + f->Read( &textRecord.color, 4 ); + f->ReadBig( textRecord.xOffset ); + f->ReadBig( textRecord.yOffset ); + f->ReadBig( textRecord.textHeight ); + f->ReadBig( textRecord.firstGlyph ); + f->ReadBig( textRecord.numGlyphs ); + } + f->ReadBig( num ); + text->glyphs.SetNum( num ); + for( int g = 0; g < text->glyphs.Num(); g++ ) + { + f->ReadBig( text->glyphs[g].index ); + f->ReadBig( text->glyphs[g].advance ); + } + break; + } + case SWF_DICT_EDITTEXT: + { + dictionary[i].edittext = new( TAG_SWF ) idSWFEditText; + idSWFEditText* edittext = dictionary[i].edittext; + f->ReadBig( edittext->bounds.tl ); + f->ReadBig( edittext->bounds.br ); + f->ReadBig( edittext->flags ); + f->ReadBig( edittext->fontID ); + f->ReadBig( edittext->fontHeight ); + f->Read( &edittext->color, 4 ); + f->ReadBig( edittext->maxLength ); + f->ReadBig( edittext->align ); + f->ReadBig( edittext->leftMargin ); + f->ReadBig( edittext->rightMargin ); + f->ReadBig( edittext->indent ); + f->ReadBig( edittext->leading ); + f->ReadString( edittext->variable ); + f->ReadString( edittext->initialText ); + break; + } + } + } + delete f; + + return true; +#else + return false; +#endif +} + /* =================== idSWF::WriteJSON diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 66f2688f..2c109fa9 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -37,7 +37,7 @@ idCVar swf_loadBinary( "swf_loadBinary", "1", CVAR_INTEGER, "used to set whether // RB begin idCVar swf_exportAtlas( "swf_exportAtlas", "0", CVAR_INTEGER, "" ); idCVar swf_exportSWF( "swf_exportSWF", "1", CVAR_INTEGER, "" ); -idCVar swf_exportXML( "swf_exportXML", "1", CVAR_INTEGER, "" ); +idCVar swf_exportJSON( "swf_exportJSON", "1", CVAR_INTEGER, "" ); // RB end int idSWF::mouseX = -1; @@ -140,12 +140,26 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) binaryFileName += filename; binaryFileName.SetFileExtension( ".bswf" ); + // RB: add JSON alternative + idStr jsonFileName = filename; + jsonFileName.SetFileExtension( ".json" ); + ID_TIME_T jsonSourceTime = fileSystem->GetTimestamp( jsonFileName ); + if( swf_loadBinary.GetBool() ) { ID_TIME_T sourceTime = fileSystem->GetTimestamp( filename ); + if( sourceTime == FILE_NOT_FOUND_TIMESTAMP ) + { + sourceTime = jsonSourceTime; + } + if( !LoadBinary( binaryFileName, sourceTime ) ) { - if( LoadSWF( filename ) ) + if( LoadJSON( jsonFileName ) ) + { + WriteBinary( binaryFileName ); + } + else if( LoadSWF( filename ) ) { WriteBinary( binaryFileName ); } @@ -153,11 +167,13 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) } else { - LoadSWF( filename ); + if( !LoadJSON( jsonFileName ) ) + { + LoadSWF( filename ); + } } - // RB begin - if( swf_exportXML.GetBool() ) + if( swf_exportJSON.GetBool() ) { idStr jsonFileName = "exported/"; jsonFileName += filename; From 09c1905fea607ee73a13259eca5d928320c10529 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sat, 25 Jul 2015 18:17:04 +0200 Subject: [PATCH 31/43] Continued RapidJSON Flash import --- neo/swf/SWF_Load.cpp | 352 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 299 insertions(+), 53 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 988db949..c2eed918 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -911,8 +911,6 @@ bool idSWF::LoadJSON( const char* bfilename ) return false; } - - int fileLength = f->Length(); const char* fileData = ( const char* )Mem_Alloc( fileLength, TAG_SWF ); size_t fileSize = f->Read( ( byte* ) fileData, fileLength ); @@ -931,24 +929,22 @@ bool idSWF::LoadJSON( const char* bfilename ) assert( d.IsObject() ); + if( d.HasMember( "version" ) ) { Value& s = d["version"]; int version = s.GetInt(); - idLib::Printf( "version = %i", version ); + //idLib::Printf( "version = %i", version ); } - //d.Begin - //Value& s = d["version"]; - //ID_TIME_T btimestamp = s.GetInt64(); //f->ReadBig( btimestamp ); -#if 0 - f->ReadBig( frameWidth ); - f->ReadBig( frameHeight ); - f->ReadBig( frameRate ); + // these values can be trash + frameWidth = d["frameWidth"].GetDouble(); + frameHeight = d["frameHeight"].GetDouble(); + frameRate = d["frameRate"].GetInt(); if( mouseX == -1 ) { @@ -960,6 +956,283 @@ bool idSWF::LoadJSON( const char* bfilename ) mouseY = ( frameHeight / 2 ); } + Value& a = d["dict"]; + assert( a.IsArray() ); + + dictionary.SetNum( a.Size() ); + + for( SizeType i = 0; i < a.Size(); i++ ) + { + Value& entry = a[i]; + idStr type = entry["type"].GetString(); + + if( type == "IMAGE" ) + { + dictionary[i].type = SWF_DICT_IMAGE; + + idStrStatic< MAX_OSPATH > imageName = entry["imagefile"].GetString(); + if( imageName[0] == '.' ) + { + // internal image in the atlas + dictionary[i].material = NULL; + } + else + { + dictionary[i].material = declManager->FindMaterial( imageName ); + } + + dictionary[i].imageSize[0] = entry["width"].GetDouble(); + dictionary[i].imageSize[1] = entry["height"].GetDouble(); + + idVec4& channelScale = dictionary[i].channelScale; + + channelScale.x = entry["channelScale"]["x"].GetDouble(); + channelScale.y = entry["channelScale"]["y"].GetDouble(); + channelScale.z = entry["channelScale"]["z"].GetDouble(); + channelScale.w = entry["channelScale"]["w"].GetDouble(); + } + else if( type == "SHAPE" || type == "MORPH" ) + { + if( type == "MORPH" ) + { + dictionary[i].type = SWF_DICT_MORPH; + } + else + { + dictionary[i].type = SWF_DICT_SHAPE; + } + + dictionary[i].shape = new( TAG_SWF ) idSWFShape; + + idSWFShape* shape = dictionary[i].shape; + + shape->startBounds.tl.x = entry["startBounds"][0].GetDouble(); + shape->startBounds.tl.y = entry["startBounds"][1].GetDouble(); + shape->startBounds.br.x = entry["startBounds"][2].GetDouble(); + shape->startBounds.br.y = entry["startBounds"][3].GetDouble(); + + shape->endBounds.tl.x = entry["endBounds"][0].GetDouble(); + shape->endBounds.tl.y = entry["endBounds"][1].GetDouble(); + shape->endBounds.br.x = entry["endBounds"][2].GetDouble(); + shape->endBounds.br.y = entry["endBounds"][3].GetDouble(); + + shape->fillDraws.SetNum( entry["fillDraws"].Size() ); + for( int d = 0; d < shape->fillDraws.Num(); d++ ) + { + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + Value& jsonDraw = entry["fillDraws"][d]; + + Value& style = jsonDraw["style"]; + idStr type = style["type"].GetString(); + + // 0 = solid, 1 = gradient, 4 = bitmap + if( type == "gradient" ) + { + fillDraw.style.type = 1; + } + else if( type == "bitmap" ) + { + fillDraw.style.type = 4; + } + else if( type == "solid" ) + { + fillDraw.style.type = 0; + } + else + { + // unknown + fillDraw.style.type = 0; + } + + // 0 = linear, 2 = radial, 3 = focal; 0 = repeat, 1 = clamp, 2 = near repeat, 3 = near clamp + idStr subType = style["subType"].GetString(); + + if( subType == "linear" || subType == "repeat" ) + { + fillDraw.style.subType = 0; + } + else if( subType == "radial" || subType == "near repeat" ) + { + fillDraw.style.subType = 2; + } + else if( subType == "focal" || subType == "near clamp" ) + { + fillDraw.style.subType = 3; + } + else + { + // unknown + fillDraw.style.subType = 0; + } + + if( fillDraw.style.type == 0 ) //style.HasMember["startColor"] )// ) + { + Value& startColor = style["startColor"]; + fillDraw.style.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + fillDraw.style.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + fillDraw.style.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + fillDraw.style.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + + if( style.HasMember( "endColor" ) ) + { + Value& endColor = style["endColor"]; + fillDraw.style.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + fillDraw.style.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + fillDraw.style.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + fillDraw.style.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + } + else + { + fillDraw.style.endColor = fillDraw.style.startColor; + } + } + + if( fillDraw.style.type > 0 ) + { + Value& startMatrix = style["startMatrix"]; + fillDraw.style.startMatrix.xx = startMatrix[0].GetDouble(); + fillDraw.style.startMatrix.yy = startMatrix[1].GetDouble(); + fillDraw.style.startMatrix.xy = startMatrix[2].GetDouble(); + fillDraw.style.startMatrix.yx = startMatrix[3].GetDouble(); + fillDraw.style.startMatrix.tx = startMatrix[4].GetDouble(); + fillDraw.style.startMatrix.ty = startMatrix[5].GetDouble(); + + if( style.HasMember( "endMatrix" ) ) + { + Value& endMatrix = style["endMatrix"]; + fillDraw.style.endMatrix.xx = endMatrix[0].GetDouble(); + fillDraw.style.endMatrix.yy = endMatrix[1].GetDouble(); + fillDraw.style.endMatrix.xy = endMatrix[2].GetDouble(); + fillDraw.style.endMatrix.yx = endMatrix[3].GetDouble(); + fillDraw.style.endMatrix.tx = endMatrix[4].GetDouble(); + fillDraw.style.endMatrix.ty = endMatrix[5].GetDouble(); + } + else + { + fillDraw.style.endMatrix = fillDraw.style.startMatrix; + } + } + + // gradient + if( fillDraw.style.type == 1 ) + { + Value& gradients = style["gradients"]; + fillDraw.style.gradient.numGradients = gradients.Size(); + + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + + Value& gradientRecord = gradients[g]; + + gr.startRatio = gradientRecord["startRatio"].GetUint() & 0xFF; + gr.endRatio = gradientRecord["endRatio"].GetUint() & 0xFF; + + Value& startColor = gradientRecord["startColor"]; + gr.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + gr.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + gr.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + gr.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + + Value& endColor = gradientRecord["endColor"]; + gr.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + gr.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + gr.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + gr.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + } + + if( style.HasMember( "focalPoint" ) ) + { + fillDraw.style.focalPoint = style["focalPoint"].GetDouble(); + } + else + { + fillDraw.style.focalPoint = 0; + } + } + + // bitmap + if( fillDraw.style.type == 4 ) + { + fillDraw.style.bitmapID = style["bitmapID"].GetUint(); + } + else + { + fillDraw.style.bitmapID = 65535; + } + + Value& startVerts = jsonDraw["startVerts"]; + + fillDraw.startVerts.SetNum( startVerts.Size() ); + for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + { + idVec2& vert = fillDraw.startVerts[v]; + + vert.x = startVerts[v]["v"][0].GetDouble(); + vert.y = startVerts[v]["v"][1].GetDouble(); + } + + if( jsonDraw.HasMember( "endVerts" ) ) + { + // this is a morph shape + + Value& endVerts = jsonDraw["endVerts"]; + + fillDraw.endVerts.SetNum( endVerts.Size() ); + for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + { + idVec2& vert = fillDraw.endVerts[v]; + + vert.x = endVerts[v]["v"][0].GetDouble(); + vert.y = endVerts[v]["v"][1].GetDouble(); + } + } + + Value& indices = jsonDraw["indices"]; + fillDraw.indices.SetNum( indices.Size() ); + +#if 1 + for( int v = 0; v < fillDraw.indices.Num(); v++ ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } +#else + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } +#endif + + } +#if 0 + f->ReadBig( num ); + shape->lineDraws.SetNum( num ); + for( int d = 0; d < shape->lineDraws.Num(); d++ ) + { + idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + f->ReadBig( lineDraw.style.startWidth ); + f->ReadBig( lineDraw.style.endWidth ); + f->Read( &lineDraw.style.startColor, 4 ); + f->Read( &lineDraw.style.endColor, 4 ); + f->ReadBig( num ); + lineDraw.startVerts.SetNum( num ); + f->ReadBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); + f->ReadBig( num ); + lineDraw.endVerts.SetNum( num ); + f->ReadBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); + f->ReadBig( num ); + lineDraw.indices.SetNum( num ); + f->ReadBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); + } +#endif + } + } + +#if 0 mainsprite->Read( f ); int num = 0; @@ -970,30 +1243,6 @@ bool idSWF::LoadJSON( const char* bfilename ) f->ReadBig( dictionary[i].type ); switch( dictionary[i].type ) { - case SWF_DICT_IMAGE: - { - idStr imageName; - f->ReadString( imageName ); - if( imageName[0] == '.' ) - { - // internal image in the atlas - dictionary[i].material = NULL; - } - else - { - dictionary[i].material = declManager->FindMaterial( imageName ); - } - for( int j = 0 ; j < 2 ; j++ ) - { - f->ReadBig( dictionary[i].imageSize[j] ); - f->ReadBig( dictionary[i].imageAtlasOffset[j] ); - } - for( int j = 0 ; j < 4 ; j++ ) - { - f->ReadBig( dictionary[i].channelScale[j] ); - } - break; - } case SWF_DICT_MORPH: case SWF_DICT_SHAPE: { @@ -1168,12 +1417,12 @@ void idSWF::WriteJSON( const char* filename ) { const idSWFDictionaryEntry& entry = dictionary[i]; - if( dictionary[i].type != SWF_DICT_NULL ) + //if( dictionary[i].type != SWF_DICT_NULL ) { file->WriteFloatString( "\t\t{\n" ); file->WriteFloatString( "\t\t\t\"type\": \"%s\",\n", idSWF::GetDictTypeName( dictionary[i].type ) ); - file->WriteFloatString( "\t\t\t\"characterID\": %i,\n", i ); + file->WriteFloatString( "\t\t\t\"characterID\": %i%s\n", i, dictionary[i].type != SWF_DICT_NULL ? "," : "" ); } switch( dictionary[i].type ) @@ -1205,20 +1454,18 @@ void idSWF::WriteJSON( const char* filename ) { idSWFShape* shape = dictionary[i].shape; - float x = shape->startBounds.tl.x; - float y = shape->startBounds.tl.y; - float width = fabs( shape->startBounds.br.x - shape->startBounds.tl.x ); - float height = fabs( shape->startBounds.br.y - shape->startBounds.tl.y ); + idVec2 tl = shape->startBounds.tl; + idVec2 br = shape->startBounds.br; //file->WriteFloatString( "\t\t\t\"imageSize\": { \"width\": %f, \"height\": %f },\n", dictionary[i].imageSize[0], dictionary[i].imageSize[1] ); - file->WriteFloatString( "\t\t\t\"startBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); + //file->WriteFloatString( "\t\t\t\"startBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); - x = shape->endBounds.tl.x; - y = shape->endBounds.tl.y; - width = fabs( shape->endBounds.br.x - shape->endBounds.tl.x ); - height = fabs( shape->endBounds.br.y - shape->endBounds.tl.y ); + file->WriteFloatString( "\t\t\t\"startBounds\": [ %f, %f, %f, %f ],\n", tl.x, tl.y, br.x, br.y ); - file->WriteFloatString( "\t\t\t\"endBounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); + tl = shape->endBounds.tl; + br = shape->endBounds.br; + + file->WriteFloatString( "\t\t\t\"endBounds\": [ %f, %f, %f, %f ],\n", tl.x, tl.y, br.x, br.y ); // export fill draws file->WriteFloatString( "\t\t\t\"fillDraws\":\n\t\t\t[\n" ); @@ -1313,6 +1560,7 @@ void idSWF::WriteJSON( const char* filename ) } } + // not used in BFG menus if( fillDraw.style.gradient.numGradients ) { file->WriteFloatString( "\t\t\t\t\t\"gradients\":\n\t\t\t\t\t[\n" ); @@ -1321,17 +1569,15 @@ void idSWF::WriteJSON( const char* filename ) { swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; - file->WriteFloatString( "\t\t\t\t\t\n", gr.startRatio, gr.endRatio ); + file->WriteFloatString( "\t\t\t\t\t{ \"startRatio\": %i, \"endRatio\": %i,\n", gr.startRatio, gr.endRatio ); idVec4 color = gr.startColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); - + file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); + idVec4 endColor = gr.endColor.ToVec4(); if( color != endColor ) { - file->WriteFloatString( "\t\t\t\t\t\t\n", - color.x, color.y, color.z, endColor.w ); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]", endColor.x, endColor.y, endColor.z, endColor.w ); } file->WriteFloatString( "\t\t\t\t\t\t}%s\n", ( g == ( fillDraw.style.gradient.numGradients - 1 ) ) ? "" : "," ); @@ -1587,7 +1833,7 @@ void idSWF::WriteJSON( const char* filename ) } } - if( dictionary[i].type != SWF_DICT_NULL ) + //if( dictionary[i].type != SWF_DICT_NULL ) { //file->WriteFloatString( "\t\t}%s\n", ( i == ( dictionary.Num() - 1 ) ) ? "" : "," ); file->WriteFloatString( "\t\t},\n" ); From 6073793ee58293731d1e4d186c06bdcb647ee322 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 26 Jul 2015 23:00:08 +0200 Subject: [PATCH 32/43] Exported Flash line draws to JSON --- neo/swf/SWF_Load.cpp | 900 ++++++++++++++++++++++--------------------- 1 file changed, 467 insertions(+), 433 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index c2eed918..b597ba4c 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -944,7 +944,7 @@ bool idSWF::LoadJSON( const char* bfilename ) // these values can be trash frameWidth = d["frameWidth"].GetDouble(); frameHeight = d["frameHeight"].GetDouble(); - frameRate = d["frameRate"].GetInt(); + frameRate = d["frameRate"].GetUint(); if( mouseX == -1 ) { @@ -1016,220 +1016,283 @@ bool idSWF::LoadJSON( const char* bfilename ) shape->endBounds.br.x = entry["endBounds"][2].GetDouble(); shape->endBounds.br.y = entry["endBounds"][3].GetDouble(); - shape->fillDraws.SetNum( entry["fillDraws"].Size() ); - for( int d = 0; d < shape->fillDraws.Num(); d++ ) + if( entry.HasMember( "fillDraws" ) ) { - idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - Value& jsonDraw = entry["fillDraws"][d]; - - Value& style = jsonDraw["style"]; - idStr type = style["type"].GetString(); - - // 0 = solid, 1 = gradient, 4 = bitmap - if( type == "gradient" ) + shape->fillDraws.SetNum( entry["fillDraws"].Size() ); + for( int d = 0; d < shape->fillDraws.Num(); d++ ) { - fillDraw.style.type = 1; + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + Value& jsonDraw = entry["fillDraws"][d]; + + Value& style = jsonDraw["style"]; + idStr type = style["type"].GetString(); + + // 0 = solid, 1 = gradient, 4 = bitmap + if( type == "gradient" ) + { + fillDraw.style.type = 1; + } + else if( type == "bitmap" ) + { + fillDraw.style.type = 4; + } + else if( type == "solid" ) + { + fillDraw.style.type = 0; + } + else + { + // unknown + fillDraw.style.type = 0; + } + + // 0 = linear, 2 = radial, 3 = focal; 0 = repeat, 1 = clamp, 2 = near repeat, 3 = near clamp + idStr subType = style["subType"].GetString(); + + if( subType == "linear" || subType == "repeat" ) + { + fillDraw.style.subType = 0; + } + else if( subType == "radial" || subType == "near repeat" ) + { + fillDraw.style.subType = 2; + } + else if( subType == "focal" || subType == "near clamp" ) + { + fillDraw.style.subType = 3; + } + else + { + // unknown + fillDraw.style.subType = 0; + } + + if( fillDraw.style.type == 0 ) //style.HasMember["startColor"] )// ) + { + Value& startColor = style["startColor"]; + fillDraw.style.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + fillDraw.style.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + fillDraw.style.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + fillDraw.style.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + + if( style.HasMember( "endColor" ) ) + { + Value& endColor = style["endColor"]; + fillDraw.style.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + fillDraw.style.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + fillDraw.style.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + fillDraw.style.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + } + else + { + fillDraw.style.endColor = fillDraw.style.startColor; + } + } + + if( fillDraw.style.type > 0 ) + { + Value& startMatrix = style["startMatrix"]; + fillDraw.style.startMatrix.xx = startMatrix[0].GetDouble(); + fillDraw.style.startMatrix.yy = startMatrix[1].GetDouble(); + fillDraw.style.startMatrix.xy = startMatrix[2].GetDouble(); + fillDraw.style.startMatrix.yx = startMatrix[3].GetDouble(); + fillDraw.style.startMatrix.tx = startMatrix[4].GetDouble(); + fillDraw.style.startMatrix.ty = startMatrix[5].GetDouble(); + + if( style.HasMember( "endMatrix" ) ) + { + Value& endMatrix = style["endMatrix"]; + fillDraw.style.endMatrix.xx = endMatrix[0].GetDouble(); + fillDraw.style.endMatrix.yy = endMatrix[1].GetDouble(); + fillDraw.style.endMatrix.xy = endMatrix[2].GetDouble(); + fillDraw.style.endMatrix.yx = endMatrix[3].GetDouble(); + fillDraw.style.endMatrix.tx = endMatrix[4].GetDouble(); + fillDraw.style.endMatrix.ty = endMatrix[5].GetDouble(); + } + else + { + fillDraw.style.endMatrix = fillDraw.style.startMatrix; + } + } + + // gradient + if( fillDraw.style.type == 1 ) + { + Value& gradients = style["gradients"]; + fillDraw.style.gradient.numGradients = gradients.Size(); + + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + + Value& gradientRecord = gradients[g]; + + gr.startRatio = gradientRecord["startRatio"].GetUint() & 0xFF; + gr.endRatio = gradientRecord["endRatio"].GetUint() & 0xFF; + + Value& startColor = gradientRecord["startColor"]; + gr.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + gr.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + gr.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + gr.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + + Value& endColor = gradientRecord["endColor"]; + gr.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + gr.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + gr.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + gr.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + } + + if( style.HasMember( "focalPoint" ) ) + { + fillDraw.style.focalPoint = style["focalPoint"].GetDouble(); + } + else + { + fillDraw.style.focalPoint = 0; + } + } + + // bitmap + if( fillDraw.style.type == 4 ) + { + fillDraw.style.bitmapID = style["bitmapID"].GetUint(); + } + else + { + fillDraw.style.bitmapID = 65535; + } + + Value& startVerts = jsonDraw["startVerts"]; + + fillDraw.startVerts.SetNum( startVerts.Size() ); + for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + { + idVec2& vert = fillDraw.startVerts[v]; + + vert.x = startVerts[v]["v"][0].GetDouble(); + vert.y = startVerts[v]["v"][1].GetDouble(); + } + + if( jsonDraw.HasMember( "endVerts" ) ) + { + // this is a morph shape + + Value& endVerts = jsonDraw["endVerts"]; + + fillDraw.endVerts.SetNum( endVerts.Size() ); + for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + { + idVec2& vert = fillDraw.endVerts[v]; + + vert.x = endVerts[v]["v"][0].GetDouble(); + vert.y = endVerts[v]["v"][1].GetDouble(); + } + } + + Value& indices = jsonDraw["indices"]; + fillDraw.indices.SetNum( indices.Size() ); + +#if 1 + for( int v = 0; v < fillDraw.indices.Num(); v++ ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } +#else + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } +#endif + } - else if( type == "bitmap" ) - { - fillDraw.style.type = 4; - } - else if( type == "solid" ) - { - fillDraw.style.type = 0; - } - else - { - // unknown - fillDraw.style.type = 0; - } - - // 0 = linear, 2 = radial, 3 = focal; 0 = repeat, 1 = clamp, 2 = near repeat, 3 = near clamp - idStr subType = style["subType"].GetString(); - - if( subType == "linear" || subType == "repeat" ) - { - fillDraw.style.subType = 0; - } - else if( subType == "radial" || subType == "near repeat" ) - { - fillDraw.style.subType = 2; - } - else if( subType == "focal" || subType == "near clamp" ) - { - fillDraw.style.subType = 3; - } - else - { - // unknown - fillDraw.style.subType = 0; - } - - if( fillDraw.style.type == 0 ) //style.HasMember["startColor"] )// ) + } + + + if( entry.HasMember( "lineDraws" ) ) + { + shape->lineDraws.SetNum( entry["lineDraws"].Size() ); + for( int d = 0; d < shape->lineDraws.Num(); d++ ) { + idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + Value& jsonDraw = entry["lineDraw"][d]; + + Value& style = jsonDraw["style"]; + lineDraw.style.startWidth = style["startWidth"].GetUint(); + lineDraw.style.endWidth = style["endWidth"].GetUint(); + Value& startColor = style["startColor"]; - fillDraw.style.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; - fillDraw.style.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; - fillDraw.style.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; - fillDraw.style.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + lineDraw.style.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + lineDraw.style.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + lineDraw.style.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + lineDraw.style.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; if( style.HasMember( "endColor" ) ) { Value& endColor = style["endColor"]; - fillDraw.style.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; - fillDraw.style.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; - fillDraw.style.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; - fillDraw.style.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + lineDraw.style.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; + lineDraw.style.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; + lineDraw.style.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; + lineDraw.style.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; } else { - fillDraw.style.endColor = fillDraw.style.startColor; + lineDraw.style.endColor = lineDraw.style.startColor; } - } - - if( fillDraw.style.type > 0 ) - { - Value& startMatrix = style["startMatrix"]; - fillDraw.style.startMatrix.xx = startMatrix[0].GetDouble(); - fillDraw.style.startMatrix.yy = startMatrix[1].GetDouble(); - fillDraw.style.startMatrix.xy = startMatrix[2].GetDouble(); - fillDraw.style.startMatrix.yx = startMatrix[3].GetDouble(); - fillDraw.style.startMatrix.tx = startMatrix[4].GetDouble(); - fillDraw.style.startMatrix.ty = startMatrix[5].GetDouble(); - if( style.HasMember( "endMatrix" ) ) - { - Value& endMatrix = style["endMatrix"]; - fillDraw.style.endMatrix.xx = endMatrix[0].GetDouble(); - fillDraw.style.endMatrix.yy = endMatrix[1].GetDouble(); - fillDraw.style.endMatrix.xy = endMatrix[2].GetDouble(); - fillDraw.style.endMatrix.yx = endMatrix[3].GetDouble(); - fillDraw.style.endMatrix.tx = endMatrix[4].GetDouble(); - fillDraw.style.endMatrix.ty = endMatrix[5].GetDouble(); - } - else - { - fillDraw.style.endMatrix = fillDraw.style.startMatrix; - } - } - - // gradient - if( fillDraw.style.type == 1 ) - { - Value& gradients = style["gradients"]; - fillDraw.style.gradient.numGradients = gradients.Size(); + Value& startVerts = jsonDraw["startVerts"]; - for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + lineDraw.startVerts.SetNum( startVerts.Size() ); + for( int v = 0; v < lineDraw.startVerts.Num(); v++ ) { - swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + idVec2& vert = lineDraw.startVerts[v]; - Value& gradientRecord = gradients[g]; - - gr.startRatio = gradientRecord["startRatio"].GetUint() & 0xFF; - gr.endRatio = gradientRecord["endRatio"].GetUint() & 0xFF; - - Value& startColor = gradientRecord["startColor"]; - gr.startColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; - gr.startColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; - gr.startColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; - gr.startColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; - - Value& endColor = gradientRecord["endColor"]; - gr.endColor.r = ( uint8 )( startColor[0].GetDouble() * 255 ) & 0xFF; - gr.endColor.g = ( uint8 )( startColor[1].GetDouble() * 255 ) & 0xFF; - gr.endColor.b = ( uint8 )( startColor[2].GetDouble() * 255 ) & 0xFF; - gr.endColor.a = ( uint8 )( startColor[3].GetDouble() * 255 ) & 0xFF; + vert.x = startVerts[v]["v"][0].GetDouble(); + vert.y = startVerts[v]["v"][1].GetDouble(); } - if( style.HasMember( "focalPoint" ) ) + if( jsonDraw.HasMember( "endVerts" ) ) { - fillDraw.style.focalPoint = style["focalPoint"].GetDouble(); - } - else - { - fillDraw.style.focalPoint = 0; - } - } - - // bitmap - if( fillDraw.style.type == 4 ) - { - fillDraw.style.bitmapID = style["bitmapID"].GetUint(); - } - else - { - fillDraw.style.bitmapID = 65535; - } - - Value& startVerts = jsonDraw["startVerts"]; - - fillDraw.startVerts.SetNum( startVerts.Size() ); - for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) - { - idVec2& vert = fillDraw.startVerts[v]; - - vert.x = startVerts[v]["v"][0].GetDouble(); - vert.y = startVerts[v]["v"][1].GetDouble(); - } - - if( jsonDraw.HasMember( "endVerts" ) ) - { - // this is a morph shape - - Value& endVerts = jsonDraw["endVerts"]; - - fillDraw.endVerts.SetNum( endVerts.Size() ); - for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) - { - idVec2& vert = fillDraw.endVerts[v]; + // this is a morph shape - vert.x = endVerts[v]["v"][0].GetDouble(); - vert.y = endVerts[v]["v"][1].GetDouble(); + Value& endVerts = jsonDraw["endVerts"]; + + lineDraw.endVerts.SetNum( endVerts.Size() ); + for( int v = 0; v < lineDraw.endVerts.Num(); v++ ) + { + idVec2& vert = lineDraw.endVerts[v]; + + vert.x = endVerts[v]["v"][0].GetDouble(); + vert.y = endVerts[v]["v"][1].GetDouble(); + } } - } - - Value& indices = jsonDraw["indices"]; - fillDraw.indices.SetNum( indices.Size() ); - + + Value& indices = jsonDraw["indices"]; + lineDraw.indices.SetNum( indices.Size() ); + #if 1 - for( int v = 0; v < fillDraw.indices.Num(); v++ ) - { - uint16& vert = fillDraw.indices[v]; - - vert = indices[v].GetUint(); - } + for( int v = 0; v < lineDraw.indices.Num(); v++ ) + { + uint16& vert = lineDraw.indices[v]; + + vert = indices[v].GetUint(); + } #else - for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) - { - uint16& vert = fillDraw.indices[v]; - - vert = indices[v].GetUint(); + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } +#endif } -#endif - } -#if 0 - f->ReadBig( num ); - shape->lineDraws.SetNum( num ); - for( int d = 0; d < shape->lineDraws.Num(); d++ ) - { - idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; - f->ReadBig( lineDraw.style.startWidth ); - f->ReadBig( lineDraw.style.endWidth ); - f->Read( &lineDraw.style.startColor, 4 ); - f->Read( &lineDraw.style.endColor, 4 ); - f->ReadBig( num ); - lineDraw.startVerts.SetNum( num ); - f->ReadBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); - f->ReadBig( num ); - lineDraw.endVerts.SetNum( num ); - f->ReadBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); - f->ReadBig( num ); - lineDraw.indices.SetNum( num ); - f->ReadBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); - } -#endif } + //if( another type) } #if 0 @@ -1243,67 +1306,6 @@ bool idSWF::LoadJSON( const char* bfilename ) f->ReadBig( dictionary[i].type ); switch( dictionary[i].type ) { - case SWF_DICT_MORPH: - case SWF_DICT_SHAPE: - { - dictionary[i].shape = new( TAG_SWF ) idSWFShape; - idSWFShape* shape = dictionary[i].shape; - f->ReadBig( shape->startBounds.tl ); - f->ReadBig( shape->startBounds.br ); - f->ReadBig( shape->endBounds.tl ); - f->ReadBig( shape->endBounds.br ); - f->ReadBig( num ); - shape->fillDraws.SetNum( num ); - for( int d = 0; d < shape->fillDraws.Num(); d++ ) - { - idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - f->ReadBig( fillDraw.style.type ); - f->ReadBig( fillDraw.style.subType ); - f->Read( &fillDraw.style.startColor, 4 ); - f->Read( &fillDraw.style.endColor, 4 ); - f->ReadBigArray( ( float* )&fillDraw.style.startMatrix, 6 ); - f->ReadBigArray( ( float* )&fillDraw.style.endMatrix, 6 ); - f->ReadBig( fillDraw.style.gradient.numGradients ); - for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) - { - f->ReadBig( fillDraw.style.gradient.gradientRecords[g].startRatio ); - f->ReadBig( fillDraw.style.gradient.gradientRecords[g].endRatio ); - f->Read( &fillDraw.style.gradient.gradientRecords[g].startColor, 4 ); - f->Read( &fillDraw.style.gradient.gradientRecords[g].endColor, 4 ); - } - f->ReadBig( fillDraw.style.focalPoint ); - f->ReadBig( fillDraw.style.bitmapID ); - f->ReadBig( num ); - fillDraw.startVerts.SetNum( num ); - f->ReadBigArray( fillDraw.startVerts.Ptr(), fillDraw.startVerts.Num() ); - f->ReadBig( num ); - fillDraw.endVerts.SetNum( num ); - f->ReadBigArray( fillDraw.endVerts.Ptr(), fillDraw.endVerts.Num() ); - f->ReadBig( num ); - fillDraw.indices.SetNum( num ); - f->ReadBigArray( fillDraw.indices.Ptr(), fillDraw.indices.Num() ); - } - f->ReadBig( num ); - shape->lineDraws.SetNum( num ); - for( int d = 0; d < shape->lineDraws.Num(); d++ ) - { - idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; - f->ReadBig( lineDraw.style.startWidth ); - f->ReadBig( lineDraw.style.endWidth ); - f->Read( &lineDraw.style.startColor, 4 ); - f->Read( &lineDraw.style.endColor, 4 ); - f->ReadBig( num ); - lineDraw.startVerts.SetNum( num ); - f->ReadBigArray( lineDraw.startVerts.Ptr(), lineDraw.startVerts.Num() ); - f->ReadBig( num ); - lineDraw.endVerts.SetNum( num ); - f->ReadBigArray( lineDraw.endVerts.Ptr(), lineDraw.endVerts.Num() ); - f->ReadBig( num ); - lineDraw.indices.SetNum( num ); - f->ReadBigArray( lineDraw.indices.Ptr(), lineDraw.indices.Num() ); - } - break; - } case SWF_DICT_SPRITE: { dictionary[i].sprite = new( TAG_SWF ) idSWFSprite( this ); @@ -1431,7 +1433,7 @@ void idSWF::WriteJSON( const char* filename ) { if( dictionary[i].material ) { - file->WriteFloatString( "\t\t\t\"material\": \"%s\"", dictionary[i].material->GetName() ); + file->WriteFloatString( "\t\t\t\"material\": \"%s\",\n", dictionary[i].material->GetName() ); } else { @@ -1448,7 +1450,6 @@ void idSWF::WriteJSON( const char* filename ) break; } -#if 1 case SWF_DICT_MORPH: case SWF_DICT_SHAPE: { @@ -1465,229 +1466,262 @@ void idSWF::WriteJSON( const char* filename ) tl = shape->endBounds.tl; br = shape->endBounds.br; - file->WriteFloatString( "\t\t\t\"endBounds\": [ %f, %f, %f, %f ],\n", tl.x, tl.y, br.x, br.y ); + file->WriteFloatString( "\t\t\t\"endBounds\": [ %f, %f, %f, %f ]", tl.x, tl.y, br.x, br.y ); // export fill draws - file->WriteFloatString( "\t\t\t\"fillDraws\":\n\t\t\t[\n" ); - - if( shape->fillDraws.Num() > 1 ) + if( shape->fillDraws.Num() > 0 ) { - idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "%s.Shape%i has %i fill draws\n", filename, i, shape->fillDraws.Num() ); - } - - for( int d = 0; d < shape->fillDraws.Num(); d++ ) - { - idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; + file->WriteFloatString( ",\n\t\t\t\"fillDraws\":\n\t\t\t[\n" ); - if( exportBitmapShapesOnly && fillDraw.style.type != 4 ) + if( shape->fillDraws.Num() > 1 ) { - continue; + idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "%s.Shape%i has %i fill draws\n", filename, i, shape->fillDraws.Num() ); } - file->WriteFloatString( "\t\t\t\t{\n" ); - - file->WriteFloatString( "\t\t\t\t\t\"style\":\n\t\t\t\t\t{\n\t\t\t\t\t\t\"type\": " ); - - // 0 = solid, 1 = gradient, 4 = bitmap - if( fillDraw.style.type == 0 ) + for( int d = 0; d < shape->fillDraws.Num(); d++ ) { - file->WriteFloatString( "\"solid\"" ); - } - else if( fillDraw.style.type == 1 ) - { - file->WriteFloatString( "\"gradient\"" ); - } - else if( fillDraw.style.type == 4 ) - { - file->WriteFloatString( "\"bitmap\"" ); - } - else - { - file->WriteFloatString( "\"%i\"", fillDraw.style.type ); - } - - // 0 = linear, 2 = radial, 3 = focal; 0 = repeat, 1 = clamp, 2 = near repeat, 3 = near clamp - file->WriteFloatString( ",\n\t\t\t\t\t\t\"subType\": " ); - if( fillDraw.style.subType == 0 ) - { - file->WriteFloatString( "\"linear\"" ); - } - else if( fillDraw.style.subType == 1 ) - { - file->WriteFloatString( "\"radial\"" ); - } - else if( fillDraw.style.subType == 2 ) - { - file->WriteFloatString( "\"focal\"" ); - } - else if( fillDraw.style.subType == 3 ) - { - file->WriteFloatString( "\"near clamp\"" ); - } - else - { - file->WriteFloatString( "\"%i\"", fillDraw.style.subType ); - } - - if( fillDraw.style.type == 1 && fillDraw.style.subType == 3 ) - { - file->WriteFloatString( ",\n\t\t\t\t\t\t\"focalPoint\": %f", fillDraw.style.focalPoint ); - } - - if( fillDraw.style.type == 4 ) - { - file->WriteFloatString( ",\n\t\t\t\t\t\t\"bitmapID\": %i", fillDraw.style.bitmapID ); - } - - if( fillDraw.style.type == 0 ) - { - idVec4 color = fillDraw.style.startColor.ToVec4(); - file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); + idSWFShapeDrawFill& fillDraw = shape->fillDraws[d]; - color = fillDraw.style.endColor.ToVec4(); - file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]\n", color.x, color.y, color.z, color.w ); - } - - if( fillDraw.style.type > 0 ) - { - swfMatrix_t m = fillDraw.style.startMatrix; - file->WriteFloatString( ",\n\t\t\t\t\t\t\"startMatrix\": [ %f, %f, %f, %f, %f, %f ]", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); - - if( fillDraw.style.startMatrix != fillDraw.style.endMatrix ) + if( exportBitmapShapesOnly && fillDraw.style.type != 4 ) { - m = fillDraw.style.endMatrix; - file->WriteFloatString( ",\n\t\t\t\t\t\t\"endMatrix\": [ %f, %f, %f, %f, %f, %f ],\n", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + continue; } - } - - // not used in BFG menus - if( fillDraw.style.gradient.numGradients ) - { - file->WriteFloatString( "\t\t\t\t\t\"gradients\":\n\t\t\t\t\t[\n" ); - for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + file->WriteFloatString( "\t\t\t\t{\n" ); + + file->WriteFloatString( "\t\t\t\t\t\"style\":\n\t\t\t\t\t{\n\t\t\t\t\t\t\"type\": " ); + + // 0 = solid, 1 = gradient, 4 = bitmap + if( fillDraw.style.type == 0 ) { - swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; - - file->WriteFloatString( "\t\t\t\t\t{ \"startRatio\": %i, \"endRatio\": %i,\n", gr.startRatio, gr.endRatio ); - - idVec4 color = gr.startColor.ToVec4(); + file->WriteFloatString( "\"solid\"" ); + } + else if( fillDraw.style.type == 1 ) + { + file->WriteFloatString( "\"gradient\"" ); + } + else if( fillDraw.style.type == 4 ) + { + file->WriteFloatString( "\"bitmap\"" ); + } + else + { + file->WriteFloatString( "\"%i\"", fillDraw.style.type ); + } + + // 0 = linear, 2 = radial, 3 = focal; 0 = repeat, 1 = clamp, 2 = near repeat, 3 = near clamp + file->WriteFloatString( ",\n\t\t\t\t\t\t\"subType\": " ); + if( fillDraw.style.subType == 0 ) + { + file->WriteFloatString( "\"linear\"" ); + } + else if( fillDraw.style.subType == 1 ) + { + file->WriteFloatString( "\"radial\"" ); + } + else if( fillDraw.style.subType == 2 ) + { + file->WriteFloatString( "\"focal\"" ); + } + else if( fillDraw.style.subType == 3 ) + { + file->WriteFloatString( "\"near clamp\"" ); + } + else + { + file->WriteFloatString( "\"%i\"", fillDraw.style.subType ); + } + + if( fillDraw.style.type == 1 && fillDraw.style.subType == 3 ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\t\"focalPoint\": %f", fillDraw.style.focalPoint ); + } + + if( fillDraw.style.type == 4 ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\t\"bitmapID\": %i", fillDraw.style.bitmapID ); + } + + if( fillDraw.style.type == 0 ) + { + idVec4 color = fillDraw.style.startColor.ToVec4(); file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); - idVec4 endColor = gr.endColor.ToVec4(); - if( color != endColor ) + color = fillDraw.style.endColor.ToVec4(); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]\n", color.x, color.y, color.z, color.w ); + } + + if( fillDraw.style.type > 0 ) + { + swfMatrix_t m = fillDraw.style.startMatrix; + file->WriteFloatString( ",\n\t\t\t\t\t\t\"startMatrix\": [ %f, %f, %f, %f, %f, %f ]", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + + if( fillDraw.style.startMatrix != fillDraw.style.endMatrix ) { - file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]", endColor.x, endColor.y, endColor.z, endColor.w ); + m = fillDraw.style.endMatrix; + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endMatrix\": [ %f, %f, %f, %f, %f, %f ],\n", m.xx, m.yy, m.xy, m.yx, m.tx, m.ty ); + } + } + + // not used in BFG menus + if( fillDraw.style.gradient.numGradients ) + { + file->WriteFloatString( "\t\t\t\t\t\"gradients\":\n\t\t\t\t\t[\n" ); + + for( int g = 0; g < fillDraw.style.gradient.numGradients; g++ ) + { + swfGradientRecord_t gr = fillDraw.style.gradient.gradientRecords[g]; + + file->WriteFloatString( "\t\t\t\t\t{ \"startRatio\": %i, \"endRatio\": %i,\n", gr.startRatio, gr.endRatio ); + + idVec4 color = gr.startColor.ToVec4(); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); + + idVec4 endColor = gr.endColor.ToVec4(); + if( color != endColor ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]", endColor.x, endColor.y, endColor.z, endColor.w ); + } + + file->WriteFloatString( "\t\t\t\t\t\t}%s\n", ( g == ( fillDraw.style.gradient.numGradients - 1 ) ) ? "" : "," ); } - file->WriteFloatString( "\t\t\t\t\t\t}%s\n", ( g == ( fillDraw.style.gradient.numGradients - 1 ) ) ? "" : "," ); + file->WriteFloatString( "\t\t\t\t\t\t],\n" ); } - file->WriteFloatString( "\t\t\t\t\t\t],\n" ); - } - - file->WriteFloatString( "\n\t\t\t\t\t}" ); - - if( fillDraw.startVerts.Num() ) - { - file->WriteFloatString( ",\n\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); - for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + file->WriteFloatString( "\n\t\t\t\t\t}" ); + + if( fillDraw.startVerts.Num() ) { - const idVec2& vert = fillDraw.startVerts[v]; - - file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.startVerts.Num() - 1 ) ) ? "" : "," ); + file->WriteFloatString( ",\n\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); + for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + { + const idVec2& vert = fillDraw.startVerts[v]; + + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.startVerts.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t\t]" ); } - file->WriteFloatString( "\t\t\t\t\t]" ); - } - - if( fillDraw.endVerts.Num() ) - { - file->WriteFloatString( ",\n\t\t\t\t\t\"endVerts\":\n\t\t\t\t\t[\n" ); - for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + + if( fillDraw.endVerts.Num() ) { - const idVec2& vert = fillDraw.endVerts[v]; - - file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.endVerts.Num() - 1 ) ) ? "" : "," ); + file->WriteFloatString( ",\n\t\t\t\t\t\"endVerts\":\n\t\t\t\t\t[\n" ); + for( int v = 0; v < fillDraw.endVerts.Num(); v++ ) + { + const idVec2& vert = fillDraw.endVerts[v]; + + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( fillDraw.endVerts.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t\t]" ); } - file->WriteFloatString( "\t\t\t\t\t]" ); - } - - if( fillDraw.indices.Num() ) - { - file->WriteFloatString( ",\n\t\t\t\t\t\"indices\": [ " ); + + if( fillDraw.indices.Num() ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\"indices\": [ " ); #if 1 - for( int v = 0; v < fillDraw.indices.Num(); v++ ) - { - const uint16& vert = fillDraw.indices[v]; - - file->WriteFloatString( "%i%s", vert, ( v == fillDraw.indices.Num() - 1 ) ? "" : ", " ); - } + for( int v = 0; v < fillDraw.indices.Num(); v++ ) + { + const uint16& vert = fillDraw.indices[v]; + + file->WriteFloatString( "%i%s", vert, ( v == fillDraw.indices.Num() - 1 ) ? "" : ", " ); + } #else - for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) - { - const uint16& vert = fillDraw.indices[v]; - - file->WriteFloatString( "%i%s", vert, ( v == 0 ) ? "" : ", " ); - } + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + const uint16& vert = fillDraw.indices[v]; + + file->WriteFloatString( "%i%s", vert, ( v == 0 ) ? "" : ", " ); + } #endif - file->WriteFloatString( "]\n" ); + file->WriteFloatString( "]\n" ); + } + + file->WriteFloatString( "\t\t\t\t}%s\n", ( d == ( shape->fillDraws.Num() - 1 ) ) ? "" : "," ); } - file->WriteFloatString( "\t\t\t\t}%s\n", ( d == ( shape->fillDraws.Num() - 1 ) ) ? "" : "," ); + file->WriteFloatString( "\t\t\t]" ); } - file->WriteFloatString( "\t\t\t]\n" ); - // export line draws -#if 0 - for( int d = 0; d < shape->lineDraws.Num(); d++ ) + if( shape->lineDraws.Num() > 0 ) { - const idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; + file->WriteFloatString( ",\n\t\t\t\"lineDraws\":\n\t\t\t[\n" ); - file->WriteFloatString( "\t\t\t\n" ); - - file->WriteFloatString( "\t\t\t\t\n", lineDraw.style.startWidth, lineDraw.style.endWidth ); - - idVec4 color = lineDraw.style.startColor.ToVec4(); - file->WriteFloatString( "\t\t\t\t\t\n", - color.x, color.y, color.z, color.w ); - - idVec4 endColor = lineDraw.style.endColor.ToVec4(); - if( color != endColor ) + for( int d = 0; d < shape->lineDraws.Num(); d++ ) { - file->WriteFloatString( "\t\t\t\t\t\n", - endColor.x, endColor.y, endColor.z, endColor.w ); - } - - file->WriteFloatString( "\t\t\t\t\n" ); - - for( int v = 0; v < lineDraw.startVerts.Num(); v++ ) - { - const idVec2& vert = lineDraw.startVerts[v]; + const idSWFShapeDrawLine& lineDraw = shape->lineDraws[d]; - file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); - } - - for( int v = 0; v < lineDraw.endVerts.Num(); v++ ) - { - const idVec2& vert = lineDraw.endVerts[v]; + file->WriteFloatString( "\t\t\t\t{\n" ); - file->WriteFloatString( "\t\t\t\t\n", vert.x, vert.y ); - } - - file->WriteFloatString( "\t\t\t\t", lineDraw.indices.Num() ); - for( int v = 0; v < lineDraw.indices.Num(); v++ ) - { - const uint16& vert = lineDraw.indices[v]; + file->WriteFloatString( "\t\t\t\t\t\"style\":\n\t\t\t\t\t{\n\t\t\t\t\t\t\"startWidth\": %i", lineDraw.style.startWidth ); - file->WriteFloatString( "%i ", vert ); - } - file->WriteFloatString( "\n" ); - } + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endWidth\": %i", lineDraw.style.endWidth ); + + idVec4 color = lineDraw.style.startColor.ToVec4(); + file->WriteFloatString( ",\n\t\t\t\t\t\t\"startColor\": [ %f, %f, %f, %f ]", color.x, color.y, color.z, color.w ); + + idVec4 endColor = lineDraw.style.endColor.ToVec4(); + if( color != endColor ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\t\"endColor\": [ %f, %f, %f, %f ]\n", endColor.x, endColor.y, endColor.z, endColor.w ); + } + + file->WriteFloatString( "\n\t\t\t\t\t}" ); + + if( lineDraw.startVerts.Num() ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\"startVerts\":\n\t\t\t\t\t[\n" ); + for( int v = 0; v < lineDraw.startVerts.Num(); v++ ) + { + const idVec2& vert = lineDraw.startVerts[v]; + + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( lineDraw.startVerts.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t\t]" ); + } + + if( lineDraw.endVerts.Num() ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\"endVerts\":\n\t\t\t\t\t[\n" ); + for( int v = 0; v < lineDraw.endVerts.Num(); v++ ) + { + const idVec2& vert = lineDraw.endVerts[v]; + + file->WriteFloatString( "\t\t\t\t\t\t{ \"v\": [ %f, %f ] }%s\n", vert.x, vert.y, ( v == ( lineDraw.endVerts.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t\t]" ); + } + + if( lineDraw.indices.Num() ) + { + file->WriteFloatString( ",\n\t\t\t\t\t\"indices\": [ " ); +#if 1 + for( int v = 0; v < lineDraw.indices.Num(); v++ ) + { + const uint16& vert = lineDraw.indices[v]; + + file->WriteFloatString( "%i%s", vert, ( v == lineDraw.indices.Num() - 1 ) ? "" : ", " ); + } +#else + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + const uint16& vert = fillDraw.indices[v]; + + file->WriteFloatString( "%i%s", vert, ( v == 0 ) ? "" : ", " ); + } #endif + file->WriteFloatString( "]\n" ); + } + + file->WriteFloatString( "\t\t\t\t}%s\n", ( d == ( shape->lineDraws.Num() - 1 ) ) ? "" : "," ); + } + + file->WriteFloatString( "\t\t\t]" ); + } + + file->WriteFloatString( "\n" ); break; } -#endif case SWF_DICT_SPRITE: { From 0792ac0f59174489dfe2d52d38454e6ad7affeb2 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Tue, 28 Jul 2015 00:29:01 +0200 Subject: [PATCH 33/43] JSON Flash sprite import --- neo/idlib/precompiled.h | 3 ++ neo/swf/SWF_Load.cpp | 9 +++- neo/swf/SWF_Sprites.cpp | 96 ++++++++++++++++++++++++++++++++++++++++- neo/swf/SWF_Sprites.h | 2 + 4 files changed, 107 insertions(+), 3 deletions(-) diff --git a/neo/idlib/precompiled.h b/neo/idlib/precompiled.h index 063623e9..4ff0b0f6 100644 --- a/neo/idlib/precompiled.h +++ b/neo/idlib/precompiled.h @@ -104,6 +104,9 @@ const int MAX_EXPRESSION_REGISTERS = 4096; #include "../ui/ListGUI.h" #include "../ui/UserInterface.h" +// RB: required for SWF extensions +#include "../libs/rapidjson/include/rapidjson/document.h" + #include "../swf/SWF.h" // collision detection system diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index b597ba4c..70a81880 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -30,7 +30,7 @@ If you have questions concerning this license or the applicable additional terms #include "precompiled.h" #include "../renderer/Font.h" #include "../renderer/Image.h" -#include "../../libs/rapidjson/include/rapidjson/document.h" +//#include "../../libs/rapidjson/include/rapidjson/document.h" using namespace rapidjson; @@ -1292,7 +1292,12 @@ bool idSWF::LoadJSON( const char* bfilename ) } } } - //if( another type) + else if( type == "SPRITE" ) + { + dictionary[i].type = SWF_DICT_SPRITE; + dictionary[i].sprite = new( TAG_SWF ) idSWFSprite( this ); + dictionary[i].sprite->ReadJSON( entry ); + } } #if 0 diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 1d5e8856..90cd483c 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -29,6 +29,8 @@ If you have questions concerning this license or the applicable additional terms #pragma hdrstop #include "precompiled.h" +using namespace rapidjson; + /* ======================== idSWFSprite::idSWFSprite @@ -291,6 +293,98 @@ void idSWFSprite::Write( idFile* f ) // RB begin +void idSWFSprite::ReadJSON( rapidjson::Value& entry ) +{ + frameCount = entry["frameCount"].GetUint(); + + Value& fo = entry["frameOffsets"]; + frameOffsets.SetNum( fo.Size() ); + + for( int i = 0; i < frameOffsets.Num(); i++ ) + { + frameOffsets[i] = fo[i].GetUint(); + } + + if( entry.HasMember( "frameLabels" ) ) + { + Value& fl = entry["frameLabels"]; + frameLabels.SetNum( fl.Size() ); + for( int i = 0; i < frameLabels.Num(); i++ ) + { + frameLabels[i].frameNum = fl[i]["frameNum"].GetUint(); + frameLabels[i].frameLabel = fl[i]["frameLabel"].GetString(); + } + } + + Value& c = entry["commands"]; + commands.SetNum( c.Size() ); + for( int i = 0; i < commands.Num(); i++ ) + { + Value& command = c[i]; + Value& type = command["type"]; + +#if 1 + if( type == "Tag_PlaceObject2" ) + { + commands[i].tag = Tag_PlaceObject2; + + idFile_SWF file( new idFile_Memory() ); + + uint8 flags1 = command["flags"].GetUint(); + file.WriteU8( flags1 ); + + uint16 depth = command["depth"].GetUint(); + file.WriteU16( depth ); + + if( ( flags1 & PlaceFlagHasCharacter ) != 0 ) + { + uint16 characterID = command["characterID"].GetUint(); + file.WriteU16( characterID ); + } + + if( ( flags1 & PlaceFlagHasMatrix ) != 0 ) + { + swfMatrix_t m; + Value& startMatrix = command["startMatrix"]; + m.xx = startMatrix[0].GetDouble(); + m.yy = startMatrix[1].GetDouble(); + m.xy = startMatrix[2].GetDouble(); + m.yx = startMatrix[3].GetDouble(); + m.tx = startMatrix[4].GetDouble(); + m.ty = startMatrix[5].GetDouble(); + file.WriteMatrix( m ); + } + + if( ( flags1 & PlaceFlagHasColorTransform ) != 0 ) + { + swfColorXform_t cxf; + + Value& mulColor = command["mulColor"]; + cxf.mul.x = mulColor[0].GetDouble(); + cxf.mul.y = mulColor[1].GetDouble(); + cxf.mul.z = mulColor[2].GetDouble(); + cxf.mul.w = mulColor[3].GetDouble(); + + if( command.HasMember( "addColor" ) ) + { + Value& addColor = command["addColor"]; + cxf.add.x = addColor[0].GetDouble(); + cxf.add.y = addColor[1].GetDouble(); + cxf.add.z = addColor[2].GetDouble(); + cxf.add.w = addColor[3].GetDouble(); + } + + // TODO + //file.WriteColorXFormRGBA( cxf ); + } + + uint32 streamLength = file->Length(); + commands[i].stream.Load( ( byte* ) static_cast( ( idFile* )file )->GetDataPtr(), streamLength, true ); + } +#endif + } +} + void idSWFSprite::WriteJSON( idFile* f, int characterID ) { f->WriteFloatString( "\t\t\t\"frameCount\": %i,\n", frameCount ); diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index 4c22105a..328db0d6 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -48,6 +48,8 @@ public: void Write( idFile* f ); // RB begin + void ReadJSON( rapidjson::Value& entry ); + void WriteJSON( idFile* f, int characterID ); void WriteJSON_PlaceObject2( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); void WriteJSON_PlaceObject3( idFile* f, idSWFBitStream& bitstream, int characterID, int commandID, const char* indentPrefix = "" ); From d769620288665ac0f41569ac61775ca230bb9477 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 13 Sep 2015 17:21:58 +0200 Subject: [PATCH 34/43] JSON Flash sprite import xform color --- neo/swf/SWF_File.cpp | 65 ++++++++++++++++++++++++++++++++++++++++- neo/swf/SWF_File.h | 3 +- neo/swf/SWF_Sprites.cpp | 35 +++++++++++++++++++--- neo/swf/SWF_Types.h | 5 ++++ 4 files changed, 102 insertions(+), 6 deletions(-) diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index 090a5a23..846cbc9e 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -2,7 +2,7 @@ =========================================================================== Doom 3 BFG Edition GPL Source Code -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -247,6 +247,69 @@ void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) ByteAlign(); } +void idFile_SWF::WriteColorXFormRGBA( const swfColorXform_t& xcf ) +{ + bool hasAddTerms = ( xcf.add.x != 0.0f || xcf.add.y != 0.0f || xcf.add.z != 0.0f || xcf.add.w != 0.0f ); + WriteUBits( hasAddTerms ? 1 : 0, 1 ); + + bool hasMulTerms = ( xcf.mul.x != 0.0f || xcf.mul.y != 0.0f || xcf.mul.z != 0.0f || xcf.mul.w != 0.0f ); + WriteUBits( hasMulTerms ? 1 : 0, 1 ); + + int nBits = 0; + + int mulRed = 0; + int mulGreen = 0; + int mulBlue = 0; + int mulAlpha = 0; + if( hasMulTerms ) + { + mulRed = FLOAT2SWFFIXED8( xcf.mul.x ); + mulGreen = FLOAT2SWFFIXED8( xcf.mul.y ); + mulBlue = FLOAT2SWFFIXED8( xcf.mul.z ); + mulAlpha = FLOAT2SWFFIXED8( xcf.mul.w ); + + nBits = EnlargeBitCountS( mulRed, nBits ); + nBits = EnlargeBitCountS( mulGreen, nBits ); + nBits = EnlargeBitCountS( mulBlue, nBits ); + nBits = EnlargeBitCountS( mulAlpha, nBits ); + } + + int red = 0; + int green = 0; + int blue = 0; + int alpha = 0; + if( hasAddTerms ) + { + red = FLOAT2SWFFIXED8( xcf.add.x ); + green = FLOAT2SWFFIXED8( xcf.add.y ); + blue = FLOAT2SWFFIXED8( xcf.add.z ); + alpha = FLOAT2SWFFIXED8( xcf.add.w ); + + nBits = EnlargeBitCountS( red, nBits ); + nBits = EnlargeBitCountS( green, nBits ); + nBits = EnlargeBitCountS( blue, nBits ); + nBits = EnlargeBitCountS( alpha, nBits ); + } + + WriteUBits( 4, nBits ); + + if( hasMulTerms ) + { + WriteSBits( mulRed, nBits ); + WriteSBits( mulGreen, nBits ); + WriteSBits( mulBlue, nBits ); + WriteSBits( mulAlpha, nBits ); + } + + if( hasAddTerms ) + { + WriteSBits( red, nBits ); + WriteSBits( green, nBits ); + WriteSBits( blue, nBits ); + WriteSBits( alpha, nBits ); + } +} + void idFile_SWF::WriteColorRGB( const swfColorRGB_t& color ) { ByteAlign(); diff --git a/neo/swf/SWF_File.h b/neo/swf/SWF_File.h index 72685fcd..353f65aa 100644 --- a/neo/swf/SWF_File.h +++ b/neo/swf/SWF_File.h @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -85,6 +85,7 @@ public: void WriteMatrix( const swfMatrix_t& matrix ); void WriteColorRGB( const swfColorRGB_t& color ); void WriteColorRGBA( const swfColorRGBA_t& color ); + void WriteColorXFormRGBA( const swfColorXform_t& xcf ); static int32 GetTagHeaderSize( swfTag_t tag, int32 tagLength ); void WriteTagHeader( swfTag_t tag, int32 tagLength ); diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 90cd483c..97ecb7ee 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -375,7 +375,37 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) } // TODO - //file.WriteColorXFormRGBA( cxf ); + file.WriteColorXFormRGBA( cxf ); + } + + if( ( flags1 & PlaceFlagHasRatio ) != 0 ) + { + float ratio = command["ratio"].GetDouble(); + file.WriteU16( ( uint16 )( ratio * 65535 ) ); + } + + if( ( flags1 & PlaceFlagHasName ) != 0 ) + { + Value& name = command["name"]; + idStr string = name.GetString(); + string.Append( '\0' ); + int len = string.Length(); + + file->Write( string.c_str(), len ); + + // TODO + //file.writeStrin + } + + if( ( flags1 & PlaceFlagHasClipDepth ) != 0 ) + { + uint16 clipDepth = command["clipDepth"].GetUint(); + file.WriteU16( clipDepth ); + } + + if( ( flags1 & PlaceFlagHasClipActions ) != 0 ) + { + // FIXME: clip actions } uint32 streamLength = file->Length(); @@ -389,8 +419,6 @@ void idSWFSprite::WriteJSON( idFile* f, int characterID ) { f->WriteFloatString( "\t\t\t\"frameCount\": %i,\n", frameCount ); - //f->WriteFloatString( "\t\t\t\"frameOffsets\": %i,\n", frameOffsets.Num() ); - if( frameOffsets.Num() ) { f->WriteFloatString( "\t\t\t\"frameOffsets\": [ " ); @@ -401,7 +429,6 @@ void idSWFSprite::WriteJSON( idFile* f, int characterID ) f->WriteFloatString( " ],\n" ); } - //f->WriteFloatString( "\t\t", frameLabels.Num() ); if( frameLabels.Num() ) { f->WriteFloatString( "\t\t\t\"frameLabels\":\n\t\t\t[\n" ); diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 1c229c0a..56053366 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -52,6 +52,11 @@ ID_INLINE int FLOAT2SWFFIXED16( float value ) { return ( int )( value * 65536 ); } + +ID_INLINE int FLOAT2SWFFIXED8( float value ) +{ + return ( int )( value * 255 ); +} // RB end struct swfHeader_t From c5545bf8f2562014b82c639e2bcdcbbc5dbe8906 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 11 Oct 2015 20:10:57 +0200 Subject: [PATCH 35/43] JSON -> Flash bugfixes --- neo/swf/SWF_File.cpp | 106 ++++++++++++++++++++++++++++++++----------- neo/swf/SWF_File.h | 9 ++-- 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index 846cbc9e..616fea0d 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -44,33 +44,27 @@ idFile_SWF::~idFile_SWF() -int idFile_SWF::BitCountS( const int value ) +int idFile_SWF::BitCountS( const int64 value, bool isSigned ) { - int count = 0; + int number = idMath::Abs( value ); -#if 1 - count = idMath::BitCount( value ); -#else - int v = value; - while( v > 0 ) + int64 x = 1; + int i; + + for( i = 1; i <= 64; i++ ) { - if( ( v & 1 ) == 1 ) - { - // lower bit is set - count++; - } - - // shift bits, remove lower bit - v >>= 1; + x <<= 1; + if( x > number ) + break; } -#endif - return count; + return ( int )( i + ( ( isSigned ) ? 1 : 0 ) ); } int idFile_SWF::BitCountU( const int value ) { - int nBits = idMath::BitCount( value ); + //int nBits = idMath::BitCount( value ); + int nBits = BitCountS( value, false ); return nBits; } @@ -79,14 +73,19 @@ int idFile_SWF::BitCountFloat( const float value ) { int value2 = ( int ) value; - int nBits = BitCountS( value2 ); + int nBits = BitCountS( value2, value < 0.0F ); return nBits; } int idFile_SWF::EnlargeBitCountS( const int value, int numBits ) { - int n = BitCountS( value ); + if( value == 0 ) + { + return numBits; + } + + int n = BitCountS( value, true ); if( n > numBits ) { numBits = n; @@ -97,6 +96,11 @@ int idFile_SWF::EnlargeBitCountS( const int value, int numBits ) int idFile_SWF::EnlargeBitCountU( const int value, int numBits ) { + if( value == 0 ) + { + return numBits; + } + int n = BitCountU( value ); if( n > numBits ) { @@ -123,26 +127,74 @@ void idFile_SWF::WriteByte( byte bits ) void idFile_SWF::WriteUBits( int value, int numBits ) { +#if 1 for( int bit = 0; bit < numBits; bit++ ) { int nb = ( int )( ( value >> ( numBits - 1 - bit ) ) & 1 ); NBits += nb * ( 1 << ( 7 - bitPos ) ); + //NBits += nb * ( 1 << ( bitPos ) ); + bitPos++; if( bitPos == 8 ) { - WriteByte( NBits ); + WriteByte( ( byte )( NBits & 0xFF ) ); bitPos = 0; NBits = 0; } } +#else + if( numBits == 0 ) + return; + + if( bitPos == 0 ) + { + bitPos = 8; + } + + while( numBits > 0 ) + { + while( ( bitPos > 0 ) && ( numBits > 0 ) ) + { + int32 or = ( value & ( 1L << ( numBits - 1 ) ) ); + int shift = bitPos - numBits; + if( shift < 0 ) + or >>= -shift; + else + or <<= shift; + NBits |= ( int ) or; + + numBits--; + bitPos--; + } + + if( bitPos == 0 ) + { + WriteByte( NBits ); + + NBits = 0; + + if( numBits > 0 ) + { + bitPos = 8; + } + } + } +#endif } void idFile_SWF::WriteSBits( int value, int numBits ) { - WriteUBits( value, numBits ); + int32 tmp = value & 0x7FFFFFFF; + + if( value < 0 ) + { + tmp |= ( 1L << ( ( int32 )numBits - 1 ) ); + } + + WriteUBits( tmp, numBits ); } void idFile_SWF::WriteU8( uint8 value ) @@ -210,7 +262,7 @@ void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) nBits = EnlargeBitCountS( xx, nBits ); nBits = EnlargeBitCountS( yy, nBits ); - WriteUBits( 5, nBits ); + WriteUBits( nBits, 5 ); WriteSBits( xx, nBits ); WriteSBits( yy, nBits ); } @@ -228,7 +280,7 @@ void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) nBits = EnlargeBitCountS( yx, nBits ); nBits = EnlargeBitCountS( xy, nBits ); - WriteUBits( 5, nBits ); + WriteUBits( nBits, 5 ); WriteSBits( yx, nBits ); WriteSBits( xy, nBits ); } @@ -240,7 +292,7 @@ void idFile_SWF::WriteMatrix( const swfMatrix_t& matrix ) nBits = EnlargeBitCountS( tx, nBits ); nBits = EnlargeBitCountS( ty, nBits ); - WriteUBits( 5, nBits ); + WriteUBits( nBits, 5 ); WriteSBits( tx, nBits ); WriteSBits( ty, nBits ); @@ -291,7 +343,7 @@ void idFile_SWF::WriteColorXFormRGBA( const swfColorXform_t& xcf ) nBits = EnlargeBitCountS( alpha, nBits ); } - WriteUBits( 4, nBits ); + WriteUBits( nBits, 4 ); if( hasMulTerms ) { @@ -308,6 +360,8 @@ void idFile_SWF::WriteColorXFormRGBA( const swfColorXform_t& xcf ) WriteSBits( blue, nBits ); WriteSBits( alpha, nBits ); } + + ByteAlign(); } void idFile_SWF::WriteColorRGB( const swfColorRGB_t& color ) diff --git a/neo/swf/SWF_File.h b/neo/swf/SWF_File.h index 353f65aa..4853c3d2 100644 --- a/neo/swf/SWF_File.h +++ b/neo/swf/SWF_File.h @@ -58,14 +58,15 @@ public: { if( bitPos > 0 ) { - bitPos = 0; WriteByte( NBits ); + + bitPos = 0; NBits = 0; } } - static int BitCountS( const int value ); + static int BitCountS( const int64 value, bool isSigned ); static int BitCountU( const int value ); static int BitCountFloat( const float value ); @@ -96,8 +97,8 @@ private: idFile* file; // The managed file pointer. - byte bitPos; - byte NBits; + int bitPos; + int NBits; }; From 45d475936f5df7d90e0d66e817c37acb6a4984e2 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 11 Oct 2015 22:01:02 +0200 Subject: [PATCH 36/43] JSON -> Flash Tag_PlaceObject3 --- neo/swf/SWF_File.cpp | 42 +------------------------------------- neo/swf/SWF_Sprites.cpp | 45 +++++++++++++++++++++++++++++++---------- neo/swf/SWF_Types.h | 2 +- 3 files changed, 36 insertions(+), 53 deletions(-) diff --git a/neo/swf/SWF_File.cpp b/neo/swf/SWF_File.cpp index 616fea0d..a51ca007 100644 --- a/neo/swf/SWF_File.cpp +++ b/neo/swf/SWF_File.cpp @@ -127,13 +127,11 @@ void idFile_SWF::WriteByte( byte bits ) void idFile_SWF::WriteUBits( int value, int numBits ) { -#if 1 for( int bit = 0; bit < numBits; bit++ ) { int nb = ( int )( ( value >> ( numBits - 1 - bit ) ) & 1 ); NBits += nb * ( 1 << ( 7 - bitPos ) ); - //NBits += nb * ( 1 << ( bitPos ) ); bitPos++; @@ -145,44 +143,6 @@ void idFile_SWF::WriteUBits( int value, int numBits ) NBits = 0; } } -#else - if( numBits == 0 ) - return; - - if( bitPos == 0 ) - { - bitPos = 8; - } - - while( numBits > 0 ) - { - while( ( bitPos > 0 ) && ( numBits > 0 ) ) - { - int32 or = ( value & ( 1L << ( numBits - 1 ) ) ); - int shift = bitPos - numBits; - if( shift < 0 ) - or >>= -shift; - else - or <<= shift; - NBits |= ( int ) or; - - numBits--; - bitPos--; - } - - if( bitPos == 0 ) - { - WriteByte( NBits ); - - NBits = 0; - - if( numBits > 0 ) - { - bitPos = 8; - } - } - } -#endif } void idFile_SWF::WriteSBits( int value, int numBits ) @@ -209,7 +169,7 @@ void idFile_SWF::WriteU16( uint16 value ) ByteAlign(); WriteByte( value & 0xFF ); - WriteByte( value >> 8 ); + WriteByte( ( value >> 8 ) & 0xFF ); } void idFile_SWF::WriteU32( uint32 value ) diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 97ecb7ee..fda91eb6 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -323,15 +323,34 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) Value& command = c[i]; Value& type = command["type"]; -#if 1 - if( type == "Tag_PlaceObject2" ) + if( type == "Tag_PlaceObject2" || type == "Tag_PlaceObject3" ) { - commands[i].tag = Tag_PlaceObject2; + if( type == "Tag_PlaceObject3" ) + { + commands[i].tag = Tag_PlaceObject3; + } + else + { + commands[i].tag = Tag_PlaceObject2; + } idFile_SWF file( new idFile_Memory() ); - uint8 flags1 = command["flags"].GetUint(); - file.WriteU8( flags1 ); + uint8 flags1 = 0; + uint8 flags2 = 0; + if( type == "Tag_PlaceObject3" ) + { + flags1 = command["flags1"].GetUint(); + file.WriteU8( flags1 ); + + flags2 = command["flags2"].GetUint(); + file.WriteU8( flags2 ); + } + else + { + flags1 = command["flags"].GetUint(); + file.WriteU8( flags1 ); + } uint16 depth = command["depth"].GetUint(); file.WriteU16( depth ); @@ -374,7 +393,6 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) cxf.add.w = addColor[3].GetDouble(); } - // TODO file.WriteColorXFormRGBA( cxf ); } @@ -403,6 +421,12 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) file.WriteU16( clipDepth ); } + if( ( flags2 & PlaceFlagHasBlendMode ) != 0 ) + { + uint8 blendMode = command["blendMode"].GetUint(); + file.WriteU8( blendMode ); + } + if( ( flags1 & PlaceFlagHasClipActions ) != 0 ) { // FIXME: clip actions @@ -411,7 +435,6 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) uint32 streamLength = file->Length(); commands[i].stream.Load( ( byte* ) static_cast( ( idFile* )file )->GetDataPtr(), streamLength, true ); } -#endif } } @@ -497,7 +520,7 @@ void idSWFSprite::WriteJSON( idFile* f, int characterID ) void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { - uint64 flags1 = bitstream.ReadU8(); + uint8 flags1 = bitstream.ReadU8(); int depth = bitstream.ReadU16(); file->WriteFloatString( "%s\t\t\t\t{\n", ( commandID != 0 ) ? ",\n" : "" ); @@ -575,9 +598,9 @@ void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstrea void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { - uint64 flags1 = bitstream.ReadU8(); - uint64 flags2 = bitstream.ReadU8(); - int depth = bitstream.ReadU16(); + uint8 flags1 = bitstream.ReadU8(); + uint8 flags2 = bitstream.ReadU8(); + uint16 depth = bitstream.ReadU16(); file->WriteFloatString( "%s\t\t\t\t{\n", ( commandID != 0 ) ? ",\n" : "" ); file->WriteFloatString( "\t\t\t\t\t\"type\": \"Tag_PlaceObject3\",\n" ); diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 56053366..39188548 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -55,7 +55,7 @@ ID_INLINE int FLOAT2SWFFIXED16( float value ) ID_INLINE int FLOAT2SWFFIXED8( float value ) { - return ( int )( value * 255 ); + return ( int )( value * 256 ); } // RB end From a1d97245b9815e39cd4078b9b5a9741eb02bbb00 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 12 Oct 2015 13:02:18 +0200 Subject: [PATCH 37/43] Fixed merge conflicts --- neo/swf/SWF.h | 14 +++----------- neo/swf/SWF_Load.cpp | 7 ++----- neo/swf/SWF_ScriptFunction.cpp | 1 + neo/swf/SWF_ScriptFunction.h | 1 + neo/swf/SWF_Sprites.cpp | 4 +--- neo/swf/SWF_Sprites.h | 2 +- neo/swf/SWF_Types.h | 2 +- neo/swf/SWF_Zlib.cpp | 1 + 8 files changed, 11 insertions(+), 21 deletions(-) diff --git a/neo/swf/SWF.h b/neo/swf/SWF.h index cefbc453..33b2effc 100644 --- a/neo/swf/SWF.h +++ b/neo/swf/SWF.h @@ -399,25 +399,17 @@ private: bool LoadBinary( const char* bfilename, ID_TIME_T sourceTime ); void WriteBinary( const char* bfilename ); -<<<<<<< HEAD -<<<<<<< HEAD + void FileAttributes( idSWFBitStream& bitstream ); void Metadata( idSWFBitStream& bitstream ); void SetBackgroundColor( idSWFBitStream& bitstream ); -======= - // RB begin -======= -<<<<<<< HEAD ->>>>>>> 534d415... SWF export void LoadXML( const char* filename ); void WriteXML( const char* filename ); -======= - void LoadJSON( const char* filename ); + + bool LoadJSON( const char* filename ); void WriteJSON( const char* filename ); ->>>>>>> 24a45e0... JSON Flash export 1 // RB end ->>>>>>> c4098bc... XML Flash part 1 //---------------------------------- // SWF_Shapes.cpp diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 70a81880..80763e7d 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -792,7 +792,7 @@ void idSWF::WriteBinary( const char* bfilename ) } } -<<<<<<< HEAD + /* =================== idSWF::FileAttributes @@ -823,7 +823,6 @@ void idSWF::SetBackgroundColor( idSWFBitStream& bitstream ) { bitstream.Seek( 4 ); // int } -======= // RB begin @@ -1888,6 +1887,4 @@ void idSWF::WriteJSON( const char* filename ) file->WriteFloatString( "\t]\n" ); file->WriteFloatString( "}\n" ); } - // RB end ->>>>>>> c4098bc... XML Flash part 1 diff --git a/neo/swf/SWF_ScriptFunction.cpp b/neo/swf/SWF_ScriptFunction.cpp index 13f83b74..5bc70a28 100644 --- a/neo/swf/SWF_ScriptFunction.cpp +++ b/neo/swf/SWF_ScriptFunction.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). diff --git a/neo/swf/SWF_ScriptFunction.h b/neo/swf/SWF_ScriptFunction.h index 93a1fb01..b5ded847 100644 --- a/neo/swf/SWF_ScriptFunction.h +++ b/neo/swf/SWF_ScriptFunction.h @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index fda91eb6..6993256a 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -514,9 +514,7 @@ void idSWFSprite::WriteJSON( idFile* f, int characterID ) } #endif } -<<<<<<< HEAD // RB end -======= void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstream, int sourceCharacterID, int commandID, const char* indentPrefix ) { @@ -812,4 +810,4 @@ void idSWFSprite::WriteSWF( idFile_SWF& f, int characterID ) } // RB end ->>>>>>> 2f86bde... Extended Shape-Command exports + diff --git a/neo/swf/SWF_Sprites.h b/neo/swf/SWF_Sprites.h index 328db0d6..ba1f652f 100644 --- a/neo/swf/SWF_Sprites.h +++ b/neo/swf/SWF_Sprites.h @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). diff --git a/neo/swf/SWF_Types.h b/neo/swf/SWF_Types.h index 39188548..06782e35 100644 --- a/neo/swf/SWF_Types.h +++ b/neo/swf/SWF_Types.h @@ -3,7 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. -Copyright (C) 2013 Robert Beckebans +Copyright (C) 2013-2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). diff --git a/neo/swf/SWF_Zlib.cpp b/neo/swf/SWF_Zlib.cpp index f9db5e64..011eb59a 100644 --- a/neo/swf/SWF_Zlib.cpp +++ b/neo/swf/SWF_Zlib.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). From 2398006cc6b88b1da26e28db1a052cf69d983808 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 16 Oct 2015 22:35:30 +0200 Subject: [PATCH 38/43] JSON -> Flash Tag_RemoveObject2, Tag_DoAction --- neo/swf/SWF_Load.cpp | 6 +++--- neo/swf/SWF_Sprites.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 80763e7d..ff7d4e02 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -969,7 +969,7 @@ bool idSWF::LoadJSON( const char* bfilename ) { dictionary[i].type = SWF_DICT_IMAGE; - idStrStatic< MAX_OSPATH > imageName = entry["imagefile"].GetString(); + idStrStatic< MAX_OSPATH > imageName = entry["material"].GetString(); if( imageName[0] == '.' ) { // internal image in the atlas @@ -1444,11 +1444,11 @@ void idSWF::WriteJSON( const char* filename ) idStr filenameWithoutExt = filename; filenameWithoutExt.StripFileExtension(); - file->WriteFloatString( "\t\t\t\"imagefile\": \"%s/image_characterid_%i.png\",\n", filenameWithoutExt.c_str(), i ); + file->WriteFloatString( "\t\t\t\"material\": \"%s/image_characterid_%i\",\n", filenameWithoutExt.c_str(), i ); } file->WriteFloatString( "\t\t\t\"width\": %i, \"height\": %i, \"atlasOffsetX\": %i, \"atlasOffsetY\": %i,\n", - entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); + entry.imageSize[0], entry.imageSize[1], 0, 0 ); // FIXME? entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); file->WriteFloatString( "\t\t\t\"channelScale\": { \"x\": %f, \"y\": %f, \"z\": %f, \"w\": %f }\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); break; diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 6993256a..7f4f9900 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -435,6 +435,31 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) uint32 streamLength = file->Length(); commands[i].stream.Load( ( byte* ) static_cast( ( idFile* )file )->GetDataPtr(), streamLength, true ); } + else if( type == "Tag_RemoveObject2" ) + { + commands[i].tag = Tag_RemoveObject2; + + idFile_SWF file( new idFile_Memory() ); + + uint16 depth = command["depth"].GetUint(); + file.WriteU16( depth ); + + uint32 streamLength = file->Length(); + commands[i].stream.Load( ( byte* ) static_cast( ( idFile* )file )->GetDataPtr(), streamLength, true ); + } + else if( type == "Tag_DoAction" ) + { + commands[i].tag = Tag_DoAction; + + idFile_SWF file( new idFile_Memory() ); + + idBase64 base64 = command["stream"].GetString(); + base64.Decode( file ); + + uint32 streamLength = file->Length() - 1; // skip trailing zero added by Decode() + commands[i].stream.Load( ( byte* ) static_cast( ( idFile* )file )->GetDataPtr(), streamLength, true ); + } + } } From c552d8108c69a3ed01335c51d34323c3677d6730 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 16 Oct 2015 22:55:33 +0200 Subject: [PATCH 39/43] JSON Flash mainsprite reimport --- neo/swf/SWF_Load.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index ff7d4e02..42df7b41 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -958,7 +958,8 @@ bool idSWF::LoadJSON( const char* bfilename ) Value& a = d["dict"]; assert( a.IsArray() ); - dictionary.SetNum( a.Size() ); + // do not include the last item which is the main sprite + dictionary.SetNum( a.Size() - 1 ); for( SizeType i = 0; i < a.Size(); i++ ) { @@ -1293,9 +1294,16 @@ bool idSWF::LoadJSON( const char* bfilename ) } else if( type == "SPRITE" ) { - dictionary[i].type = SWF_DICT_SPRITE; - dictionary[i].sprite = new( TAG_SWF ) idSWFSprite( this ); - dictionary[i].sprite->ReadJSON( entry ); + if( entry.HasMember( "mainsprite" ) || i == ( a.Size() - 1 ) ) + { + mainsprite->ReadJSON( entry ); + } + else + { + dictionary[i].type = SWF_DICT_SPRITE; + dictionary[i].sprite = new( TAG_SWF ) idSWFSprite( this ); + dictionary[i].sprite->ReadJSON( entry ); + } } } From db560ecd7b6f8c11c20b8991cdc867ddce03d70c Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sat, 17 Oct 2015 23:33:07 +0200 Subject: [PATCH 40/43] JSON Flash improved reimport --- neo/swf/SWF_Load.cpp | 199 ++++++++++++++++++++++++++----------------- 1 file changed, 121 insertions(+), 78 deletions(-) diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 42df7b41..708fa868 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -1163,15 +1163,18 @@ bool idSWF::LoadJSON( const char* bfilename ) fillDraw.style.bitmapID = 65535; } - Value& startVerts = jsonDraw["startVerts"]; - - fillDraw.startVerts.SetNum( startVerts.Size() ); - for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + if( jsonDraw.HasMember( "startVerts" ) ) { - idVec2& vert = fillDraw.startVerts[v]; + Value& startVerts = jsonDraw["startVerts"]; - vert.x = startVerts[v]["v"][0].GetDouble(); - vert.y = startVerts[v]["v"][1].GetDouble(); + fillDraw.startVerts.SetNum( startVerts.Size() ); + for( int v = 0; v < fillDraw.startVerts.Num(); v++ ) + { + idVec2& vert = fillDraw.startVerts[v]; + + vert.x = startVerts[v]["v"][0].GetDouble(); + vert.y = startVerts[v]["v"][1].GetDouble(); + } } if( jsonDraw.HasMember( "endVerts" ) ) @@ -1190,29 +1193,30 @@ bool idSWF::LoadJSON( const char* bfilename ) } } - Value& indices = jsonDraw["indices"]; - fillDraw.indices.SetNum( indices.Size() ); - -#if 1 - for( int v = 0; v < fillDraw.indices.Num(); v++ ) + if( jsonDraw.HasMember( "indices" ) ) { - uint16& vert = fillDraw.indices[v]; + Value& indices = jsonDraw["indices"]; + fillDraw.indices.SetNum( indices.Size() ); - vert = indices[v].GetUint(); - } +#if 1 + for( int v = 0; v < fillDraw.indices.Num(); v++ ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } #else - for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) - { - uint16& vert = fillDraw.indices[v]; - - vert = indices[v].GetUint(); - } + for( int v = fillDraw.indices.Num() - 1; v >= 0; v-- ) + { + uint16& vert = fillDraw.indices[v]; + + vert = indices[v].GetUint(); + } #endif - + } } } - if( entry.HasMember( "lineDraws" ) ) { shape->lineDraws.SetNum( entry["lineDraws"].Size() ); @@ -1305,8 +1309,80 @@ bool idSWF::LoadJSON( const char* bfilename ) dictionary[i].sprite->ReadJSON( entry ); } } + else if( type == "FONT" ) + { + dictionary[i].type = SWF_DICT_FONT; + dictionary[i].font = new( TAG_SWF ) idSWFFont; + + idSWFFont* font = dictionary[i].font; + idStr fontName = entry["name"].GetString(); + font->fontID = renderSystem->RegisterFont( fontName ); + + + font->ascent = entry["ascent"].GetUint(); + font->descent = entry["descent"].GetUint(); + font->leading = entry["leading"].GetUint(); + + // RB: ignore glyphs because they are not used + } + else if( type == "EDITTEXT" ) + { + dictionary[i].type = SWF_DICT_EDITTEXT; + dictionary[i].edittext = new( TAG_SWF ) idSWFEditText; + idSWFEditText* edittext = dictionary[i].edittext; + + edittext->bounds.tl.x = entry["bounds"][0].GetDouble(); + edittext->bounds.tl.y = entry["bounds"][1].GetDouble(); + edittext->bounds.br.x = entry["bounds"][2].GetDouble(); + edittext->bounds.br.y = entry["bounds"][3].GetDouble(); + + + edittext->flags = entry["flags"].GetUint(); + edittext->fontID = entry["fontID"].GetUint(); + edittext->fontHeight = entry["fontHeight"].GetUint(); + + Value& color = entry["color"]; + edittext->color.r = ( uint8 )( color[0].GetDouble() * 255 ) & 0xFF; + edittext->color.g = ( uint8 )( color[1].GetDouble() * 255 ) & 0xFF; + edittext->color.b = ( uint8 )( color[2].GetDouble() * 255 ) & 0xFF; + edittext->color.a = ( uint8 )( color[3].GetDouble() * 255 ) & 0xFF; + + edittext->maxLength = entry["maxLength"].GetUint(); + + idStr align = entry["align"].GetString(); + if( align == "RIGHT" ) + { + edittext->align = SWF_ET_ALIGN_RIGHT; + } + else if( align == "CENTER" ) + { + edittext->align = SWF_ET_ALIGN_CENTER; + } + else if( align == "JUSTITY" ) + { + edittext->align = SWF_ET_ALIGN_JUSTIFY; + } + else + { + edittext->align = SWF_ET_ALIGN_LEFT; + } + + edittext->leftMargin = entry["leftMargin"].GetUint(); + edittext->rightMargin = entry["rightMargin"].GetUint(); + edittext->indent = entry["indent"].GetUint(); + edittext->leading = entry["leading"].GetUint(); + + edittext->variable = entry["variable"].GetString(); + edittext->initialText = entry["initialText"].GetString(); + } + else if( type == "TEXT" ) + { + // RB: FIXME? not used on BFG files + } } + return false; + #if 0 mainsprite->Read( f ); @@ -1318,37 +1394,6 @@ bool idSWF::LoadJSON( const char* bfilename ) f->ReadBig( dictionary[i].type ); switch( dictionary[i].type ) { - case SWF_DICT_SPRITE: - { - dictionary[i].sprite = new( TAG_SWF ) idSWFSprite( this ); - dictionary[i].sprite->Read( f ); - break; - } - case SWF_DICT_FONT: - { - dictionary[i].font = new( TAG_SWF ) idSWFFont; - idSWFFont* font = dictionary[i].font; - idStr fontName; - f->ReadString( fontName ); - font->fontID = renderSystem->RegisterFont( fontName ); - f->ReadBig( font->ascent ); - f->ReadBig( font->descent ); - f->ReadBig( font->leading ); - f->ReadBig( num ); - font->glyphs.SetNum( num ); - for( int g = 0; g < font->glyphs.Num(); g++ ) - { - f->ReadBig( font->glyphs[g].code ); - f->ReadBig( font->glyphs[g].advance ); - f->ReadBig( num ); - font->glyphs[g].verts.SetNum( num ); - f->ReadBigArray( font->glyphs[g].verts.Ptr(), font->glyphs[g].verts.Num() ); - f->ReadBig( num ); - font->glyphs[g].indices.SetNum( num ); - f->ReadBigArray( font->glyphs[g].indices.Ptr(), font->glyphs[g].indices.Num() ); - } - break; - } case SWF_DICT_TEXT: { dictionary[i].text = new( TAG_SWF ) idSWFText; @@ -1423,8 +1468,7 @@ void idSWF::WriteJSON( const char* filename ) return; } - //file->WriteFloatString( "\n" ); - file->WriteFloatString( "{\n \t\"version\": %i,\n \t\"timestamp\": %i,\n \t\"frameWidth\": %f,\n \t\"frameHeight\": %f,\n \t\"frameRate\": %i,\n", XSWF_VERSION, timestamp, frameWidth, frameHeight, frameRate ); + file->WriteFloatString( "{\n \t\"version\": %i,\n \t\"frameWidth\": %f,\n \t\"frameHeight\": %f,\n \t\"frameRate\": %i,\n", XSWF_VERSION, ( float )frameWidth, ( float )frameHeight, ( int )frameRate ); file->WriteFloatString( "\t\"dict\":\n\t[\n" ); for( int i = 0; i < dictionary.Num(); i++ ) @@ -1614,6 +1658,10 @@ void idSWF::WriteJSON( const char* filename ) } file->WriteFloatString( "\t\t\t\t\t]" ); } + else + { + idLib::Printf( "fillDraw %i of characterID %i has no startVerts\n", d, i ); + } if( fillDraw.endVerts.Num() ) { @@ -1648,6 +1696,16 @@ void idSWF::WriteJSON( const char* filename ) file->WriteFloatString( "]\n" ); } + if( !fillDraw.startVerts.Num() && !fillDraw.endVerts.Num() && !fillDraw.indices.Num() ) + { + file->WriteFloatString( "\n" ); + } + //else + { + // skip \t\t\t\t} + //file->WriteFloatString( "%s\n", ( d == ( shape->fillDraws.Num() - 1 ) ) ? "" : "," ); + } + file->WriteFloatString( "\t\t\t\t}%s\n", ( d == ( shape->fillDraws.Num() - 1 ) ) ? "" : "," ); } @@ -1745,8 +1803,8 @@ void idSWF::WriteJSON( const char* filename ) { const idSWFFont* font = dictionary[i].font; - file->WriteFloatString( "\t\t\t\"name\": \"%s\", \"ascent\": %i, \"descent\": %i, \"leading\": %i, \"glyphsNum\": %i\n", - font->fontID->GetName(), font->ascent, font->descent, font->leading, font->glyphs.Num() ); + file->WriteFloatString( "\t\t\t\"name\": \"%s\", \"ascent\": %i, \"descent\": %i, \"leading\": %i\n", //, \"glyphsNum\": %i\n", + font->fontID->GetName(), font->ascent, font->descent, font->leading ); //, font->glyphs.Num() ); #if 0 for( int g = 0; g < font->glyphs.Num(); g++ ) @@ -1779,6 +1837,8 @@ void idSWF::WriteJSON( const char* filename ) case SWF_DICT_TEXT: { + // RB: not used in BFG files + const idSWFText* text = dictionary[i].text; file->WriteFloatString( "\t\t\n", i ); @@ -1851,30 +1911,13 @@ void idSWF::WriteJSON( const char* filename ) et->leftMargin, et->rightMargin, et->indent, et->leading, et->variable.c_str(), initialText.c_str() ); - float x = et->bounds.tl.x; - float y = et->bounds.tl.y; - float width = fabs( et->bounds.br.x - et->bounds.tl.x ); - float height = fabs( et->bounds.br.y - et->bounds.tl.y ); + idVec2 tl = et->bounds.tl; + idVec2 br = et->bounds.br; - file->WriteFloatString( "\t\t\t\"bounds\": { \"x\": %f, \"y\": %f, \"width\": %f, \"height\": %f },\n", x, y, width, height ); + file->WriteFloatString( "\t\t\t\"bounds\": [ %f, %f, %f, %f ],\n", tl.x, tl.y, br.x, br.y ); idVec4 color = et->color.ToVec4(); file->WriteFloatString( "\t\t\t\"color\": [ %f, %f, %f, %f ]\n", color.x, color.y, color.z, color.w ); - - //file->WriteBig( et->bounds.tl ); - //file->WriteBig( et->bounds.br ); - //file->WriteBig( et->flags ); - //file->WriteBig( et->fontID ); - //file->WriteBig( et->fontHeight ); - //file->Write( &et->color, 4 ); - //file->WriteBig( et->maxLength ); - //file->WriteBig( et->align ); - //file->WriteBig( et->leftMargin ); - //file->WriteBig( et->rightMargin ); - //file->WriteBig( et->indent ); - //file->WriteBig( et->leading ); - //file->WriteString( et->variable ); - //file->WriteString( et->initialText ); break; } } From 25764b07ad309f5fb220a455a848c6152ffe9260 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Thu, 22 Oct 2015 23:46:52 +0200 Subject: [PATCH 41/43] JSON Flash image reimport --- neo/swf/SWF_Image.cpp | 2 +- neo/swf/SWF_Load.cpp | 116 +++++++++++----------------------------- neo/swf/SWF_Main.cpp | 18 ++++--- neo/swf/SWF_Sprites.cpp | 11 ++-- 4 files changed, 52 insertions(+), 95 deletions(-) diff --git a/neo/swf/SWF_Image.cpp b/neo/swf/SWF_Image.cpp index 267e7722..1ee33a65 100644 --- a/neo/swf/SWF_Image.cpp +++ b/neo/swf/SWF_Image.cpp @@ -328,7 +328,7 @@ void idSWF::WriteSwfImageAtlas( const char* filename ) } // the TGA is only for examination during development - R_WriteTGA( filename, swfAtlas.Ptr(), atlasWidth, atlasHeight, false, "fs_basepath" ); + R_WritePNG( filename, swfAtlas.Ptr(), 4, atlasWidth, atlasHeight, false, "fs_basepath" ); } /* diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 708fa868..06eceba8 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -902,9 +902,9 @@ struct MyHandler idSWF::LoadJSON =================== */ -bool idSWF::LoadJSON( const char* bfilename ) +bool idSWF::LoadJSON( const char* filename ) { - idFile* f = fileSystem->OpenFileReadMemory( bfilename ); + idFile* f = fileSystem->OpenFileReadMemory( filename ); if( f == NULL || f->Length() <= 0 ) { return false; @@ -968,9 +968,9 @@ bool idSWF::LoadJSON( const char* bfilename ) if( type == "IMAGE" ) { - dictionary[i].type = SWF_DICT_IMAGE; + //dictionary[i].type = SWF_DICT_IMAGE; - idStrStatic< MAX_OSPATH > imageName = entry["material"].GetString(); + idStrStatic< MAX_OSPATH > imageName = entry["imageFile"].GetString(); if( imageName[0] == '.' ) { // internal image in the atlas @@ -981,15 +981,25 @@ bool idSWF::LoadJSON( const char* bfilename ) dictionary[i].material = declManager->FindMaterial( imageName ); } - dictionary[i].imageSize[0] = entry["width"].GetDouble(); - dictionary[i].imageSize[1] = entry["height"].GetDouble(); + //dictionary[i].imageSize[0] = entry["width"].GetDouble(); + //dictionary[i].imageSize[1] = entry["height"].GetDouble(); - idVec4& channelScale = dictionary[i].channelScale; + //idVec4& channelScale = dictionary[i].channelScale; + //channelScale.x = entry["channelScale"]["x"].GetDouble(); + //channelScale.y = entry["channelScale"]["y"].GetDouble(); + //channelScale.z = entry["channelScale"]["z"].GetDouble(); + //channelScale.w = entry["channelScale"]["w"].GetDouble(); - channelScale.x = entry["channelScale"]["x"].GetDouble(); - channelScale.y = entry["channelScale"]["y"].GetDouble(); - channelScale.z = entry["channelScale"]["z"].GetDouble(); - channelScale.w = entry["channelScale"]["w"].GetDouble(); + byte* imageData = NULL; + int width, height; + ID_TIME_T timestamp; + R_LoadImage( imageName.c_str(), &imageData, &width, &height, ×tamp, false ); + if( imageData != NULL ) + { + LoadImage( i, imageData, width, height ); + + Mem_Free( imageData ); + } } else if( type == "SHAPE" || type == "MORPH" ) { @@ -1381,76 +1391,14 @@ bool idSWF::LoadJSON( const char* bfilename ) } } - return false; + // now that all images have been loaded, write out the combined image + idStr atlasFileName = "generated/"; + atlasFileName += filename; + atlasFileName.SetFileExtension( ".png" ); -#if 0 - mainsprite->Read( f ); - - int num = 0; - f->ReadBig( num ); - dictionary.SetNum( num ); - for( int i = 0; i < dictionary.Num(); i++ ) - { - f->ReadBig( dictionary[i].type ); - switch( dictionary[i].type ) - { - case SWF_DICT_TEXT: - { - dictionary[i].text = new( TAG_SWF ) idSWFText; - idSWFText* text = dictionary[i].text; - f->ReadBig( text->bounds.tl ); - f->ReadBig( text->bounds.br ); - f->ReadBigArray( ( float* )&text->matrix, 6 ); - f->ReadBig( num ); - text->textRecords.SetNum( num ); - for( int t = 0; t < text->textRecords.Num(); t++ ) - { - idSWFTextRecord& textRecord = text->textRecords[t]; - f->ReadBig( textRecord.fontID ); - f->Read( &textRecord.color, 4 ); - f->ReadBig( textRecord.xOffset ); - f->ReadBig( textRecord.yOffset ); - f->ReadBig( textRecord.textHeight ); - f->ReadBig( textRecord.firstGlyph ); - f->ReadBig( textRecord.numGlyphs ); - } - f->ReadBig( num ); - text->glyphs.SetNum( num ); - for( int g = 0; g < text->glyphs.Num(); g++ ) - { - f->ReadBig( text->glyphs[g].index ); - f->ReadBig( text->glyphs[g].advance ); - } - break; - } - case SWF_DICT_EDITTEXT: - { - dictionary[i].edittext = new( TAG_SWF ) idSWFEditText; - idSWFEditText* edittext = dictionary[i].edittext; - f->ReadBig( edittext->bounds.tl ); - f->ReadBig( edittext->bounds.br ); - f->ReadBig( edittext->flags ); - f->ReadBig( edittext->fontID ); - f->ReadBig( edittext->fontHeight ); - f->Read( &edittext->color, 4 ); - f->ReadBig( edittext->maxLength ); - f->ReadBig( edittext->align ); - f->ReadBig( edittext->leftMargin ); - f->ReadBig( edittext->rightMargin ); - f->ReadBig( edittext->indent ); - f->ReadBig( edittext->leading ); - f->ReadString( edittext->variable ); - f->ReadString( edittext->initialText ); - break; - } - } - } - delete f; + WriteSwfImageAtlas( atlasFileName ); return true; -#else - return false; -#endif } /* @@ -1458,11 +1406,11 @@ bool idSWF::LoadJSON( const char* bfilename ) idSWF::WriteJSON =================== */ -void idSWF::WriteJSON( const char* filename ) +void idSWF::WriteJSON( const char* jsonFilename ) { const bool exportBitmapShapesOnly = false; - idFileLocal file( fileSystem->OpenFileWrite( filename, "fs_basepath" ) ); + idFileLocal file( fileSystem->OpenFileWrite( jsonFilename, "fs_basepath" ) ); if( file == NULL ) { return; @@ -1489,18 +1437,18 @@ void idSWF::WriteJSON( const char* filename ) { if( dictionary[i].material ) { - file->WriteFloatString( "\t\t\t\"material\": \"%s\",\n", dictionary[i].material->GetName() ); + file->WriteFloatString( "\t\t\t\"imageFile\": \"%s\",\n", dictionary[i].material->GetName() ); } else { idStr filenameWithoutExt = filename; filenameWithoutExt.StripFileExtension(); - file->WriteFloatString( "\t\t\t\"material\": \"%s/image_characterid_%i\",\n", filenameWithoutExt.c_str(), i ); + file->WriteFloatString( "\t\t\t\"imageFile\": \"%s/image_characterid_%i.png\",\n", filenameWithoutExt.c_str(), i ); } file->WriteFloatString( "\t\t\t\"width\": %i, \"height\": %i, \"atlasOffsetX\": %i, \"atlasOffsetY\": %i,\n", - entry.imageSize[0], entry.imageSize[1], 0, 0 ); // FIXME? entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); + entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); file->WriteFloatString( "\t\t\t\"channelScale\": { \"x\": %f, \"y\": %f, \"z\": %f, \"w\": %f }\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); break; @@ -1531,7 +1479,7 @@ void idSWF::WriteJSON( const char* filename ) if( shape->fillDraws.Num() > 1 ) { - idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "%s.Shape%i has %i fill draws\n", filename, i, shape->fillDraws.Num() ); + idLib::Printf( S_COLOR_YELLOW "WARNING: " S_COLOR_RED "%s.Shape%i has %i fill draws\n", filename.c_str(), i, shape->fillDraws.Num() ); } for( int d = 0; d < shape->fillDraws.Num(); d++ ) diff --git a/neo/swf/SWF_Main.cpp b/neo/swf/SWF_Main.cpp index 2c109fa9..c89ad6f3 100644 --- a/neo/swf/SWF_Main.cpp +++ b/neo/swf/SWF_Main.cpp @@ -145,18 +145,20 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) jsonFileName.SetFileExtension( ".json" ); ID_TIME_T jsonSourceTime = fileSystem->GetTimestamp( jsonFileName ); + bool loadedFromJSON = false; if( swf_loadBinary.GetBool() ) { - ID_TIME_T sourceTime = fileSystem->GetTimestamp( filename ); - if( sourceTime == FILE_NOT_FOUND_TIMESTAMP ) + if( timestamp == FILE_NOT_FOUND_TIMESTAMP ) { - sourceTime = jsonSourceTime; + timestamp = jsonSourceTime; } - if( !LoadBinary( binaryFileName, sourceTime ) ) + if( !LoadBinary( binaryFileName, timestamp ) ) { if( LoadJSON( jsonFileName ) ) { + loadedFromJSON = true; + WriteBinary( binaryFileName ); } else if( LoadSWF( filename ) ) @@ -167,7 +169,11 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) } else { - if( !LoadJSON( jsonFileName ) ) + if( LoadJSON( jsonFileName ) ) + { + loadedFromJSON = true; + } + else { LoadSWF( filename ); } @@ -190,7 +196,7 @@ idSWF::idSWF( const char* filename_, idSoundWorld* soundWorld_ ) int atlasExportImageWidth = 0; int atlasExportImageHeight = 0; - if( swf_exportAtlas.GetBool() || swf_exportSWF.GetBool() ) + if( /*!loadedFromJSON &&*/ ( swf_exportAtlas.GetBool() || swf_exportSWF.GetBool() ) ) { idStrStatic< MAX_OSPATH > generatedName = atlasFileName; generatedName.StripFileExtension(); diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 7f4f9900..3bc49331 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -725,13 +725,13 @@ void idSWFSprite::WriteJSON_DoAction( idFile* file, idSWFBitStream& bitstream, i base64.Encode( bitstream.Ptr(), bitstream.Length() ); - file->WriteFloatString( "%s\t\t\t\t{\t\"type\": \"Tag_DoAction\", \"streamLength\": %i, \"stream\": \"%s\" }", ( commandID != 0 ) ? ",\n" : "", bitstream.Length(), base64.c_str() ); - #if 0 + file->WriteFloatString( "%s\t\t\t\t{\t\"type\": \"Tag_DoAction\", \"streamLength\": %i, \"stream\": \"%s\" }", ( commandID != 0 ) ? ",\n" : "", bitstream.Length(), base64.c_str() ); +#else idSWFScriptObject* scriptObject = idSWFScriptObject::Alloc(); scriptObject->SetPrototype( &spriteInstanceScriptObjectPrototype ); // scriptObject->SetSprite( this ); - + idSWFScriptFunction_Script* actionScript = idSWFScriptFunction_Script::Alloc(); idList scope; @@ -739,9 +739,12 @@ void idSWFSprite::WriteJSON_DoAction( idFile* file, idSWFBitStream& bitstream, i scope.Append( scriptObject ); actionScript->SetScope( scope ); // actionScript->SetDefaultSprite( this ); - + actionScript->SetData( bitstream.Ptr(), bitstream.Length() ); idStr scriptText = actionScript->CallToScript( scriptObject, idSWFParmList(), file->GetName(), characterID, commandID ); + idStr quotedText = idStr::CStyleQuote( scriptText.c_str() ); + + file->WriteFloatString( "%s\t\t\t\t{\n\t\t\t\t\t\"type\": \"Tag_DoAction\", \"streamLength\": %i, \"stream\": \"%s\",\n\t\t\t\t\t\"luaCode\": %s\n\t\t\t\t}", ( commandID != 0 ) ? ",\n" : "", bitstream.Length(), base64.c_str(), quotedText.c_str() ); //file->WriteFloatString( "%s\t%s\n", indentPrefix, bitstream.Length(), base64.c_str() ); From 4b1f6cfaf34dab440be4cb8b1bbb6af236b9f65f Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Thu, 22 Oct 2015 23:48:38 +0200 Subject: [PATCH 42/43] Defaulted fs_resourceLoadPriority to 0 --- neo/framework/FileSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/framework/FileSystem.cpp b/neo/framework/FileSystem.cpp index e6608ce6..fe870963 100644 --- a/neo/framework/FileSystem.cpp +++ b/neo/framework/FileSystem.cpp @@ -307,7 +307,7 @@ idCVar idFileSystemLocal::fs_game_base( "fs_game_base", "", CVAR_SYSTEM | CVAR_ idCVar fs_basepath( "fs_basepath", "", CVAR_SYSTEM | CVAR_INIT, "" ); idCVar fs_savepath( "fs_savepath", "", CVAR_SYSTEM | CVAR_INIT, "" ); -idCVar fs_resourceLoadPriority( "fs_resourceLoadPriority", "1", CVAR_SYSTEM , "if 1, open requests will be honored from resource files first; if 0, the resource files are checked after normal search paths" ); +idCVar fs_resourceLoadPriority( "fs_resourceLoadPriority", "0", CVAR_SYSTEM , "if 1, open requests will be honored from resource files first; if 0, the resource files are checked after normal search paths" ); idCVar fs_enableBackgroundCaching( "fs_enableBackgroundCaching", "1", CVAR_SYSTEM , "if 1 allow the 360 to precache game files in the background" ); idFileSystemLocal fileSystemLocal; From cae1397d78798f7483958249a426ef5df63fd417 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Sun, 22 Nov 2015 17:01:43 +0100 Subject: [PATCH 43/43] Disabled SWF Image channelScale export --- neo/swf/SWF_Image.cpp | 25 ++++++++++++++++++------- neo/swf/SWF_Load.cpp | 3 ++- neo/swf/SWF_Sprites.cpp | 14 ++++++-------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/neo/swf/SWF_Image.cpp b/neo/swf/SWF_Image.cpp index 1ee33a65..09afadcf 100644 --- a/neo/swf/SWF_Image.cpp +++ b/neo/swf/SWF_Image.cpp @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2015 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -31,6 +32,8 @@ If you have questions concerning this license or the applicable additional terms //#include "../../renderer/ImageTools/ImageProcess.h" #include +idCVar swf_useChannelScale( "swf_useChannelScale", "0", CVAR_BOOL, "compress texture atlas colors" ); + /* ======================== idSWF::idDecompressJPEG @@ -269,12 +272,16 @@ void idSWF::WriteSwfImageAtlas( const char* filename ) // a bias as well as a scale to enable us to take advantage of the // min values as well as the max, but very few gui images don't go to black, // and just doing a scale avoids changing more code. - for( int j = 0; j < pack.trueSize.x * pack.trueSize.y * 4; j++ ) + + if( swf_useChannelScale.GetBool() ) { - int v = pack.imageData[ j ]; - int x = j & 3; - v = v * 255 / maxV[x]; - pack.imageData[ j ] = v; + for( int j = 0; j < pack.trueSize.x * pack.trueSize.y * 4; j++ ) + { + int v = pack.imageData[ j ]; + int x = j & 3; + v = v * 255 / maxV[x]; + pack.imageData[ j ] = v; + } } assert( ( x + blockWidth ) * 4 <= atlasWidth ); @@ -318,9 +325,13 @@ void idSWF::WriteSwfImageAtlas( const char* filename ) entry->imageSize.y = pack.trueSize.y; entry->imageAtlasOffset.x = x + 1; entry->imageAtlasOffset.y = y + 1; - for( int i = 0; i < 4; i++ ) + + if( swf_useChannelScale.GetBool() ) { - entry->channelScale[i] = maxV[i] / 255.0f; + for( int i = 0; i < 4; i++ ) + { + entry->channelScale[i] = maxV[i] / 255.0f; + } } Mem_Free( pack.imageData ); diff --git a/neo/swf/SWF_Load.cpp b/neo/swf/SWF_Load.cpp index 06eceba8..ee22d36c 100644 --- a/neo/swf/SWF_Load.cpp +++ b/neo/swf/SWF_Load.cpp @@ -1450,7 +1450,8 @@ void idSWF::WriteJSON( const char* jsonFilename ) file->WriteFloatString( "\t\t\t\"width\": %i, \"height\": %i, \"atlasOffsetX\": %i, \"atlasOffsetY\": %i,\n", entry.imageSize[0], entry.imageSize[1], entry.imageAtlasOffset[0], entry.imageAtlasOffset[1] ); - file->WriteFloatString( "\t\t\t\"channelScale\": { \"x\": %f, \"y\": %f, \"z\": %f, \"w\": %f }\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); + //file->WriteFloatString( "\t\t\t\"channelScale\": { \"x\": %f, \"y\": %f, \"z\": %f, \"w\": %f }\n", entry.channelScale.x, entry.channelScale.y, entry.channelScale.z, entry.channelScale.w ); + file->WriteFloatString( "\t\t\t\"channelScale\": { \"x\": 1, \"y\": 1, \"z\": 1, \"w\": 1 }\n" ); break; } diff --git a/neo/swf/SWF_Sprites.cpp b/neo/swf/SWF_Sprites.cpp index 3bc49331..993bc239 100644 --- a/neo/swf/SWF_Sprites.cpp +++ b/neo/swf/SWF_Sprites.cpp @@ -398,8 +398,8 @@ void idSWFSprite::ReadJSON( rapidjson::Value& entry ) if( ( flags1 & PlaceFlagHasRatio ) != 0 ) { - float ratio = command["ratio"].GetDouble(); - file.WriteU16( ( uint16 )( ratio * 65535 ) ); + uint16 ratio = command["ratio"].GetUint(); + file.WriteU16( ratio ); } if( ( flags1 & PlaceFlagHasName ) != 0 ) @@ -582,9 +582,8 @@ void idSWFSprite::WriteJSON_PlaceObject2( idFile* file, idSWFBitStream& bitstrea if( ( flags1 & PlaceFlagHasRatio ) != 0 ) { - float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); - - file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %f", ratio ); + uint16 ratio = bitstream.ReadU16(); + file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %i", ratio ); } if( ( flags1 & PlaceFlagHasName ) != 0 ) @@ -661,9 +660,8 @@ void idSWFSprite::WriteJSON_PlaceObject3( idFile* file, idSWFBitStream& bitstrea if( ( flags1 & PlaceFlagHasRatio ) != 0 ) { - float ratio = bitstream.ReadU16() * ( 1.0f / 65535.0f ); - - file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %f", ratio ); + uint16 ratio = bitstream.ReadU16(); + file->WriteFloatString( ",\n\t\t\t\t\t\"ratio\": %i", ratio ); } if( ( flags1 & PlaceFlagHasName ) != 0 )