/*************************************************
*           BK Emulator v3.8 for Windows         *
*************************************************/

/*
File: MSFManager.cpp
Version: 1.0.0
Written by: Yuriy Kalmykov <kalmykov@stoik.com>
    Copyright (c) 2002-2004 Yuriy Kalmykov

    BK Emulator is a program emulated hardware environment for running
code for BK 0010(01) in different configurations.

    This code may be used in compiled form in any way you desire.
This file or it's parts can't be redistributed without the authors
written consent, but can be modified for your private needs.
    Providing that this notice and the authors name and all copyright
notices remains intact.

    Please, an email me to know how you are using it and where. You can
ask me for any information about this below code or any attendant
knowledge.

    This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability for any damage or loss of business that
this product may cause.
*/


// MSFManager.cpp: implementation of the CMSFManager class.
//


#include "stdafx.h"
#include "Globals.h"
#include "MSFManager.h"
#include "BKErrMsgSys.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction


CMSFManager::CMSFManager()
{
	m_header.type = MSF_STATE_ID;
	m_header.version = MSF_VERSION_CURRENT;
	m_header.configuration = CONFIG_BKMODEL_NUMBER_BK_0010_01;
}

CMSFManager::~CMSFManager()
{
	ReleaseFile();
}


bool CMSFManager::ReleaseFile()
{
	if (m_fileMSF.m_hFile != CFile::hFileNull) //если файл был открыт
	{
		m_fileMSF.Close(); //закрываем
		m_blocks.RemoveAll();
		return true;
	}
	return false;
}

bool CMSFManager::Seek(int nOffset, UINT nFrom)
{
	try
	{
		m_fileMSF.Seek(nOffset, nFrom);
	}
	catch (CFileException &e)
	{
		return false;
	}

	return true;
}

//проверка msf файла
bool CMSFManager::CheckFile(const CString &strPath, bool bSilent)
{
	bool bRet = true;
	ReleaseFile();
	DWORD flag = CFile::modeRead;

	if (m_fileMSF.Open(strPath, flag))
	{
		int nFilePos = 0;

		if (m_fileMSF.Read(&m_header, sizeof(m_header)) == sizeof(m_header))
		{
			nFilePos += sizeof(m_header);

			if (m_header.type == MSF_STATE_ID)
			{
				MSF_BLOCK_INFO bi;

				while (m_fileMSF.Read(&bi.header, sizeof(MSF_BLOCK_HEADER)) == sizeof(MSF_BLOCK_HEADER))
				{
					bi.offset = nFilePos;
					m_blocks.Add(bi);

					if (!Seek(bi.header.length - sizeof(MSF_BLOCK_HEADER), CFile::current))
					{
						if (!bSilent)
						{
							CBKMessageBox(g_pstrErrors[BK_ERROR_MSFCORRUPTMSF], MB_OK);
						}

						bRet = false;
						break;
					}

					nFilePos += bi.header.length;
				}
			}
			else
			{
				if (!bSilent)
				{
					CBKMessageBox(g_pstrErrors[BK_ERROR_MSFNOMSFFILE], MB_OK);
				}

				bRet = false;
			}
		}
		else
		{
			if (!bSilent)
			{
				CBKMessageBox(g_pstrErrors[BK_ERROR_MSFREADHEADERRROR], MB_OK);
			}

			bRet = false;
		}
		ReleaseFile();
	}
	else
	{
		if (!bSilent)
		{
			CBKMessageBox(g_pstrErrors[BK_ERROR_MSFOPENFILEERROR], MB_OK);
		}

		bRet = false;
	}

	return bRet;
}

bool CMSFManager::OpenFile(const CString &strPath, bool bLoad)
{
	bool bRet = true;
	ReleaseFile();
	DWORD flag = CFile::modeRead;

	if (!bLoad)
	{
		flag |= CFile::modeCreate | CFile::modeWrite;
	}

	if (m_fileMSF.Open(strPath, flag))
	{
		m_bOpenForLoad = bLoad;

		if (m_bOpenForLoad)
		{
			if (m_fileMSF.Read(&m_header, sizeof(m_header)) == sizeof(m_header))
			{
				int nFilePos = sizeof(m_header);

				if (m_header.type == MSF_STATE_ID)
				{
					MSF_BLOCK_INFO bi;

					while (m_fileMSF.Read(&bi.header, sizeof(MSF_BLOCK_HEADER)) == sizeof(MSF_BLOCK_HEADER))
					{
						bi.offset = nFilePos;
						m_blocks.Add(bi);

						if (!Seek(bi.header.length - sizeof(MSF_BLOCK_HEADER), CFile::current))
						{
							CBKMessageBox(g_pstrErrors[BK_ERROR_MSFCORRUPTMSF], MB_OK);
							bRet = false;
							break;
						}

						nFilePos += bi.header.length;
					}
				}
				else
				{
					CBKMessageBox(g_pstrErrors[BK_ERROR_MSFNOMSFFILE], MB_OK);
					bRet = false;
				}
			}
			else
			{
				CBKMessageBox(g_pstrErrors[BK_ERROR_MSFREADHEADERRROR], MB_OK);
				bRet = false;
			}
		}
		else
		{
			try
			{
				m_fileMSF.Write(&m_header, sizeof(m_header));
			}
			catch (CFileException &e)
			{
				CBKMessageBox(g_pstrErrors[BK_ERROR_MSFWRITEHEADERRROR], MB_OK);
				bRet = false;
			}
		}
	}
	else
	{
		CBKMessageBox(g_pstrErrors[BK_ERROR_MSFOPENFILEERROR], MB_OK);
		bRet = false;
	}

	if (!bRet)
	{
		ReleaseFile();
	}

	return bRet;
}


