UE4读取steam sessionsetting中Value是亚洲文字时 乱码/ UE4 fix asian character garbled when get sessionsetting

使用的UE4版本:4.26-chaos源码版


在UE4使用steam在线子系统时,在查询到SessonResult时,读取SessionSettings设置的Value如果是亚洲文字的话,读取是乱码

xxxInstance.cpp

...
    FOnlineSessionSetting ExtraSetting;
    // 设置settings键值
	ExtraSetting.Data = *HostName.ToString();
	ExtraSetting.AdvertisementType = EOnlineDataAdvertisementType::ViaOnlineService;
	SessionSettings.Settings.Add(FName("SERVER_NAME"), ExtraSetting);
...
    // 查询到某个SessionResult, 获取settings的键值
    FString serverNameString;
	OnlineSearchResult.Session.SessionSettings.Get(FName("SERVER_NAME"), serverNameString);

这样的serverNameString显示的都是 ???

原因是在源码中SetValue时FString转的是ANSI;

源码位置在 Engine\Plugins\Online\OnlineSubsystemSteam\Source\Private\SteamSessionKeys.h

...

case EOnlineKeyValuePairDataType::String:
		KeyStr = FString::Printf(TEXT("%s_s"), *Key.ToString());
		break;

...

inline bool SteamKeyToSessionSetting(const TCHAR* SteamKey, const ANSICHAR* SteamValue, FName& Key, FOnlineSessionSetting& Setting)
{
	bool bSuccess = false;

	TCHAR SteamKeyCopy[1024];

	FCString::Strncpy(SteamKeyCopy, SteamKey, UE_ARRAY_COUNT(SteamKeyCopy));

	TCHAR* DataType = FCString::Strrchr(SteamKeyCopy, '_');
	if (DataType)
	{
		bSuccess = true;

		// NULL Terminate the key
		*DataType = '\0';
		Key = FName(SteamKeyCopy);

		// Advance to the data type
		DataType += 1;

		switch(DataType[0])
		{
		case 'i':
			Setting.Data.SetValue((int32)0);
			Setting.Data.FromString(ANSI_TO_TCHAR(SteamValue));
			break;
		case 'l':
			Setting.Data.SetValue((uint64)0);
			Setting.Data.FromString(ANSI_TO_TCHAR(SteamValue));
			break;
		case 'd':
			Setting.Data.SetValue((double)0);
			Setting.Data.FromString(ANSI_TO_TCHAR(SteamValue));
			break;
		case 's':
			Setting.Data.SetValue(ANSI_TO_TCHAR(SteamValue));  // 这里的问题
            //改成Setting.Data.SetValue(UTF8_TO_TCHAR(SteamValue)); 
			break;
		case 'f':
			Setting.Data.SetValue((float)0);
			Setting.Data.FromString(ANSI_TO_TCHAR(SteamValue));
			break;
		case 'b':
			Setting.Data.SetValue(false);
			Setting.Data.FromString(ANSI_TO_TCHAR(SteamValue));
			break;
		default:
			bSuccess = false;
		}
	}
	
	if (!bSuccess)
	{
		UE_LOG_ONLINE_SESSION(Warning, TEXT("Unknown or unsupported data type from Steam key data %s %s"), SteamKey, ANSI_TO_TCHAR(SteamValue));
	}

	return bSuccess;
}

改一个地方

把 Setting.Data.SetValue(ANSI_TO_TCHAR(SteamValue)); 改成 Setting.Data.SetValue(UTF8_TO_TCHAR(SteamValue))

然后把ue4editor关掉;

再把引擎源码build一下;

再compile运行,亚洲文字就能显示了

UE4读取steam sessionsetting中Value是亚洲文字时 乱码/ UE4 fix asian character garbled when get sessionsetting_第1张图片

UE4读取steam sessionsetting中Value是亚洲文字时 乱码/ UE4 fix asian character garbled when get sessionsetting_第2张图片

你可能感兴趣的:(问题,unreal,engine,unreal4,unreal)