#include "CFTTXmlReader.h"
#include "CFTTFile_ROM.h"
#include "CFTTFileSystem.h"
#include "CFTTXmlMemWrapper.h"
#include "zlib.h"

//2022.03.08 xml系列的所有函数更衣室批量测试正常 Anderson

//-------------------------------------------------------------------------------------------------
//00166254 //^_^
CFTTXmlReader::CFTTXmlReader() {
}
//-------------------------------------------------------------------------------------------------
//0023A6DC //^_-
CFTTXmlReader::CFTTXmlReader(char const *pData, uint dSize, bool isCompress, uint dKey) {
  pxml_doc_8 = nullptr;
  pXmlBuf_C = nullptr;
  bClose_10 = false;

  TFTTFileAttributes vAttr;
  vAttr.filesize_0 = dSize;
  vAttr.ullTime_8 = 0;
  CFTTFile_ROM vFileRom(pData, &vAttr, false, false, false, false, EFTTMemHeapID_1);

  OpenFile(&vFileRom, isCompress, dKey);
}
//-------------------------------------------------------------------------------------------------
//0023A730
CFTTXmlReader::CFTTXmlReader(char const *pName, bool isCompress, uint dKey) {
  pxml_doc_8 = nullptr;
  pXmlBuf_C = nullptr;
  bClose_10 = false;
  CFTTFile *pFile = CFTTFileSystem::fopen(pName, "rb", EFTTMemHeapID_0);
  if (pFile) {
    //0023A75E
    if (pFile->IsOpen()) {
      int dr0 = 0;
      if (strstr(pName, ".zl") != nullptr) {
        dr0 = 1;
      } else if (strstr(pName, ".dat") != nullptr) {
        //loc_23A784
        dr0 = 1;
      }
      //loc_23A794
      OpenFile(pFile, dr0 | isCompress, dKey);
    } else {
      //loc_23A77E
      bClose_10 = true;
    }
  } else {
    //loc_23A778
    bClose_10 = true;
  }
  //loc_23A7A2
}
//-------------------------------------------------------------------------------------------------
//0023A7C0
CFTTXmlReader::CFTTXmlReader(CFTTFile *pFile, bool b, uint a3) {
  pxml_doc_8 = nullptr;
  pXmlBuf_C = nullptr;
  bClose_10 = false;
  OpenFile(pFile, b, a3);
}
//-------------------------------------------------------------------------------------------------
//0023A7E8 0023A854
CFTTXmlReader::~CFTTXmlReader() {
  if (pxml_doc_8) {
    pxml_doc_8->clear();
    delete pxml_doc_8;
  }

  delete pXmlBuf_C;
  pXmlBuf_C = nullptr;
}
//-------------------------------------------------------------------------------------------------
//0023A4D8 -_- 经过单元测试，功能正常
//单元测试要注意，不能hook uncompress 函数
void CFTTXmlReader::OpenFile(CFTTFile *pFile, bool isCompress, uint dKey) {
  TFTTFileAttributes *pAttr = pFile->GetAttributes();
  int len32_r5 = pAttr->filesize_0;
  bClose_10 = false;
  if (isCompress) {
    //0023A4FA
    uLongf dlen_r2 = 0x20000;
    int dfilesize_r5 = len32_r5 * 0x14;
    if (dfilesize_r5 > 0x20000) {
      dlen_r2 = dfilesize_r5;
    }

    pXmlBuf_C = new char[dlen_r2 | 1];
    uchar *pFileContent = new uchar[len32_r5 + 1];
    memset(pFileContent, 0, len32_r5 + 1);
    pFile->Read(pFileContent, pAttr->filesize_0);
    if (dKey) {
      FTTDecode(pFileContent, len32_r5, dKey);
    }

    //loc_23A548
    int duncomp_err = uncompress((Bytef *)pXmlBuf_C, &dlen_r2, pFileContent, len32_r5);
    //0023A596
    if (duncomp_err == Z_OK) {
      pXmlBuf_C[dlen_r2] = 0;
      delete[] pFileContent;
    } else {
      //loc_23A5C4
      delete[] pXmlBuf_C;
      bClose_10 = true;
    }
  } else {
    //loc_23A5A2
    pXmlBuf_C = new char[len32_r5 + 1];
    pFile->Read(pXmlBuf_C, pAttr->filesize_0);
    pXmlBuf_C[len32_r5] = 0;
  }

  //loc_23A5D8
  if (!bClose_10)
    CreateXmlDoc();
}
//-------------------------------------------------------------------------------------------------
//0023A5EC
void CFTTXmlReader::CreateXmlDoc() {
  pxml_doc_8 = new rapidxml::xml_document<>;
  pxml_doc_8->set_allocator(CFTTXmlMemWrapper::MemAlloc, CFTTXmlMemWrapper::MemFree);
  if (pXmlBuf_C) {
    pxml_doc_8->parse<0>(pXmlBuf_C);
    XmlReaderNode_4.prapidxml_xml_node_0 = pxml_doc_8;
  }
  //locret_23A624
}
//-------------------------------------------------------------------------------------------------
//0023A6D8
bool CFTTXmlReader::UncompressFailed() {
  return bClose_10;
}
//-------------------------------------------------------------------------------------------------