bool CMSFManager::FindBlock(DWORD blockType, MSF_BLOCK_INFO *pBi)
{
	for (int n = 0; n < m_blocks.GetSize(); ++n)
	{
		MSF_BLOCK_INFO bi = m_blocks[n];

		if (bi.header.type == blockType)
		{
			*pBi = bi;
			return true;
		}
	}

	return false;
}


bool CMSFManager::GetBlockHeader(MSF_BLOCK_INFO *pBi)
{
	bool bRet = false;

	if (Seek(pBi->offset, CFile::begin))
	{
		if (Seek(sizeof(MSF_BLOCK_HEADER), CFile::current))
		{
			bRet = true;
		}
	}
	else
	{
		CBKMessageBox(g_pstrErrors[BK_ERROR_MSFSEEKERROR], MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockData(BYTE *pBuff, DWORD length)
{
	if (m_fileMSF.Read(pBuff, length) != length)
	{
		return false;
	}

	return true;
}


bool CMSFManager::SetBlockHeader(MSF_BLOCK_INFO *pBi)
{
	try
	{
		m_fileMSF.Write(&pBi->header, sizeof(MSF_BLOCK_HEADER));
	}
	catch (CFileException &e)
	{
		return false;
	}

	return true;
}


bool CMSFManager::SetBlockData(BYTE *pBuff, DWORD length)
{
	try
	{
		m_fileMSF.Write(pBuff, length);
	}
	catch (CFileException &e)
	{
		return false;
	}

	return true;
}



bool CMSFManager::GetBlockPreview(HBITMAP *hScreenshot)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_PREVIEW, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			BITMAPINFOHEADER bmih;

			if (GetBlockData(reinterpret_cast<BYTE *>(&bmih), sizeof(bmih)))
			{
				BYTE *pBits = nullptr;
				HBITMAP hBm = nullptr;

				if ((hBm = ::CreateDIBSection(nullptr, (BITMAPINFO *)&bmih, DIB_RGB_COLORS, (void **)&pBits, nullptr, 0)))
				{
					if (GetBlockData(pBits, bmih.biSizeImage))
					{
						bRet = true;
					}
					else
					{
						str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
						CBKMessageBox(str, MB_OK);
					}
				}
				else
				{
					CBKMessageBox(g_pstrErrors[BK_ERROR_MSFPREVIEWBLK_DIB], MB_OK);
				}
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockPreview(HBITMAP hScreenshot)
{
	DIBSECTION ds;
	CString str;
	bool bRet = false;

	if (::GetObject(hScreenshot, sizeof(ds), &ds))
	{
		MSF_BLOCK_INFO bi;
		bi.header.type = MSF_BLOCKTYPE_PREVIEW;
		bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(ds.dsBmih) + ds.dsBmih.biSizeImage;

		if (SetBlockHeader(&bi))
		{
			if (SetBlockData(reinterpret_cast<BYTE *>(&ds.dsBmih), sizeof(ds.dsBmih)))
			{
				ASSERT(ds.dsBm.bmBits);

				if (SetBlockData(reinterpret_cast<BYTE *>(ds.dsBm.bmBits), ds.dsBmih.biSizeImage))
				{
					bRet = true;
				}
				else
				{
					str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
					CBKMessageBox(str, MB_OK);
				}
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKPREVIEW]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		CBKMessageBox(g_pstrErrors[BK_ERROR_MSFPREVIEWBLK_OBJ]);
	}

	return bRet;
}


bool CMSFManager::GetBlockBaseMemory(BYTE *pMemory)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_BASEMEMORY, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(pMemory, 0200000))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKBASEMEM]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKBASEMEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKBASEMEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockBaseMemory(BYTE *pMemory)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_BASEMEMORY;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + 0200000;

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(pMemory, 0200000))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKBASEMEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKBASEMEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockBaseMemory11M(BYTE *pMemory)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_BASEMEMORY11M, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(pMemory, 0700000))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKBASEMEM11]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKBASEMEM11]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKBASEMEM11]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockBaseMemory11M(BYTE *pMemory)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_BASEMEMORY11M;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + 0700000;  //0700000 == 128кб ОЗУ + 80кб ПЗУ, в том числе FDD + 16кб ОЗУ FDD

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(pMemory, 0700000))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKBASEMEM11]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKBASEMEM11]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockMemorySMK512(BYTE *pMemory)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_MEMORY_SMK512, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(pMemory, (512 - 16) * 1024))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKSMK512MEM]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKSMK512MEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKSMK512MEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockMemorySMK512(BYTE *pMemory)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_MEMORY_SMK512;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + (512 - 16) * 1024;

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(pMemory, (512 - 16) * 1024))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKSMK512MEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKSMK512MEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockCPURegisters(MSF_CPU_REGISTERS *pCPURegisters)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_CPU_REGISTERS, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(reinterpret_cast<BYTE *>(pCPURegisters), sizeof(MSF_CPU_REGISTERS)))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKCPUREGS]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKCPUREGS]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKCPUREGS]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockCPURegisters(MSF_CPU_REGISTERS *pCPURegisters)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_CPU_REGISTERS;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(MSF_CPU_REGISTERS);

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(reinterpret_cast<BYTE *>(pCPURegisters), sizeof(MSF_CPU_REGISTERS)))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKCPUREGS]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKCPUREGS]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockPortRegs(MSF_PORT_REGS *pPortRegs)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (m_header.version < MSF_VERSION_MINIMAL)
	{
		return bRet;
	}

	if (FindBlock(MSF_BLOCKTYPE_PORT_REGS, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(reinterpret_cast<BYTE *>(pPortRegs), sizeof(MSF_PORT_REGS)))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKSYSREGS]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKSYSREGS]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKSYSREGS]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::SetBlockPortRegs(MSF_PORT_REGS *pPortRegs)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_PORT_REGS;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(MSF_PORT_REGS);

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(reinterpret_cast<BYTE *>(pPortRegs), sizeof(MSF_PORT_REGS)))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKSYSREGS]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKSYSREGS]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockExt32Memory(DWORD *pnPage, BYTE *pMemoryExt)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_10EXT32, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(reinterpret_cast<BYTE *>(&pnPage), sizeof(DWORD)))
			{
				if (GetBlockData(pMemoryExt, 4 * 8192))
				{
					bRet = true;
				}
				else
				{
					str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
					CBKMessageBox(str, MB_OK);
				}
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockExt32Memory(DWORD nPage, BYTE *pMemoryExt)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_10EXT32;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(DWORD) + 4 * 8192;

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(reinterpret_cast<BYTE *>(&nPage), sizeof(DWORD)))
		{
			if (SetBlockData(pMemoryExt, 4 * 8192))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKEXT32MEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockExt16Memory(BYTE *pMemoryExt)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_10EXT16, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(pMemoryExt, 3 * 8192))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKEXT16MEM]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKEXT16MEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKEXT16MEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockExt16Memory(BYTE *pMemoryExt)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_10EXT16;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + 3 * 8192;  //сохраняем 16кб озу + 8 кб пзу контроллера

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(pMemoryExt, 3 * 8192))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKEXT16MEM]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKEXT16MEM]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::GetBlockTape(BYTE *pPackedWave, DWORD *pnBitsLength, DWORD *pnPackLength)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_WAVE, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			DWORD nPackLength = bi.header.length - sizeof(MSF_BLOCK_HEADER) - sizeof(DWORD);

			if (GetBlockData(reinterpret_cast<BYTE *>(pnBitsLength), sizeof(DWORD)))
			{
				if (!pPackedWave)
				{
					*pnPackLength = nPackLength;
					return true;
				}

				if (GetBlockData(reinterpret_cast<BYTE *>(pPackedWave), nPackLength))
				{
					bRet = true;
				}
				else
				{
					str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKTAPE]);
					CBKMessageBox(str, MB_OK);
				}
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKTAPE]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKTAPE]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKTAPE]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockTape(BYTE *pPackedWave, DWORD nBitsLength, DWORD nPackLength)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_WAVE;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(DWORD) + nPackLength;

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(reinterpret_cast<BYTE *>(&nBitsLength), sizeof(DWORD)))
		{
			if (SetBlockData(reinterpret_cast<BYTE *>(pPackedWave), nPackLength))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKTAPE]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKTAPE]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKTAPE]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::GetBlockMemMap(BKMEMBank_Type *MBType, ConfBKModel_Type *CBKMType)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_MEMMAP, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(reinterpret_cast<BYTE *>(MBType), sizeof(BKMEMBank_Type) * 16))
			{
				if (GetBlockData(reinterpret_cast<BYTE *>(CBKMType), sizeof(ConfBKModel_Type)))
				{
					bRet = true;
				}
				else
				{
					str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
					CBKMessageBox(str, MB_OK);
				}
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		//в старых версиях этого блока не было,
		//поэтому старые версии будут не совместимы с новой. и ничего тут не поделать
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockMemMap(BKMEMBank_Type *MBType, ConfBKModel_Type *CBKMType)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_MEMMAP;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(BKMEMBank_Type) * 16 + sizeof(ConfBKModel_Type);

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(reinterpret_cast<BYTE *>(MBType), sizeof(BKMEMBank_Type) * 16))
		{
			if (SetBlockData(reinterpret_cast<BYTE *>(CBKMType), sizeof(ConfBKModel_Type)))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKMEMMAPMGR]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}



