int CScriptBind_GameToken::GetToken( IFunctionHandler *pH,const char *sTokenName )
{
	IGameToken *pToken = m_pTokenSystem->FindToken(sTokenName);
	if (!pToken)
	{
		GameWarning("[GameToken.GetToken] Cannot find token '%s'", sTokenName);
		return pH->EndFunction();
	}

	return pH->EndFunction( pToken->GetValueAsString() );
}
Пример #2
0
string CTweakMetadataGameToken::GetValue(void)
{
	string result = "Not found";

	IGameToken* pGameToken = GetGameToken();
	if (pGameToken) 
	{
		// Should we treat this as a bool?
		if (eFDT_Bool == pGameToken->GetType() )
		{
			bool value = false;
			pGameToken->GetValueAs(value);
			result = value ? "True" : "False";
		}
		else
		{
			result = pGameToken->GetValueAsString();
		}
	}
	return result;
}
int CScriptBind_GameToken::SetToken( IFunctionHandler *pH)
{
	SCRIPT_CHECK_PARAMETERS(2);
	const char* tokenName = 0;
	if (pH->GetParams(tokenName) == false)
	{
		GameWarning("[GameToken.SetToken] Usage: GameToken.SetToken TokenName TokenValue]");
		return pH->EndFunction();
	}
	ScriptAnyValue val;
	TFlowInputData data;

	if (pH->GetParamAny( 2, val) == false)
	{
		GameWarning("[GameToken.SetToken(%s)] Usage: GameToken.SetToken TokenName TokenValue]", tokenName);
		return pH->EndFunction();
	}
	switch (val.type)
	{
	case ANY_TBOOLEAN:
		{ 
			bool v; 
			val.CopyTo(v); 	
			data.Set(v);
		}
		break;
	case ANY_TNUMBER:
		{ 
			float v; 
			val.CopyTo(v); 		
			data.Set(v);
		}
		break;
	case ANY_TSTRING:
		{ 
			const char* v;
			val.CopyTo(v); 		
			data.Set(string(v));
		}
		break;
	case ANY_TVECTOR:
		{ 
			Vec3 v; 
			val.CopyTo(v); 		
			data.Set(v);
		}
	case ANY_TTABLE:
		{
			float x,y,z;
			IScriptTable * pTable = val.table;
			assert (pTable != 0);
			if (pTable->GetValue( "x", x ) && pTable->GetValue( "y", y ) && pTable->GetValue( "z", z ))
			{
				data.Set(Vec3(x,y,z));
			}
			else
			{
				GameWarning("[GameToken.SetToken(%s)] Cannot convert parameter type '%s' to Vec3", tokenName, ScriptAnyTypeToString(val.type));
				return pH->EndFunction();
			}
		}
		break;
	case ANY_THANDLE:
		{
			ScriptHandle handle;
			val.CopyTo(handle);
			data.Set((EntityId)handle.n);
		}
		break;
	default:
		GameWarning("[GameToken.SetToken(%s)] Cannot convert parameter type '%s'", tokenName, ScriptAnyTypeToString(val.type));
		return pH->EndFunction();
		break; // dummy ;-)
	}
#ifdef SCRIPT_GAMETOKEN_ALWAYS_CREATE
	m_pTokenSystem->SetOrCreateToken(tokenName, data);
#else
	IGameToken *pToken = m_pTokenSystem->FindToken(tokenName);
	if (!pToken)
		GameWarning("[GameToken.SetToken] Cannot find token '%s'", tokenName);
	else
	{
		pToken->SetValue(data);
	}
#endif
	return pH->EndFunction();
}
Пример #4
0
bool CTweakMetadataGameToken::ChangeValue(bool bIncrement)
{
	// Get delta
	double fDelta = m_fDelta;
	if (!bIncrement)
		fDelta *= -1.0f;

	// Get and check CVAR
	IGameToken* pGameToken = GetGameToken();
	if(!pGameToken)
		return false;

	string sValue;

	// Deal with appropriate type
	switch (pGameToken->GetType())
	{
	case eFDT_Bool:
		{
			bool value = false;
			pGameToken->GetValueAs(value);
			TFlowInputData newValue;
			newValue.SetValueWithConversion(!value);
			pGameToken->SetValue(newValue);
		}
		break;
	case eFDT_Int:
		{
			int value = 0;
			pGameToken->GetValueAs(value);
			TFlowInputData newValue;
			newValue.SetValueWithConversion((int)ClampToLimits(value + fDelta));
			pGameToken->SetValue(newValue);
		}
		break;
	case eFDT_Float:
		{
			float value = 0.0f;
			pGameToken->GetValueAs(value);
			TFlowInputData newValue;
			newValue.SetValueWithConversion((float)ClampToLimits(value + fDelta));
			pGameToken->SetValue(newValue);
		}
		break;
	case eFDT_String:
		{
			// bools pretending to be strings
			string value = pGameToken->GetValueAsString();
			if(!value.compareNoCase("true"))
				pGameToken->SetValueAsString("false");
			else if(!value.compareNoCase("false"))
				pGameToken->SetValueAsString("true");

			// other strings are non-obvious
		}
		break;
	default:
		break;
	}

	return true;
}
bool CGameTokenSystem::_InternalLoadLibrary( const char *filename, const char* tag )
{
	XmlNodeRef root = GetISystem()->LoadXmlFromFile( filename );
	if (!root)
	{
		GameWarning( _HELP("Unable to load game token database: %s"), filename );
		return false;
	}

	if (0 != strcmp( tag, root->getTag() ))
	{
		GameWarning( _HELP("Not a game tokens library : %s"), filename );
		return false;
	}

	// GameTokens are (currently) not saved with their full path
	// we expand it here to LibName.TokenName
	string libName;
	{
		const char *sLibName = root->getAttr("Name");
		if (sLibName == 0) {
			GameWarning( "GameTokensLibrary::LoadLibrary: Unable to find LibName in file '%s'", filename);
			return false;
		}
		libName = sLibName;
	}
 
	// we dont skip already loaded libraries anymore. We need to reload them to be sure that all necessary gametokens are present even if some level has not up-to-date libraries.
	if (!stl::find(m_libraries, libName)) //return true;
		m_libraries.push_back(libName);

	libName+= ".";

	int numChildren = root->getChildCount();
	for (int i=0; i<numChildren; i++)
	{
		XmlNodeRef node = root->getChild(i);

		const char *sName = node->getAttr("Name");
		const char *sType = node->getAttr("Type");
		const char *sValue = node->getAttr("Value");
		const char *sLocalOnly = node->getAttr("LocalOnly");
		int localOnly=atoi(sLocalOnly);

		EFlowDataTypes tokenType = eFDT_Any;
		if (0 == strcmp(sType,"Int"))
			tokenType = eFDT_Int;
		else if (0 == strcmp(sType,"Float"))
			tokenType = eFDT_Float;
		else if (0 == strcmp(sType,"EntityId"))
			tokenType = eFDT_EntityId;
		else if (0 == strcmp(sType,"Vec3"))
			tokenType = eFDT_Vec3;
		else if (0 == strcmp(sType,"String"))
			tokenType = eFDT_String;
		else if (0 == strcmp(sType,"Bool"))
			tokenType = eFDT_Bool;

		if (tokenType == eFDT_Any)
		{
			GameWarning(_HELP("Unknown game token type %s in token %s (%s:%d)"),sType,sName,node->getTag(),node->getLine());
			continue;
		}

		TFlowInputData initialValue = TFlowInputData(string(sValue));

		string fullName (libName);
		fullName+=sName;
		
		IGameToken *pToken = stl::find_in_map(*m_pGameTokensMap,fullName,NULL);
		if (!pToken)
		{
			pToken = SetOrCreateToken( fullName,initialValue );
			if (pToken && localOnly)
				pToken->SetFlags(pToken->GetFlags()|EGAME_TOKEN_LOCALONLY);
		}
	}
	return true;
}