doom3-bfg/neo/framework/PlayerProfile.cpp

468 lines
11 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
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 <http://www.gnu.org/licenses/>.
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"
2012-11-26 18:58:24 +00:00
#pragma hdrstop
#include "PlayerProfile.h"
// After releasing a version to the market, here are limitations for compatibility:
// - the major version should not ever change
// - always add new items to the bottom of the save/load routine
// - never remove or change the size of items, just stop using them or add new items to the end
//
// The biggest reason these limitations exist is because if a newer profile is created and then loaded with a GMC
// version, we have to support it.
const int16 PROFILE_TAG = ( 'D' << 8 ) | '3';
const int8 PROFILE_VER_MAJOR = 10; // If this is changed, you should reset the minor version and remove all backward compatible code
const int8 PROFILE_VER_MINOR = 0; // Within each major version, minor versions can be supported for backward compatibility
class idPlayerProfileLocal : public idPlayerProfile
{
2012-11-26 18:58:24 +00:00
};
idPlayerProfileLocal playerProfiles[MAX_INPUT_DEVICES];
/*
========================
Contains data that needs to be saved out on a per player profile basis, global for the lifetime of the player so
the data can be shared across computers.
- HUD tint colors
2012-11-26 18:58:24 +00:00
- key bindings
- etc...
========================
*/
/*
========================
idPlayerProfile * CreatePlayerProfile
========================
*/
idPlayerProfile* idPlayerProfile::CreatePlayerProfile( int deviceIndex )
{
2012-11-26 18:58:24 +00:00
playerProfiles[deviceIndex].SetDefaults();
playerProfiles[deviceIndex].deviceNum = deviceIndex;
return &playerProfiles[deviceIndex];
}
/*
========================
idPlayerProfile::idPlayerProfile
========================
*/
idPlayerProfile::idPlayerProfile()
{
2012-11-26 18:58:24 +00:00
SetDefaults();
2012-11-26 18:58:24 +00:00
// Don't have these in SetDefaults because they're used for state management and SetDefaults is called when
// loading the profile
state = IDLE;
requestedState = IDLE;
deviceNum = -1;
dirty = false;
}
/*
========================
idPlayerProfile::SetDefaults
========================
*/
void idPlayerProfile::SetDefaults()
{
2012-11-26 18:58:24 +00:00
achievementBits = 0;
achievementBits2 = 0;
dlcReleaseVersion = 0;
2012-11-26 18:58:24 +00:00
stats.SetNum( MAX_PLAYER_PROFILE_STATS );
for( int i = 0; i < MAX_PLAYER_PROFILE_STATS; ++i )
{
2012-11-26 18:58:24 +00:00
stats[i].i = 0;
}
2012-11-26 18:58:24 +00:00
leftyFlip = false;
customConfig = false;
configSet = 0;
}
/*
========================
idPlayerProfile::~idPlayerProfile
========================
*/
idPlayerProfile::~idPlayerProfile()
{
2012-11-26 18:58:24 +00:00
}
/*
========================
idPlayerProfile::Serialize
========================
*/
bool idPlayerProfile::Serialize( idSerializer& ser )
{
2012-11-26 18:58:24 +00:00
// NOTE:
// See comments at top of file on versioning rules
2012-11-26 18:58:24 +00:00
// Default to current tag/version
int32 magicNumber = 0;
magicNumber += PROFILE_TAG << 16;
magicNumber += PROFILE_VER_MAJOR << 8;
magicNumber += PROFILE_VER_MINOR;
2012-11-26 18:58:24 +00:00
// Serialize version
ser.SerializePacked( magicNumber );
int16 tag = ( magicNumber >> 16 ) & 0xffff;
int8 majorVersion = ( magicNumber >> 8 ) & 0xff;
int8 minorVersion = magicNumber & 0xff;
minorVersion;
if( tag != PROFILE_TAG )
{
return false;
2012-11-26 18:58:24 +00:00
}
if( majorVersion != PROFILE_VER_MAJOR )
{
return false;
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// Archived cvars (all the menu settings for Doom3 are archived cvars)
idDict cvarDict;
cvarSystem->MoveCVarsToDict( CVAR_ARCHIVE, cvarDict );
cvarDict.Serialize( ser );
if( ser.IsReading() )
{
2012-11-26 18:58:24 +00:00
// Never sync these cvars with Steam because they require an engine or video restart
cvarDict.Delete( "r_fullscreen" );
cvarDict.Delete( "r_vidMode" );
cvarDict.Delete( "r_multisamples" );
cvarDict.Delete( "com_engineHz" );
cvarSystem->SetCVarsFromDict( cvarDict );
common->StartupVariable( NULL );
}
2012-11-26 18:58:24 +00:00
// The dlcReleaseVersion is used to determine that new content is available
ser.SerializePacked( dlcReleaseVersion );
2012-11-26 18:58:24 +00:00
// New setting to save to make sure that we have or haven't seen this achievement before used to pass TRC R149d
ser.Serialize( achievementBits );
ser.Serialize( achievementBits2 );
2012-11-26 18:58:24 +00:00
// Check to map sure we are on a valid map before we save, this helps prevent someone from creating a test map and
// gaining a bunch of achievements from it
int numStats = stats.Num();
ser.SerializePacked( numStats );
stats.SetNum( numStats );
for( int i = 0; i < numStats; ++i )
{
2012-11-26 18:58:24 +00:00
ser.SerializePacked( stats[i].i );
}
2012-11-26 18:58:24 +00:00
ser.Serialize( leftyFlip );
ser.Serialize( configSet );
if( ser.IsReading() )
{
2012-11-26 18:58:24 +00:00
// Which binding is used on the console?
ser.Serialize( customConfig );
2012-11-26 18:58:24 +00:00
ExecConfig( false );
if( customConfig )
{
for( int i = 0; i < K_LAST_KEY; ++i )
{
idStr bind;
2012-11-26 18:58:24 +00:00
ser.SerializeString( bind );
idKeyInput::SetBinding( i, bind.c_str() );
}
}
}
else
{
if( !customConfig )
{
2012-11-26 18:58:24 +00:00
ExecConfig( false );
}
2012-11-26 18:58:24 +00:00
customConfig = true;
ser.Serialize( customConfig );
for( int i = 0; i < K_LAST_KEY; ++i )
{
2012-11-26 18:58:24 +00:00
idStr bind = idKeyInput::GetBinding( i );
ser.SerializeString( bind );
}
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
========================
idPlayerProfile::StatSetInt
========================
*/
void idPlayerProfile::StatSetInt( int s, int v )
{
2012-11-26 18:58:24 +00:00
stats[s].i = v;
MarkDirty( true );
}
/*
========================
idPlayerProfile::StatSetFloat
========================
*/
void idPlayerProfile::StatSetFloat( int s, float v )
{
2012-11-26 18:58:24 +00:00
stats[s].f = v;
MarkDirty( true );
}
/*
========================
idPlayerProfile::StatGetInt
========================
*/
int idPlayerProfile::StatGetInt( int s ) const
{
2012-11-26 18:58:24 +00:00
return stats[s].i;
}
/*
========================
idPlayerProfile::StatGetFloat
========================
*/
float idPlayerProfile::StatGetFloat( int s ) const
{
2012-11-26 18:58:24 +00:00
return stats[s].f;
}
/*
========================
idPlayerProfile::SaveSettings
========================
*/
void idPlayerProfile::SaveSettings( bool forceDirty )
{
if( state != SAVING )
{
if( forceDirty )
{
2012-11-26 18:58:24 +00:00
MarkDirty( true );
}
if( GetRequestedState() == IDLE && IsDirty() )
{
2012-11-26 18:58:24 +00:00
SetRequestedState( SAVE_REQUESTED );
}
}
}
/*
========================
idPlayerProfile::SaveSettings
========================
*/
void idPlayerProfile::LoadSettings()
{
if( state != LOADING )
{
if( verify( GetRequestedState() == IDLE ) )
{
2012-11-26 18:58:24 +00:00
SetRequestedState( LOAD_REQUESTED );
}
}
}
/*
========================
idPlayerProfile::SetAchievement
========================
*/
void idPlayerProfile::SetAchievement( const int id )
{
if( id >= idAchievementSystem::MAX_ACHIEVEMENTS )
{
2012-11-26 18:58:24 +00:00
assert( false ); // FIXME: add another set of achievement bit flags
return;
}
2012-11-26 18:58:24 +00:00
uint64 mask = 0;
if( id < 64 )
{
2012-11-26 18:58:24 +00:00
mask = achievementBits;
achievementBits |= ( int64 )1 << id;
2012-11-26 18:58:24 +00:00
mask = ~mask & achievementBits;
}
else
{
2012-11-26 18:58:24 +00:00
mask = achievementBits2;
achievementBits2 |= ( int64 )1 << ( id - 64 );
2012-11-26 18:58:24 +00:00
mask = ~mask & achievementBits2;
}
2012-11-26 18:58:24 +00:00
// Mark the profile dirty if achievement bits changed
if( mask != 0 )
{
2012-11-26 18:58:24 +00:00
MarkDirty( true );
}
}
/*
========================
idPlayerProfile::ClearAchievement
========================
*/
void idPlayerProfile::ClearAchievement( const int id )
{
if( id >= idAchievementSystem::MAX_ACHIEVEMENTS )
{
2012-11-26 18:58:24 +00:00
assert( false ); // FIXME: add another set of achievement bit flags
return;
}
if( id < 64 )
{
achievementBits &= ~( ( int64 )1 << id );
2012-11-26 18:58:24 +00:00
}
else
{
achievementBits2 &= ~( ( int64 )1 << ( id - 64 ) );
}
2012-11-26 18:58:24 +00:00
MarkDirty( true );
}
/*
========================
idPlayerProfile::GetAchievement
========================
*/
bool idPlayerProfile::GetAchievement( const int id ) const
{
if( id >= idAchievementSystem::MAX_ACHIEVEMENTS )
{
2012-11-26 18:58:24 +00:00
assert( false ); // FIXME: add another set of achievement bit flags
return false;
}
if( id < 64 )
{
return ( achievementBits & ( int64 )1 << id ) != 0;
}
else
{
return ( achievementBits2 & ( int64 )1 << ( id - 64 ) ) != 0;
2012-11-26 18:58:24 +00:00
}
}
/*
========================
idPlayerProfile::SetConfig
========================
*/
void idPlayerProfile::SetConfig( int config, bool save )
{
2012-11-26 18:58:24 +00:00
configSet = config;
ExecConfig( save );
}
/*
========================
idPlayerProfile::SetConfig
========================
*/
void idPlayerProfile::RestoreDefault()
{
2012-11-26 18:58:24 +00:00
ExecConfig( true, true );
}
/*
========================
idPlayerProfile::SetLeftyFlip
========================
*/
void idPlayerProfile::SetLeftyFlip( bool lf )
{
2012-11-26 18:58:24 +00:00
leftyFlip = lf;
ExecConfig( true );
}
/*
========================
idPlayerProfile::ExecConfig
========================
*/
void idPlayerProfile::ExecConfig( bool save, bool forceDefault )
{
2012-11-26 18:58:24 +00:00
int flags = 0;
if( !save )
{
2012-11-26 18:58:24 +00:00
flags = cvarSystem->GetModifiedFlags();
}
if( !customConfig || forceDefault )
{
2012-11-26 18:58:24 +00:00
cmdSystem->AppendCommandText( "exec default.cfg\n" );
cmdSystem->AppendCommandText( "exec joy_360_0.cfg\n" );
}
if( leftyFlip )
{
2012-11-26 18:58:24 +00:00
cmdSystem->AppendCommandText( "exec joy_lefty.cfg" );
}
else
{
2012-11-26 18:58:24 +00:00
cmdSystem->AppendCommandText( "exec joy_righty.cfg" );
}
2012-11-26 18:58:24 +00:00
cmdSystem->ExecuteCommandBuffer();
if( !save )
{
2012-11-26 18:58:24 +00:00
cvarSystem->ClearModifiedFlags( CVAR_ARCHIVE );
cvarSystem->SetModifiedFlags( flags );
}
}
CONSOLE_COMMAND( setProfileDefaults, "sets profile settings to default and saves", 0 )
{
if( session->GetSignInManager().GetMasterLocalUser() == NULL )
{
2012-11-26 18:58:24 +00:00
return;
}
idPlayerProfile* profile = session->GetSignInManager().GetMasterLocalUser()->GetProfile();
if( verify( profile != NULL ) )
{
2012-11-26 18:58:24 +00:00
profile->SetDefaults();
profile->SaveSettings( true );
}
}