Exemplo n.º 1
0
bool PLH::X86Detour::Hook()
{
	DWORD OldProtection;

	m_hkLength = CalculateLength(m_hkSrc, 5);
	m_OriginalLength = m_hkLength;
	if (m_hkLength == 0)
	{
		XTrace("Function to small to hook\n");
		return false;
	}

	m_Trampoline = new BYTE[m_hkLength + 30];   //Allocate Space for original plus extra to jump back and for jmp table
	VirtualProtect(m_Trampoline, m_hkLength + 5, PAGE_EXECUTE_READWRITE, &OldProtection); //Allow Execution
	m_NeedFree = true;

	memcpy(m_OriginalCode, m_hkSrc, m_hkLength);
	memcpy(m_Trampoline, m_hkSrc, m_hkLength); //Copy original into allocated space
	RelocateASM(m_Trampoline, m_hkLength, (DWORD)m_hkSrc, (DWORD)m_Trampoline);
	WriteRelativeJMP((DWORD)&m_Trampoline[m_hkLength], (DWORD)m_hkSrc + m_hkLength); //JMP back to original code

	//Change protection to allow write on original function
	MemoryProtect Protector(m_hkSrc, m_hkLength, PAGE_EXECUTE_READWRITE);
	//Encode Jump from Hooked Function to the Destination function
	WriteRelativeJMP((DWORD)m_hkSrc, (DWORD)m_hkDest);

	//Write nops over bytes of overwritten instructions
	for (int i = 5; i < m_OriginalLength; i++)
		m_hkSrc[i] = 0x90;
	FlushSrcInsCache();
	//Revert to old protection on original function
	VirtualProtect(m_hkSrc, m_hkLength, OldProtection, &OldProtection);
	PostError(RuntimeError(RuntimeError::Severity::Warning, "PolyHook x86Detour: Some opcodes may not be relocated properly"));
	return true;
	/*Original
	-JMP Destination
	-NOP (extends to length of overwritten opcode)
	-Rest of function

	Destination
	-Do your shit
	-Return Trampoline (goes to trampoline)

	Trampoline
	-Execute Overwritten Opcodes
	-Patch original relative jmps to point to jump table (JE Jumptable entry 1)
	-JMP to rest of function (in original)
	-*BEGIN JUMPTABLE*
	-1)JMP to location of relative jmp one
	-2)...continue pattern for all relative jmps
	*/
}
void VsSpring::Physics_CollectData()
{
	if(m_vxSpring)
	{
		m_fltLength = CalculateLength();
		if(m_bEnabled)
			m_fltDisplacement = (m_fltLength - m_fltNaturalLengthNotScaled);
		else
			m_fltDisplacement = 0;

		m_fltTension = m_fltStiffnessNotScaled * m_fltDisplacement;
		m_fltEnergy = 0.5f*m_fltStiffnessNotScaled*m_fltDisplacement*m_fltDisplacement;
	}
}
Exemplo n.º 3
0
void SplinePath::AddControlPoint(Node* point, unsigned index)
{
    if (!point)
        return;

    WeakPtr<Node> controlPoint(point);

    point->AddListener(this);
    controlPoints_.Insert(index, controlPoint);
    spline_.AddKnot(point->GetWorldPosition(), index);

    UpdateNodeIds();
    CalculateLength();
}
Exemplo n.º 4
0
//依次从容器中取出两点,计算两点间的距离,
//计算出需要插入的点到两个点之间的距离,
//如果两个距离与两点之间的距离相等,将插入点插入到这两点之间
unsigned int CStakeInfo::InserteLinePoint(const DCoord& coord, const std::vector<DCoord>& vecCoords, std::vector<DCoord>& newCoords)
{
	unsigned int nPos = 0;
	newCoords.assign(vecCoords.begin(), vecCoords.end());
	for (auto itCoords = newCoords.cbegin(); (itCoords + 1) != newCoords.cend(); ++itCoords)
	{
		const DCoord& coordA = *itCoords;
		const DCoord& coordB = *(itCoords + 1);
		double dbLenA = CalculateLength(coord.Lon, coord.Lat, coordA.Lon, coordA.Lat);
		double dbLenB = CalculateLength(coord.Lon, coord.Lat, coordB.Lon, coordB.Lat);
		double dbLenAB = CalculateLength(coordA.Lon, coordA.Lat, coordB.Lon, coordB.Lat);
		if (dbLenAB + 0.00005 >= (dbLenA + dbLenB))
		{
			++nPos;
			newCoords.insert(itCoords + 1, coord);
			return nPos;
		}
		
		++nPos;
	}

	return -1;
}
Exemplo n.º 5
0
void SplinePath::ClearControlPoints()
{
    for (unsigned i = 0; i < controlPoints_.Size(); ++i)
    {
        Node* node = controlPoints_[i];
        if (node)
            node->RemoveListener(this);
    }

    controlPoints_.Clear();
    spline_.Clear();

    UpdateNodeIds();
    CalculateLength();
}
Exemplo n.º 6
0
void SplinePath::ApplyAttributes()
{
    if (!dirty_)
        return;

    // Remove all old instance nodes before searching for new. Can not call RemoveAllInstances() as that would modify
    // the ID list on its own
    for (unsigned i = 0; i < controlPoints_.Size(); ++i)
    {
        Node* node = controlPoints_[i];
        if (node)
            node->RemoveListener(this);
    }

    controlPoints_.Clear();
    spline_.Clear();

    Scene* scene = GetScene();

    if (scene)
    {
        // The first index stores the number of IDs redundantly. This is for editing
        for (unsigned i = 1; i < controlPointIdsAttr_.Size(); ++i)
        {
            Node* node = scene->GetNode(controlPointIdsAttr_[i].GetUInt());
            if (node)
            {
                WeakPtr<Node> controlPoint(node);
                node->AddListener(this);
                controlPoints_.Push(controlPoint);
                spline_.AddKnot(node->GetWorldPosition());
            }
        }

        Node* node = scene->GetNode(controlledIdAttr_);
        if (node)
        {
            WeakPtr<Node> controlled(node);
            controlledNode_ = controlled;
        }
    }

    CalculateLength();
    dirty_ = false;
}
Exemplo n.º 7
0
void SplinePath::OnMarkedDirty(Node* point)
{
    if (!point)
        return;

    WeakPtr<Node> controlPoint(point);

    for (unsigned i = 0; i < controlPoints_.Size(); ++i)
    {
        if (controlPoints_[i] == controlPoint)
        {
            spline_.SetKnot(point->GetWorldPosition(), i);
            break;
        }
    }

    CalculateLength();
}
Exemplo n.º 8
0
	VFTableHook(PPDWORD ppClass, bool bReplace) {
		m_ppClassBase = ppClass;
		m_bReplace = bReplace;
		if (bReplace) {
			m_pOriginalVMTable = *ppClass;
			uint32_t dwLength = CalculateLength();

			m_pNewVMTable = new DWORD[dwLength];
			memcpy(m_pNewVMTable, m_pOriginalVMTable, dwLength * sizeof(DWORD));

			DWORD old;
			VirtualProtect(m_ppClassBase, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &old);
			*m_ppClassBase = m_pNewVMTable;
			VirtualProtect(m_ppClassBase, sizeof(DWORD), old, &old);

		}
		else {
			m_pOriginalVMTable = *ppClass;
			m_pNewVMTable = *ppClass;
		}
	}
