SPAuthCheckPrv::SPAuthCheckPrv(const char *theProduct)
{
	mAPI = NULL;
	mStatus = WS_None;
	mStartedChecking = false;
	mOnlyForceCheck = false;
	mStartTime = time(NULL);
	mGameSecondsBeforeNextCheck = 1;

	WriteBuffer aBuf;
	aBuf.Reserve(8);
	aBuf.AppendLong(0x87ab3215);
	aBuf.AppendLong(CDKey::GetMachineId());
	mEncryptKey.SetKey(aBuf.data(),aBuf.length());
	
	mAuthContext = new AuthContext;
	mCommunity = StringToWString(theProduct);
	mProductDir = L"/" + mCommunity;

	mCDKey.SetProductString(theProduct);
	mCDKey.LoadFromRegistry();
	if(!mCDKey.IsValid())
	{
		mStatus = WS_AuthServ_InvalidCDKey;
		return;
	}
		
	if(!AsyncSocket::HasInternetConnection())
	{
		mStatus = WS_NoInternetConnection;
		return;
	}

	if(WONAPICoreEx::GetInstance()==NULL)
	{
		mAPI = new WONAPICoreEx;
		mAPI->SetDoPumpThread(true);
		mAPI->Startup();
	}	

	ReadCheckFile();
}
Esempio n. 2
0
Status SavedGames::Save(const std::wstring& prefix, CSimulation2& simulation, CGUIManager* gui, int playerID)
{
	// Determine the filename to save under
	const VfsPath basenameFormat(L"saves/" + prefix + L"-%04d");
	const VfsPath filenameFormat = basenameFormat.ChangeExtension(L".0adsave");
	VfsPath filename;

	// Don't make this a static global like NextNumberedFilename expects, because
	// that wouldn't work when 'prefix' changes, and because it's not thread-safe
	size_t nextSaveNumber = 0;
	vfs::NextNumberedFilename(g_VFS, filenameFormat, nextSaveNumber, filename);

	// ArchiveWriter_Zip can only write to OsPaths, not VfsPaths,
	// but we'd like to handle saved games via VFS.
	// To avoid potential confusion from writing with non-VFS then
	// reading the same file with VFS, we'll just write to a temporary
	// non-VFS path and then load and save again via VFS,
	// which is kind of a hack.

	OsPath tempSaveFileRealPath;
	WARN_RETURN_STATUS_IF_ERR(g_VFS->GetDirectoryRealPath("cache/", tempSaveFileRealPath));
	tempSaveFileRealPath = tempSaveFileRealPath / "temp.0adsave";

	time_t now = time(NULL);

	// Construct the serialized state to be saved

	std::stringstream simStateStream;
	if (!simulation.SerializeState(simStateStream))
		WARN_RETURN(ERR::FAIL);

	CScriptValRooted metadata;
	simulation.GetScriptInterface().Eval("({})", metadata);
	simulation.GetScriptInterface().SetProperty(metadata.get(), "version_major", SAVED_GAME_VERSION_MAJOR);
	simulation.GetScriptInterface().SetProperty(metadata.get(), "version_minor", SAVED_GAME_VERSION_MINOR);
	simulation.GetScriptInterface().SetProperty(metadata.get(), "time", (double)now);
	simulation.GetScriptInterface().SetProperty(metadata.get(), "player", playerID);
	simulation.GetScriptInterface().SetProperty(metadata.get(), "initAttributes", simulation.GetInitAttributes());
	if (gui)
	{
		CScriptVal guiMetadata = simulation.GetScriptInterface().CloneValueFromOtherContext(gui->GetScriptInterface(), gui->GetSavedGameData().get());
		simulation.GetScriptInterface().SetProperty(metadata.get(), "gui", guiMetadata);
	}
	
	std::string metadataString = simulation.GetScriptInterface().StringifyJSON(metadata.get(), true);
	
	// Write the saved game as zip file containing the various components
	PIArchiveWriter archiveWriter = CreateArchiveWriter_Zip(tempSaveFileRealPath, false);
	if (!archiveWriter)
		WARN_RETURN(ERR::FAIL);

	WARN_RETURN_STATUS_IF_ERR(archiveWriter->AddMemory((const u8*)metadataString.c_str(), metadataString.length(), now, "metadata.json"));
	WARN_RETURN_STATUS_IF_ERR(archiveWriter->AddMemory((const u8*)simStateStream.str().c_str(), simStateStream.str().length(), now, "simulation.dat"));
	archiveWriter.reset(); // close the file

	WriteBuffer buffer;
	FileInfo tempSaveFile;
	WARN_RETURN_STATUS_IF_ERR(GetFileInfo(tempSaveFileRealPath, &tempSaveFile));
	buffer.Reserve(tempSaveFile.Size());
	WARN_RETURN_STATUS_IF_ERR(io::Load(tempSaveFileRealPath, buffer.Data().get(), buffer.Size()));
	WARN_RETURN_STATUS_IF_ERR(g_VFS->CreateFile(filename, buffer.Data(), buffer.Size()));

	OsPath realPath;
	WARN_RETURN_STATUS_IF_ERR(g_VFS->GetRealPath(filename, realPath));
	LOGMESSAGERENDER(L"Saved game to %ls\n", realPath.string().c_str());

	return INFO::OK;
}
Esempio n. 3
0
ByteBufferPtr ElGamal::Encrypt(const void *thePlainText, int thePlainTextLen) const
{
    if(!IsPublic())
        return NULL;

    const unsigned char *aPlainText = (const unsigned char*)thePlainText;

    int aBlockLen = modulusLen - 3;
    int aNumBlock = thePlainTextLen / aBlockLen;
    if ((thePlainTextLen % aBlockLen) != 0) aNumBlock++;

    WriteBuffer anEncrypt;
    anEncrypt.Reserve(4+modulusLen*2*aNumBlock);

    int anOffset = 0;

    anEncrypt.AppendLong(aNumBlock);

    while(anOffset < thePlainTextLen)
    {
        int thisBlockLen = aBlockLen;

        if(thePlainTextLen - anOffset < aBlockLen)
            thisBlockLen = thePlainTextLen - anOffset;

        RawBuffer anEncryptBlock(modulusLen-1,(unsigned char)0);

        for(int k=0,j=modulusLen-2-thisBlockLen; j<modulusLen-2; j++,k++)
            anEncryptBlock[j] = aPlainText[anOffset+k];

        anEncryptBlock[modulusLen - 2] = (unsigned char)thisBlockLen;


        BigInteger ab[2];

        if(!encrypt(BigInteger(anEncryptBlock),ab))
            return NULL;


        RawBuffer aa,bb;
        ab[0].toBinary(aa);
        ab[1].toBinary(bb);

        if(aa.length()==modulusLen)
            anEncrypt.AppendBytes(aa.data(),aa.length());
        else if(aa.length()>modulusLen)
            anEncrypt.AppendBytes(aa.data()+aa.length()-modulusLen,modulusLen);
        else
        {
            for(int i=aa.length(); i<modulusLen; i++)
                anEncrypt.AppendByte(0);

            anEncrypt.AppendBytes(aa.data(),aa.length());
        }


        if(bb.length()==modulusLen)
            anEncrypt.AppendBytes(bb.data(),bb.length());
        else if(bb.length()>modulusLen)
            anEncrypt.AppendBytes(bb.data()+bb.length()-modulusLen,modulusLen);
        else
        {
            for(int i=bb.length(); i<modulusLen; i++)
                anEncrypt.AppendByte(0);

            anEncrypt.AppendBytes(bb.data(),bb.length());
        }
        anOffset+=thisBlockLen;
    }

    return anEncrypt.ToByteBuffer();
}