bool CMSFManager::GetBlockConfig()
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (FindBlock(MSF_BLOCKTYPE_CONFIG, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			DWORD nBlockLength = bi.header.length - sizeof(MSF_BLOCK_HEADER);
			BYTE *pBlock = new BYTE[nBlockLength];

			if (pBlock)
			{
				if (GetBlockData(pBlock, nBlockLength))
				{
					g_Config.LoadConfig_FromMemory(pBlock, nBlockLength);
					bRet = true;
				}
				else
				{
					str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKCONFIG]);
					CBKMessageBox(str, MB_OK);
				}

				delete [] pBlock;
			}
			else
			{
				CBKMessageBox(g_pstrErrors[BK_ERROR_NOTENMEMR]);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKCONFIG]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKCONFIG]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}


bool CMSFManager::SetBlockConfig()
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_CONFIG;
	bi.header.length = sizeof(MSF_BLOCK_HEADER);  //+ длина блока. длина переменная
	UINT nSize = _MAX_PATH * 1000;
	BYTE *pBlock = new BYTE[nSize]; //размер будет достаточен наверняка

	if (pBlock)
	{
		ZeroMemory(pBlock, nSize);
		ULONGLONG len = g_Config.SaveConfig_ToMemory(pBlock, nSize);
		bi.header.length += (DWORD)len;

		if (SetBlockHeader(&bi))
		{
			if (SetBlockData(pBlock, (DWORD)len))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKCONFIG]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKCONFIG]);
			CBKMessageBox(str, MB_OK);
		}

		delete [] pBlock;
	}
	else
	{
		CBKMessageBox(g_pstrErrors[BK_ERROR_NOTENMEMR]);
	}

	return bRet;
}