Exemplo n.º 9
0
void SplinePath::OnNodeSetEnabled(Node* point)
{
    if (!point)
        return;

    WeakPtr<Node> controlPoint(point);

    for (unsigned i = 0; i < controlPoints_.Size(); ++i)
    {
        if (controlPoints_[i] == controlPoint)
        {
            if (point->IsEnabled())
                spline_.AddKnot(point->GetWorldPosition(), i);
            else
                spline_.RemoveKnot(i);

            break;
        }
    }

    CalculateLength();
}
Exemplo n.º 10
0
void SplinePath::RemoveControlPoint(Node* point)
{
    if (!point)
        return;

    WeakPtr<Node> controlPoint(point);

    point->RemoveListener(this);

    for (unsigned i = 0; i < controlPoints_.Size(); ++i)
    {
        if (controlPoints_[i] == controlPoint)
        {
            controlPoints_.Erase(i);
            spline_.RemoveKnot(i);
            break;
        }
    }

    UpdateNodeIds();
    CalculateLength();
}
Exemplo n.º 11
0
bool PLH::X64Detour::Hook()
{
	//Allocate Memory as close as possible to src, to minimize chance 32bit displacements will be out of range (for relative jmp type)
	MEMORY_BASIC_INFORMATION mbi;
	for (size_t Addr = (size_t)m_hkSrc; Addr > (size_t)m_hkSrc - 0x80000000; Addr = (size_t)mbi.BaseAddress - 1)
	{
		if (!VirtualQuery((LPCVOID)Addr, &mbi, sizeof(mbi)))
			break;

		if (mbi.State != MEM_FREE)
			continue;

		if (m_Trampoline = (BYTE*)VirtualAlloc(mbi.BaseAddress, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE))
			break;
	}
	if (!m_Trampoline)
		return false;
	m_NeedFree = true;

	//Decide which jmp type to use based on function size
	bool UseRelativeJmp = false;
	m_hkLength = CalculateLength(m_hkSrc, 16); //More stable 16 byte jmp
	m_OriginalLength = m_hkLength; //We modify hkLength in Relocation routine
	if (m_hkLength == 0)
	{
		UseRelativeJmp = true;
		m_hkLength = CalculateLength(m_hkSrc, 6); //Smaller, less safe 6 byte (jmp could be out of bounds)
		if (m_hkLength == 0)
		{
			PostError(RuntimeError(RuntimeError::Severity::UnRecoverable, "PolyHook x64Detour: Function to small to hook"));
			return false;
		}
	}

	memcpy(m_OriginalCode, m_hkSrc, m_hkLength);
	memcpy(m_Trampoline, m_hkSrc, m_hkLength);
	RelocateASM(m_Trampoline,m_hkLength, (DWORD64)m_hkSrc, (DWORD64)m_Trampoline);
	//Write the jmp from our trampoline back to the original
	WriteAbsoluteJMP((DWORD64)&m_Trampoline[m_hkLength], (DWORD64)m_hkSrc + m_hkLength); 

	// Build a far jump to the Destination function. (jmps not to address pointed at but to the value in the address)
	MemoryProtect Protector(m_hkSrc, m_hkLength, PAGE_EXECUTE_READWRITE);
	int HookSize = 0;
	if (UseRelativeJmp)
	{
		HookSize = 6;
		m_hkSrc[0] = 0xFF;
		m_hkSrc[1] = 0x25;
		//Write 32Bit Displacement from rip
		*(long*)(m_hkSrc + 2) = CalculateRelativeDisplacement<long>((DWORD64)m_hkSrc, (DWORD64)&m_Trampoline[m_hkLength + 16], 6);
		*(DWORD64*)&m_Trampoline[m_hkLength + 16] = (DWORD64)m_hkDest; //Write the address into memory at [RIP+Displacement]
	}else {
		HookSize = 16;
		WriteAbsoluteJMP((DWORD64)m_hkSrc, (DWORD64)m_hkDest);
	}
	//Nop Extra bytes from overwritten opcode
	for (int i = HookSize; i < m_OriginalLength; i++)
		m_hkSrc[i] = 0x90;

	FlushInstructionCache(GetCurrentProcess(), m_hkSrc, m_hkLength);
	PostError(RuntimeError(RuntimeError::Severity::Warning, "PolyHook x64Detour: Relocation can be out of range"));
	return true;
}
Exemplo n.º 12
0
void SplinePath::SetInterpolationMode(InterpolationMode interpolationMode) 
{ 
    spline_.SetInterpolationMode(interpolationMode); 
    CalculateLength();
}