//virtual std::string CsSerialiserPackageObjectCodec::serialiseAsStringRef( void* InData, const ReClass* InType ) { std::string RetVal; if( Package_ != nullptr ) { const ReClass* InClass = static_cast< const ReClass* >( InType ); // Check if it's a resource. if( InClass->hasBaseClass( ReObject::StaticGetClass() ) ) { CsResource* Resource = reinterpret_cast< CsResource* >( InData ); ReObject* ResourceRootOwner = Resource->getRootOwner(); if( ResourceRootOwner != nullptr ) { BcChar OutChars[ 128 ]; BcSPrintf( OutChars, "$(%s:%s.%s)", (*Resource->getClass()->getName()).c_str(), (*ResourceRootOwner->getName()).c_str(), (*Resource->getName()).c_str() ); RetVal = OutChars; } } } if( RetVal.empty() ) { // Default formatting. BcChar OutChars[ 128 ]; BcSPrintf( OutChars, "$(%s:%s.%llu)", (*InType->getName()).c_str(), ( "this" ), ( (unsigned long long)InData ) ); } return RetVal; }
void GaMatchmakingState::event_channel(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { GaMatchmakingState* pBridge = reinterpret_cast< GaMatchmakingState* >( irc_get_ctx( session ) ); BcScopedLock< BcMutex > Lock( pBridge->Lock_ ); // If we aren't currently handshaking if( pBridge->HandshakeState_ == HSS_WAIT_INVITE ) { // Grab short name from origin. BcChar NameCopy[ 256 ]; BcStrCopy( NameCopy, origin ); BcChar* pNameEnd = BcStrStr( NameCopy, "!" ); if( pNameEnd != NULL ) { *pNameEnd = '\0'; } BcChar PlayBuffer[256]; BcSPrintf( PlayBuffer, "REQ:%u", pBridge->SysID_ ); // If the message isn't from ourself, and it's the play with me message, then send our local address. if( !BcStrCompare( pBridge->ScreenName_, NameCopy ) && BcStrCompare( params[1], PlayBuffer ) ) { if( pBridge->sendLocalAddress( NameCopy ) ) { // Invite player to play, we send address first. ClientID_ = 0; // Give handshake 15 seconds to complete. pBridge->HandshakeTimer_ = 15.0f; // Set wait for address state. pBridge->HandshakeState_ = HSS_WAIT_ADDR; } } } }
////////////////////////////////////////////////////////////////////////// // create BcBool OsClientWindows::create( const BcChar* pTitle, BcHandle Instance, BcU32 Width, BcU32 Height, BcBool Fullscreen, BcBool Visible ) { WNDCLASSEX wc; RECT WindowRect; WindowRect.left = (long)0; WindowRect.right = (long)Width; WindowRect.top = (long)0; WindowRect.bottom = (long)Height; hInstance_ = Instance; // Enumerate device guff. BOOL EnumSuccess = FALSE; BcU32 ModeIdx = 0; do { DEVMODEA DevMode; ZeroMemory( &DevMode, sizeof( DevMode ) ); EnumSuccess = ::EnumDisplaySettingsA( nullptr, ModeIdx++, &DevMode ); if( EnumSuccess ) { DeviceModes_.push_back( DevMode ); } } while( EnumSuccess ); BcU32 DeviceIdx = 0; do { DISPLAY_DEVICEA DisplayDevice; ZeroMemory( &DisplayDevice, sizeof( DisplayDevice ) ); EnumSuccess = ::EnumDisplayDevicesA( nullptr, DeviceIdx++, &DisplayDevice, EDD_GET_DEVICE_INTERFACE_NAME ); if( EnumSuccess ) { DisplayDevices_.push_back( DisplayDevice ); } } while( EnumSuccess ); BcU32 MonitorIdx = 0; do { MONITORINFO MonitorInfo; ZeroMemory( &MonitorInfo, sizeof( MonitorInfo ) ); EnumSuccess = ::GetMonitorInfoA( (HMONITOR)MonitorIdx, &MonitorInfo ); if( EnumSuccess ) { MonitorInfos_.push_back( MonitorInfo ); } } while( EnumSuccess ); // Generate class name. BcSPrintf( ClassName_, "PsybrusWindow_0x%x", gClassID_++ ); wc.cbSize = sizeof( WNDCLASSEX ); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = (HINSTANCE)Instance; wc.hIcon = ::LoadIcon( NULL, IDI_WINLOGO ); wc.hCursor = ::LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)( COLOR_WINDOW ); wc.lpszMenuName = NULL; wc.lpszClassName = ClassName_; wc.hIconSm = NULL; if ( !RegisterClassEx( &wc ) ) { return BcFalse; } if( Fullscreen == BcFalse ) { WindowStyleEx_ = WS_EX_WINDOWEDGE; WindowStyle_ = WS_CAPTION | WS_SYSMENU | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX; } else { WindowStyleEx_ = WS_EX_APPWINDOW; WindowStyle_ = WS_POPUP; } ::AdjustWindowRectEx( &WindowRect, WindowStyle_, FALSE, WindowStyleEx_); centreWindow( Width, Height ); // Create The Window if ( !( hWnd_= ::CreateWindowEx( WindowStyleEx_, ClassName_, pTitle, WindowStyle_, WindowSize_.left, WindowSize_.top, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, (HINSTANCE)Instance, NULL))) { return BcFalse; } // Set user data. ::SetWindowLongPtr( hWnd_, GWLP_USERDATA, (LONG_PTR)this ); // Get the device context hDC_ = GetDC( hWnd_ ); // Show the window if( Visible ) { ::ShowWindow( hWnd_, SW_SHOWNORMAL ); ::SetForegroundWindow( hWnd_ ); ::SetFocus( hWnd_ ); } return BcTrue; }
//////////////////////////////////////////////////////////////////////////////// // 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; }
////////////////////////////////////////////////////////////////////////// // create BcBool OsClientWindows::create( const BcChar* pTitle, BcHandle Instance, BcU32 Width, BcU32 Height, BcBool Fullscreen, BcBool Visible ) { WNDCLASSEX wc; RECT WindowRect; WindowRect.left = (long)0; WindowRect.right = (long)Width; WindowRect.top = (long)0; WindowRect.bottom = (long)Height; hInstance_ = Instance; // Generate class name. BcSPrintf( ClassName_, "PsybrusWindow_0x%x", gClassID_++ ); wc.cbSize = sizeof( WNDCLASSEX ); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = (HINSTANCE)Instance; wc.hIcon = ::LoadIcon( NULL, IDI_WINLOGO ); wc.hCursor = ::LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)( COLOR_WINDOW ); wc.lpszMenuName = NULL; wc.lpszClassName = ClassName_; wc.hIconSm = NULL; if ( !RegisterClassEx( &wc ) ) { return BcFalse; } if( Fullscreen == BcFalse ) { WindowStyleEx_ = WS_EX_WINDOWEDGE; WindowStyle_ = WS_CAPTION | WS_SYSMENU | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX; } else { WindowStyleEx_ = WS_EX_APPWINDOW; WindowStyle_ = WS_POPUP; } ::AdjustWindowRectEx( &WindowRect, WindowStyle_, FALSE, WindowStyleEx_); centreWindow( Width, Height ); // Create The Window if ( !( hWnd_=::CreateWindowEx( WindowStyleEx_, ClassName_, pTitle, WindowStyle_, WindowSize_.left, WindowSize_.top, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, (HINSTANCE)Instance, NULL))) { return BcFalse; } // Set user data. ::SetWindowLongPtr( hWnd_, GWL_USERDATA, (LONG_PTR)this ); // Get the device context hDC_ = GetDC( hWnd_ ); // Show the window if( Visible ) { ::ShowWindow( hWnd_, SW_SHOW ); ::SetForegroundWindow( hWnd_ ); ::SetFocus( hWnd_ ); } return BcTrue; }