the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 217 lines 5.1 kB view raw
1 2 3 4#include "stdafx.h" 5#include "Orbis_PlayerUID.h" 6#include "..\Minecraft.World\StringHelpers.h" 7 8#include <libnetctl.h> 9 10 11// taken from Boost, there is no TR1 equivalent 12template <class T> 13inline void hash_combine(std::size_t& seed, T const& v) 14{ 15 hash<T> hasher; 16 seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); 17} 18 19std::size_t PlayerUID::Hash::operator()(const PlayerUID& k) const 20{ 21 // now only hashing against the local ID, as this is now unique across the network too. 22 std::size_t seed = 0; 23 hash_combine(seed, k.m_userID); 24 for(int i=0; i<SCE_NET_ETHER_ADDR_LEN; i++) 25 hash_combine(seed, k.m_macAddress[i]); 26 27 return seed; 28} 29 30PlayerUID::PlayerUID() 31{ 32 memset(this,0,sizeof(PlayerUID)); 33} 34 35 36PlayerUID::PlayerUID(SceUserServiceUserId userID, SceNpOnlineId& onlineID, bool bSignedInPSN, int quadrant) 37{ 38 m_userID = userID; 39 m_quadrant = quadrant; 40 setCurrentMacAddress(); 41 setOnlineID(onlineID, bSignedInPSN); 42} 43 44PlayerUID::PlayerUID(wstring fromString) 45{ 46 // create for the format P_5e7ff8372ea9_00000004_Mark_4J 47 48 if(fromString[0] == L'P') 49 m_quadrant = 0; // primary player, so make this zero 50 else 51 m_quadrant = 1; //'N' might not be quadrant 1, but doesn't matter here 52 53 // parse the mac address next 54 for(int i=0;i<6;i++) 55 { 56 wstring macDigit = fromString.substr(2+(i*2),2); 57 m_macAddress[i] = _fromHEXString<int>(macDigit); 58 } 59 60 int userIdLen = 0; 61 62 // parse the userID 63 wstring userIDDigits = L""; 64 65 while(fromString.at(15 + userIdLen) != L'_') 66 { 67 userIDDigits.push_back(fromString.at(15 + userIdLen)); 68 ++userIdLen; 69 } 70 fromString.substr(15,8); 71 m_userID = _fromString<int>(userIDDigits); 72 73 // finally, the onlineID, if there is one 74 wstring onlineID = fromString.substr(15 + userIdLen + 1); 75 if(onlineID.size() > 0) 76 { 77 wcstombs(m_onlineID, onlineID.c_str(), 16); 78 m_bSignedIntoPSN = true; 79 } 80 else 81 { 82 m_onlineID[0] = 0; 83 m_bSignedIntoPSN = false; 84 } 85} 86 87 88bool PlayerUID::operator==(const PlayerUID& rhs) const 89{ 90 // comparing online IDs 91 if(isSignedIntoPSN() && rhs.isSignedIntoPSN()) 92 { 93 return (strcmp(m_onlineID, rhs.m_onlineID) == 0); 94 } 95 // comparing offline IDs 96 if(m_userID != rhs.m_userID) 97 return false; 98 for(int i=0; i<SCE_NET_ETHER_ADDR_LEN;i++) 99 if(m_macAddress[i] != rhs.m_macAddress[i])return false; 100 return true; 101} 102 103bool PlayerUID::operator!=(const PlayerUID& rhs) 104{ 105 return !(*this==rhs); 106} 107 108void PlayerUID::setCurrentMacAddress() 109{ 110 // get the mac address of this machine 111 SceNetEtherAddr etherAddr; 112 int err = sceNetGetMacAddress(&etherAddr, 0); 113 assert(err == 0); 114 115 for(int i=0;i<SCE_NET_ETHER_ADDR_LEN; i++) 116 m_macAddress[i] = etherAddr.data[i]; 117} 118 119std::wstring g_testStringW; 120 121std::wstring PlayerUID::macAddressStr() const 122{ 123 wchar_t macAddr[16]; 124 const uint8_t* m = m_macAddress; 125 swprintf(macAddr, 16, L"%02x%02x%02x%02x%02x%02x", m[0],m[1],m[2],m[3],m[4],m[5]); 126 g_testStringW = std::wstring(macAddr); 127 return g_testStringW; 128} 129 130std::wstring PlayerUID::userIDStr() const 131{ 132 char finalString[16]; 133 wchar_t finalStringW[16]; 134 sprintf(finalString, "%08d", m_userID); 135 mbstowcs(finalStringW, finalString, 64); 136 std::wstring retVal = finalStringW; 137 return retVal; 138} 139 140std::wstring PlayerUID::toString() const 141{ 142 char macAddr[16]; 143 char finalString[64]; 144 wchar_t finalStringW[64]; 145 146 const uint8_t* m = m_macAddress; 147 sprintf(macAddr, "%02x%02x%02x%02x%02x%02x", m[0],m[1],m[2],m[3],m[4],m[5]); 148 sprintf(finalString, "%s_%s_%08d_%s", isPrimaryUser() ? "P" : "N", 149 macAddr, m_userID, 150 isSignedIntoPSN() ? m_onlineID : ""); 151 mbstowcs(finalStringW, finalString, 64); 152 return std::wstring(finalStringW); 153} 154void PlayerUID::setOnlineID(SceNpOnlineId& id, bool bSignedIntoPSN) 155{ 156 memcpy(m_onlineID, id.data, SCE_NP_ONLINEID_MAX_LENGTH); 157 term = id.term; 158 m_bSignedIntoPSN = bSignedIntoPSN; 159} 160void PlayerUID::setUserID(unsigned int id) { m_userID = id; } 161 162bool PlayerUID::isPrimaryUser() const /* only true if we're on the local machine and signed into the first quadrant */ 163{ 164 if(m_quadrant != 0) 165 return false; 166 167 // get the mac address of this machine 168 SceNetEtherAddr etherAddr; 169 int err = sceNetGetMacAddress(&etherAddr, 0); 170 assert(err == 0); 171 bool macIsLocal = true; 172 for(int i=0;i<SCE_NET_ETHER_ADDR_LEN; i++) 173 { 174 if(m_macAddress[i] != etherAddr.data[i]) 175 macIsLocal = false; 176 } 177 return macIsLocal; // only true if we're on the local machine and signed into the first quadrant 178} 179 180 181 182 183 184GameSessionUID::GameSessionUID() 185{ 186 memset(this,0,sizeof(GameSessionUID)); 187} 188GameSessionUID::GameSessionUID(int nullVal) 189{ 190 assert(nullVal == 0); 191 memset(this,0,sizeof(GameSessionUID)); 192} 193 194bool GameSessionUID::operator==(const GameSessionUID& rhs) const 195{ 196 // comparing online IDs 197 if( getQuadrant() != rhs.getQuadrant() ) 198 { 199 return false; 200 } 201 return (strcmp(m_onlineID, rhs.m_onlineID) == 0); 202} 203bool GameSessionUID::operator!=(const GameSessionUID& rhs) 204{ 205 return !(*this==rhs); 206} 207 208 209GameSessionUID& GameSessionUID::operator=(const PlayerUID& rhs) 210{ 211 memcpy(m_onlineID, rhs.getOnlineID(), SCE_NP_ONLINEID_MAX_LENGTH); 212 term = 0; 213 m_quadrant = rhs.getQuadrant(); 214 m_bSignedIntoPSN = rhs.isSignedIntoPSN(); 215 return *this; 216} 217