aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PSVita/PSVita_PlayerUID.cpp
blob: a678d0a07e242049b9137076270b5ae4738e19a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "stdafx.h"
#include "PSVita_PlayerUID.h"
#include "..\Minecraft.World\StringHelpers.h"

// taken from Boost, there is no TR1 equivalent
template <class T>
inline void hash_combine(std::size_t& seed, T const& v)
{
	hash<T> hasher;
	seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}

std::size_t PlayerUID::Hash::operator()(const PlayerUID& k) const
{
	//  now only hashing against the local ID, as this is now unique across the network too.
	std::size_t seed = 0;
	hash_combine(seed, k.m_userID);
	for(int i=0; i<SCE_NET_ETHER_ADDR_LEN; i++)
		hash_combine(seed, k.m_macAddress[i]);

	return seed;
}

PlayerUID::PlayerUID()
{
	memset(this,0,sizeof(PlayerUID));
} 


PlayerUID::PlayerUID(int userID, SceNpOnlineId& onlineID, bool bSignedInPSN, int quadrant)
{
	m_userID = userID;
	m_quadrant = quadrant;
	setCurrentMacAddress();
	setOnlineID(onlineID, bSignedInPSN);
} 

PlayerUID::PlayerUID(wstring fromString)
{
	// create for the format 	P_5e7ff8372ea9_00000004_Mark_4J

	if(fromString[0] == L'P')
		m_quadrant = 0;	// primary player, so make this zero
	else
		m_quadrant = 1; //'N' might not be quadrant 1, but doesn't matter here

	// parse the mac address next
	for(int i=0;i<6;i++)
	{
		wstring macDigit = fromString.substr(2+(i*2),2);
		m_macAddress[i] = _fromHEXString<int>(macDigit);
	}

	// parse the userID
	wstring userIDDigits = fromString.substr(15,8);
	m_userID = _fromString<int>(userIDDigits);

	// finally, the onlineID, if there is one
	wstring onlineID = fromString.substr(24);
	if(onlineID.size() > 0)
	{
		wcstombs(m_onlineID, onlineID.c_str(), 16);
		m_bSignedIntoPSN = true;
	}
	else
	{
		m_onlineID[0] = 0;
		m_bSignedIntoPSN = false;
	}
}


bool PlayerUID::operator==(const PlayerUID& rhs) const
{
	// comparing online IDs	
	if(isSignedIntoPSN() && rhs.isSignedIntoPSN())
	{
		return (strcmp(m_onlineID, rhs.m_onlineID) == 0);
	}
	// comparing offline IDs	
	if(m_userID != rhs.m_userID)
		return false;
	for(int i=0; i<SCE_NET_ETHER_ADDR_LEN;i++)
		if(m_macAddress[i] != rhs.m_macAddress[i])return false;
	return true;
}

bool PlayerUID::operator!=(const PlayerUID& rhs)
{
	return !(*this==rhs);
}

void PlayerUID::setCurrentMacAddress()
{
	// get the mac address of this machine
	SceNetEtherAddr etherAddr;
	int err = sceNetGetMacAddress(&etherAddr, 0);
	assert(err == 0);

	for(int i=0;i<SCE_NET_ETHER_ADDR_LEN; i++)
		m_macAddress[i] = etherAddr.data[i];
}

std::wstring g_testStringW;

std::wstring PlayerUID::macAddressStr() const
{
	wchar_t macAddr[16];
	const uint8_t* m = m_macAddress;
	swprintf(macAddr, 16, L"%02x%02x%02x%02x%02x%02x", m[0],m[1],m[2],m[3],m[4],m[5]);
	g_testStringW = std::wstring(macAddr);
	return g_testStringW;
}

std::wstring PlayerUID::userIDStr() const
{
	char finalString[16];
	wchar_t finalStringW[16];
	sprintf(finalString, "%08d", m_userID);
	mbstowcs(finalStringW, finalString, 64);
	std::wstring retVal = finalStringW;
	return retVal;
}

std::wstring PlayerUID::toString() const
{
	char macAddr[16];
	char finalString[64];
	wchar_t finalStringW[64];

	const uint8_t* m = m_macAddress;
	sprintf(macAddr, "%02x%02x%02x%02x%02x%02x", m[0],m[1],m[2],m[3],m[4],m[5]);
	sprintf(finalString, "%s_%s_%08d_%s",	isPrimaryUser() ? "P" : "N", 
		macAddr, m_userID, 
		isSignedIntoPSN() ? m_onlineID : "");
	mbstowcs(finalStringW, finalString, 64);
	return std::wstring(finalStringW);
}

void PlayerUID::setOnlineID(SceNpOnlineId& id, bool bSignedIntoPSN)
{
	memcpy(m_onlineID, id.data, SCE_NP_ONLINEID_MAX_LENGTH);
	term = id.term;
	m_bSignedIntoPSN = bSignedIntoPSN;
}

void PlayerUID::setUserID(unsigned int id) { m_userID = id; }

bool PlayerUID::isPrimaryUser() const /* only true if we're on the local machine and signed into the first quadrant */
{
	if(m_quadrant != 0)
		return false;

	// get the mac address of this machine
	SceNetEtherAddr etherAddr;
	int err = sceNetGetMacAddress(&etherAddr, 0);
	assert(err == 0);
	bool macIsLocal = true;
	for(int i=0;i<SCE_NET_ETHER_ADDR_LEN; i++)
	{
		if(m_macAddress[i] != etherAddr.data[i])
			macIsLocal = false;
	}
	return macIsLocal;		// only true if we're on the local machine and signed into the first quadrant
}

void PlayerUID::setForAdhoc()
{
	m_bSignedIntoPSN = false;
	m_quadrant = 0;
	memcpy(m_onlineID, ProfileManager.GetGamertag(0), SCE_NP_ONLINEID_MAX_LENGTH);
	setCurrentMacAddress();
}


GameSessionUID::GameSessionUID()
{
	memset(this,0,sizeof(GameSessionUID));
} 

GameSessionUID::GameSessionUID(int nullVal)
{
	assert(nullVal == 0);
	memset(this,0,sizeof(GameSessionUID));
}  

bool GameSessionUID::operator==(const GameSessionUID& rhs) const
{
	// comparing online IDs	
	if( getQuadrant() != rhs.getQuadrant() )
	{
		return false;
	}
	return (strcmp(m_onlineID, rhs.m_onlineID) == 0);
}

bool GameSessionUID::operator!=(const GameSessionUID& rhs)
{
	return !(*this==rhs);
}


GameSessionUID& GameSessionUID::operator=(const PlayerUID& rhs)
{
	memcpy(m_onlineID, rhs.getOnlineID(), SCE_NP_ONLINEID_MAX_LENGTH);
	term = 0;
	m_quadrant = rhs.getQuadrant();
	m_bSignedIntoPSN = rhs.isSignedIntoPSN();
	return *this;
}

void GameSessionUID::setForAdhoc()
{
	m_bSignedIntoPSN = false;
	m_quadrant = 0;
	memcpy(m_onlineID, ProfileManager.GetGamertag(0), SCE_NP_ONLINEID_MAX_LENGTH);
}