bool CMSFManager::GetBlockFrameData(MSF_FRAMEDATA *FDBlock)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;

	if (m_header.version < MSF_VERSION_MINIMAL)
	{
		return bRet;
	}

	if (FindBlock(MSF_BLOCKTYPE_FRAMEDATA, &bi))
	{
		if (GetBlockHeader(&bi))
		{
			if (GetBlockData(reinterpret_cast<BYTE *>(FDBlock), sizeof(MSF_FRAMEDATA)))
			{
				bRet = true;
			}
			else
			{
				str.Format(g_pstrErrors[BK_ERROR_DTABLKRDERR], g_pstrErrors[BK_ERROR_BLKFRAMEDATA]);
				CBKMessageBox(str, MB_OK);
			}
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_HDRBLKRDERR], g_pstrErrors[BK_ERROR_BLKFRAMEDATA]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_BLKNOTFOUND], g_pstrErrors[BK_ERROR_BLKFRAMEDATA]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}

bool CMSFManager::SetBlockFrameData(MSF_FRAMEDATA *FDBlock)
{
	MSF_BLOCK_INFO bi;
	CString str;
	bool bRet = false;
	bi.header.type = MSF_BLOCKTYPE_FRAMEDATA;
	bi.header.length = sizeof(MSF_BLOCK_HEADER) + sizeof(MSF_FRAMEDATA);

	if (SetBlockHeader(&bi))
	{
		if (SetBlockData(reinterpret_cast<BYTE *>(FDBlock), sizeof(MSF_FRAMEDATA)))
		{
			bRet = true;
		}
		else
		{
			str.Format(g_pstrErrors[BK_ERROR_DTABLKWRERR], g_pstrErrors[BK_ERROR_BLKFRAMEDATA]);
			CBKMessageBox(str, MB_OK);
		}
	}
	else
	{
		str.Format(g_pstrErrors[BK_ERROR_HDRBLKWRERR], g_pstrErrors[BK_ERROR_BLKFRAMEDATA]);
		CBKMessageBox(str, MB_OK);
	}

	return bRet;
}
