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