////////////////////////////////////////////////////////////////////////// // parseJsonFile BcBool CsCore::parseJsonFile( const BcChar* pFileName, Json::Value& Root ) { BcBool Success = BcFalse; BcFile File; if( File.open( pFileName ) ) { char* pData = new char[ File.size() ]; File.read( pData, File.size() ); Json::Reader Reader; if( Reader.parse( pData, pData + File.size(), Root ) ) { Success = BcTrue; // Add file for monitoring. FsCore::pImpl()->addFileMonitor( pFileName ); } else { BcPrintf( "CsCore: Failed to parse Json:\n %s\n", Reader.getFormatedErrorMessages().c_str() ); } delete [] pData; } return Success; }
////////////////////////////////////////////////////////////////////////// // Gets a plain text file std::string DsTemplate::loadTemplateFile( std::string filename ) { BcFile file; std::string f = filename; file.open( f.c_str() ); // PSY_LOG("Loading file: %s (size: %d)\n", filename.c_str(), file.size()); if ( !file.isOpen() ) return ""; char* data = new char[file.size() + 1]; BcMemZero(data, file.size() + 1); file.read(data, file.size()); std::string output = data; delete data; return output; }
////////////////////////////////////////////////////////////////////////// // loadJsonFile BcBool CsPackageImporter::loadJsonFile( const BcChar* pFileName, Json::Value& Root ) { BcBool Success = BcFalse; BcFile File; if( File.open( pFileName ) ) { const BcU8* pData = File.readAllBytes(); Json::Reader Reader; if( Reader.parse( (const char*)pData, (const char*)pData + File.size(), Root ) ) { Success = BcTrue; } else { PSY_LOG( "Failed to parse Json:\n %s\n", Reader.getFormatedErrorMessages().c_str() ); BcAssertMsg( BcFalse, "Failed to parse \"%s\", see log for more details.", pFileName ); } BcMemFree( (void*)pData ); } else { BcAssertMsg( BcFalse, "Failed to load \"%s\"", pFileName ); } return Success; }
////////////////////////////////////////////////////////////////////////// // loadDependancies void CsCore::loadDependancies( const BcPath& FileName ) { BcPath DependanciesFileName( FileName ); // Append new extension. DependanciesFileName.append( ".dep" ); // Overwrite old dependancy list. DependancyMap_[ *FileName ] = CsDependancyList(); // Open dependancy file. BcFile OutFile; if( OutFile.open( (*DependanciesFileName).c_str(), bcFM_READ ) ) { BcU32 BufferSize = OutFile.size() + 1; BcChar* pJsonData = new BcChar[ BufferSize ]; BcMemZero( pJsonData, BufferSize ); OutFile.read( pJsonData, OutFile.size() ); Json::Value Root; Json::Reader JsonReader; if( JsonReader.parse( pJsonData, pJsonData + OutFile.size(), Root ) ) { // Create a new dependancy list. CsDependancyList DependancyList; // Iterate over all dependancies for file and parse. for( Json::Value::iterator It( Root.begin() ); It != Root.end(); ++It ) { const Json::Value& Value = (*It); CsDependancy Dependancy = loadDependancy( Value ); DependancyList.push_back( Dependancy ); } // Assign. DependancyMap_[ *FileName ] = DependancyList; } OutFile.close(); } }
//////////////////////////////////////////////////////////////////////////////// // update eSysStateReturn GaMatchmakingState::main() { BcScopedLock< BcMutex > Lock( Lock_ ); BcReal Delta = SysKernel::pImpl()->getFrameTime(); switch( HandshakeState_ ) { case HSS_STUN: { // Only do once. if( MappedHandshakeAddr_ == 0 ) { if( doSTUN() ) { SysID_ = static_cast< BcU32 >( BcHash( (BcU8*)&MappedHandshakeAddr_, sizeof( MappedHandshakeAddr_ ) ) ); // Hash the mapped address to we don't broadcast it. if( MappedHandshakeAddr_ != 0 ) { HandshakeState_ = HSS_IDLE; } } else { HandshakeState_ = HSS_STUN; } } else { HandshakeState_ = HSS_IDLE; } } break; case HSS_IDLE: { ConnectTimer_ -= Delta; if( ConnectTimer_ < 0.0f ) { if( pSession_ == NULL ) { pSession_ = irc_create_session( &Callbacks_ ); } if( pSession_ != NULL && !irc_is_connected( pSession_ ) ) { irc_set_ctx( pSession_, this ); std::string Channel = "#testchannel"; BcFile File; if( File.open( "config.json" ) ) { char* pData = new char[ File.size() ]; File.read( pData, File.size() ); Json::Reader Reader; Json::Value Root; if( Reader.parse( pData, pData + File.size(), Root ) ) { Channel = Root["channel"].asCString(); } delete [] pData; } BcSPrintf( ScreenName_, "%s_%x", "PSY", BcRandom::Global.rand() ); BcSPrintf( Channel_, Channel.c_str() ); // Connect to the server. int RetVal = irc_connect( pSession_, "www.neilo.gd", 8000, NULL, ScreenName_, ScreenName_, ScreenName_ ); if( RetVal == 0 ) { // Start the thread to tick the client. BcThread::start( "EvtBridgeIRC" ); ClientID_ = BcErrorCode; RemoteHandshakeAddr_ = 0; RemoteHandshakePort_ = 0; //LocalHandshakeAddr_ = 0; //LocalHandshakePort_ = 0; //MappedHandshakeAddr_ = 0; //MappedHandshakePort_ = 0; HandshakeState_ = HSS_WAIT_INVITE; } else { BcThread::join(); irc_destroy_session( pSession_ ); pSession_ = NULL; } } } } break; case HSS_WAIT_INVITE: { InviteTimer_ -= Delta; if( InviteTimer_ < 0.0f ) { InviteTimer_ = BcAbs( BcRandom::Global.randReal() ) * 5.0f + 5.0f; // Send play with me message to channel. BcChar PlayBuffer[256]; BcSPrintf( PlayBuffer, "REQ:%u", SysID_ ); irc_cmd_msg( pSession_, Channel_, PlayBuffer ); } } break; case HSS_WAIT_ADDR: { HandshakeTimer_ -= Delta; if( HandshakeTimer_ < 0.0f ) { HandshakeState_ = HSS_WAIT_INVITE; } } break; case HSS_COMPLETE: { BcPrintf("GaMatchmakingState: Complete! ClientID of ours is %u\n", ClientID_); return sysSR_FINISHED; } break; } if( HandshakeState_ != HSS_STUN ) { if( HandshakeState_ != HSS_IDLE && ( pSession_ == NULL || !irc_is_connected( pSession_ ) ) ) { BcSleep( 0.1f ); BcThread::join(); BcSleep( 0.1f ); if( pSession_ != NULL ) { irc_destroy_session( pSession_ ); pSession_ = NULL; } HandshakeState_ = HSS_IDLE; ConnectTimer_ = 10.0f; } } return sysSR_CONTINUE; }