⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wilimage.pas

📁 尚未完成的传奇3资源编辑器,需要就下吧
💻 PAS
📖 第 1 页 / 共 2 页
字号:

					end else if ((PWORD(pwSrc + x + x)^ = $C1) or (PWORD(pwSrc + x + x)^ = $C2) or (PWORD(pwSrc + x + x)^ = $C3)) then begin
						x:=x+1;
						nCntCopyWord := PWORD(pwSrc + x + x)^;
						x:=x+1;
						nLastWidth := nCurrWidth;
						nCurrWidth := nCurrWidth + nCntCopyWord;

						if ( (rc.left > nCurrWidth) or (rc.right < nLastWidth) ) then begin x := x + nCntCopyWord;end
            else begin
              if ((nLastWidth < rc.left) and (rc.left <= nCurrWidth)) then begin
								  x := x + (rc.left-nLastWidth);
                  if pDst<>nil then CopyMemory(@pwDst[((nYCnt+nY) * nDstXSize) + (rc.left+nX)], Pointer( pwSrc + x + x), sizeof(WORD)*(nCurrWidth-rc.left));
                  m_nCalLen:=((nYCnt+nY) * nDstXSize) + (rc.left+nX) + nCurrWidth-rc.left + nCurrWidth-rc.left;
                  if m_nCalLen > nSize then begin
                    ReSult:=True;
                    Exit;
                  end;
								  x := x + (nCurrWidth-rc.left);

              end else if ((nLastWidth <= rc.right) and (rc.right < nCurrWidth) ) then begin
                 if pDst<>nil then CopyMemory(@pwDst[((nYCnt+nY) * nDstXSize) + (nLastWidth+nX)], Pointer( pwSrc + x + x), sizeof(WORD)*(rc.right-nLastWidth));
                  m_nCalLen:=((nYCnt+nY) * nDstXSize) + (nLastWidth+nX) + rc.right-nLastWidth + rc.right-nLastWidth;
                  if m_nCalLen > nSize then begin
                    ReSult:=True;
                    Exit;
                  end;
								  x := x + nCntCopyWord;

              end else begin
                  if pDst<>nil then CopyMemory(@pwDst[((nYCnt+nY) * nDstXSize) + (nLastWidth+nX)], Pointer( pwSrc + x + x), sizeof(WORD)*(nCntCopyWord));
                  m_nCalLen:=((nYCnt+nY) * nDstXSize) + (nLastWidth+nX) + nCntCopyWord + nCntCopyWord;
                  if m_nCalLen > nSize then begin
                    ReSult:=True;
                    Exit;
                  end;
								  x := x + nCntCopyWord;

              end;
            end;
          end;
				end;
				nWidthEnd   :=nWidthEnd + 1;
        m_nWidthEnd :=nWidthEnd;
				nWidthStart	:= nWidthEnd;
				nCurrWidth  := 0;
			end;
	  end;
  end;
end;

procedure TWilImageData.SaveFileFromContainer(bComp:boolean);
var i,nValid:integer;
    pwwmi:PWHOLEWILIMAGEINFO;
    hWil,hWilMap:THandle;
    tfs:TFileStream;
    pWilBuf:Pointer;
    wii:WIXIMAGEINFO;
    nPos:integer;
    P,Img:pointer;

begin

  if m_ImgContainer.Count <=0 then Exit;

  m_nWixNeedSpace:= sizeof(WIXIMAGEINFO) - sizeof(integer) + m_ImgContainer.Count * sizeof(integer) + sizeof(integer)*gnSpaceSize;
  m_nWilNeedSpace:=sizeof(WILFILEHEADER) + gnSpaceSize; //空开1个字节

  for i:=0 to  m_ImgContainer.Count - 1 do begin
    pwwmi:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[i]);
    if pwwmi.bValid then begin
      pwwmi.nOffset:=m_nWilNeedSpace;
      m_nWilNeedSpace:=m_nWilNeedSpace + sizeof(WILIMAGEINFO) + (pwwmi.dwImageLength shl 1)  + gnSpaceSize;
      if m_bIsCompressed then m_nWilNeedSpace := m_nWilNeedSpace + sizeof(dword);
    end else pwwmi.nOffset:=0;
  end;

  InstClear;
  
  if bComp then begin
     m_nWixNeedSpace:=m_nWixNeedSpace + sizeof(Integer);
  end;

  //创建两个文件.
  tfs:=TFileStream.Create(m_sWilFileName,fmCreate);
  tfs.Size:=m_nWilNeedSpace;
  tfs.Free;

  tfs:=TFileStream.Create(m_sWixFileName,fmCreate);
  tfs.Size:=m_nWixNeedSpace;

  hWil:=CreateFile(pchar(m_sWilFileName),GENERIC_WRITE or GENERIC_READ,FILE_SHARE_WRITE,Nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if hWil = INVALID_HANDLE_VALUE then Exit;

  hWilMap:=CreateFileMapping(hWil,nil,PAGE_READWRITE,0,0,nil);
  if hWilMap <= 0 then begin
    CloseHandle(hWil);
    Exit;
  end;
  
  pWilBuf:=MapViewOfFile(hWilMap,FILE_MAP_WRITE,0,0,m_nWilNeedSpace);
  if pWilBuf=nil then begin
    CloseHandle(hWilMap);
    CloseHandle(hWil);
    Exit;
  end;

  try
    CloseHandle(hWilMap);
    CloseHandle(hWil);
  except end;

  wii.nIndexCount:=m_ImgContainer.Count;
  tfs.Position:=0;
  tfs.WriteBuffer(wii, sizeof(WIXIMAGEINFO));

  ZeroMemory(pWilBuf,m_nWilNeedSpace);
  PWILFILEHEADER(pWilBuf).nImageCount :=  m_ImgContainer.Count;

  if bComp then nPos := sizeof(WIXIMAGEINFO)
  else nPos := sizeof(WIXIMAGEINFO) - sizeof(Integer);


  for i:=0 to m_ImgContainer.Count - 1 do begin
    pwwmi:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[i]);
    tfs.Position:=nPos;
    tfs.WriteBuffer(pwwmi.nOffset, sizeof(integer));
    if pwwmi.bValid then begin
      Img := Pointer( dword(pWilBuf) + pwwmi.nOffset );
      CopyImageInfo(Img , pwwmi , true);
      Img := Pointer( dword(Img) + sizeof(WILIMAGEINFO));
      if bComp then Img:=Pointer( dword (Img) + sizeof(dword));
      CopyMemory(Img, pwwmi.pData, (pwwmi.dwImageLength shl 1));
    end;
    nPos:=nPos + sizeof(integer);
  end;

  try
    UnmapViewOfFile(pWilBuf);
    tfs.Free;
  except end;
  
end;

function TWilImageData.ReBuilt():boolean;
var i,j:integer;
    nError:integer;
    bError:boolean;
    pwwmi,ppwwmi2:PWHOLEWILIMAGEINFO;
begin

  ReSult:=false;
  //先清理m_ImgContainer
  while m_ImgContainer.Count > 0 do begin
    pwwmi:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[0]);
    if pwwmi.bValid then FreeMem(pwwmi.pData);
    Dispose(pwwmi);
    m_ImgContainer.Delete(0);
  end;

    if not m_bIsMemMapped then Exit;              //尚未加载
    if m_stWixImgaeInfo.nIndexCount<=0 then Exit; //没有图片

    for i:=0 to m_stWixImgaeInfo.nIndexCount - 1 do begin     //校验
      nError:=0;   //0:有效图片 1:空 2:尺寸错误 3:长度错误 4:越界
      if SetIndex(i) then begin
        Inc(m_nErrors[0]);

        if CheckImageLenError then begin
          nError:=3;
          Inc(m_nErrors[nError]);
        end;
      end else begin
        nError:=1;
        Inc(m_nErrors[nError]);
      end;

      New(pwwmi);

      if nError=0 then begin
        pwwmi.pData:=nil;
        GetMem(pwwmi.pData,m_nWidthEnd shl 1);
        CopyMemory(pwwmi.pData, m_pbCurrImage, m_nWidthEnd shl 1);
        CopyImageInfo(m_lpstCurrWilImageInfo,pwwmi);
        pwwmi.bValid:=true;
        pwwmi.dwImageLength:= m_nWidthEnd;
        pwwmi.nFrom:=Integer(m_pbCurrImage);
        pwwmi.nTo:=pwwmi.nFrom + pwwmi.dwImageLength;
      end else begin
        pwwmi.bValid:=false;
      end;

      m_ImgContainer.AddObject(i,TObject(pwwmi));
    end;

    m_nWixNeedSpace:= sizeof(WIXIMAGEINFO) - sizeof(integer) + m_ImgContainer.Count * sizeof(integer) + sizeof(integer)*gnSpaceSize;
    m_nWilNeedSpace:=sizeof(WILFILEHEADER) + gnSpaceSize; //空开1个字节

    for i:=0 to m_ImgContainer.Count - 1 do begin
      pwwmi:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[i]);
      if pwwmi.bValid then begin
         bError:=false;
         for j:=0 to m_ImgContainer.Count -1 do begin
           if j<>i then begin
            ppwwmi2:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[j]);
            if ((pwwmi.nFrom in [ppwwmi2.nFrom..ppwwmi2.nTo]) or(pwwmi.nTo in [ppwwmi2.nFrom..ppwwmi2.nTo])) then begin
              bError:=True;
              break;
            end;
           end;
         end;
         if bError then begin
           Inc(m_nErrors[4]);
           FreeMem(pwwmi.pData);
           pwwmi.bValid:=false;
           ReSult:=True;
           pwwmi.nOffset:=0;
         end else begin
           pwwmi.nOffset:=m_nWilNeedSpace;
           m_nWilNeedSpace:=m_nWilNeedSpace + sizeof(WILIMAGEINFO) + (pwwmi.dwImageLength shl 1)  + gnSpaceSize;
           if m_bIsCompressed then m_nWilNeedSpace := m_nWilNeedSpace + sizeof(dword);
         end;
      end else  pwwmi.nOffset:=0;
    end;
end;

function  TWilImageData.SetIndex(nIndex:integer):boolean;
var nPos:integer;
    pbTmpData:Pointer;
    pwwmi:PWHOLEWILIMAGEINFO;
begin

  if m_nOpenMode = NrmMode then begin

  if not m_bIsMemMapped then begin
    ReSult:=false;
    Exit;
  end;

  if (nIndex>=0) and (nIndex<=m_stWixImgaeInfo.nIndexCount) then begin

     if m_bIsCompressed then nPos:=m_stWixImgaeInfo.pnPosition[nIndex+1]
     else nPos:=m_stWixImgaeInfo.pnPosition[nIndex];

     if (nPos<=0) then begin
        Result:=false;
        Exit;
     end;

     if (nPos >=(m_WilFileSize - sizeof(WILIMAGEINFO))) then begin
        Result:=false;
        Exit;
     end;

     pbTmpData:=Pointer(dword(m_pbStartData) + nPos);
     m_lpstCurrWilImageInfo:=pbTmpData;

     m_pbCurrImage:=Pointer(dword(pbTmpData) + sizeof(WILIMAGEINFO));

     //if m_bIsCompressed then m_pbCurrImage:=Pointer(dword(m_pbCurrImage) + sizeof(dword));

     if Pdword(dword(m_pbCurrImage))^=0 then m_pbCurrImage:=Pointer(dword(m_pbCurrImage) + sizeof(dword));  //改成自动判断图片是GT的还是EI的。
     //因为光通的图片,在WILIMAGEINFO后有一个无用的dword;

     m_nCurrImageIdx:=nIndex;
     ReSult:=true;
     Exit;
  end;
  ReSult:=false;

  end else if m_nOpenMode = ReBMode then ReSult:= SetIndexEx(nIndex);

end;

function  TWilImageData.SetIndexEx(nIndex:integer):boolean;
var pwwmi:PWHOLEWILIMAGEINFO;
begin
  ReSult:=false;
  if m_ImgContainer.Count > 0 then begin
    if ((nIndex>=0) and (nIndex<m_ImgContainer.Count)) then begin
      m_nCurrImageIdx:=nIndex;
      pwwmi:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[nIndex]);
      if pwwmi.bValid then begin
        m_lpstCurrWilImageInfo:=PWILIMAGEINFO(pwwmi);
        m_pbCurrImage:=pwwmi.pData;
        ReSult:=true;
      end else begin
        m_lpstCurrWilImageInfo:=nil;
        m_pbCurrImage:=nil;
      end;
    end;
  end;
end;

function  TWilImageData.Load(const szWilFile:string;nOpenMode:OpenMode;bComp:boolean):boolean;
begin
  ReSult:=OpenFile(szWilFile, bComp);
  case nOpenMode of
    ReBMode:
      try
        Result:=ReBuilt;
        InstClear;
      except
        ReSult:=false;
      end;
  end;
  m_nOpenMode := nOpenMode;
end;


function  TWilImageData.OpenFile(const szWilFile:string;bComp:boolean):boolean;
var
  hWilFile,hWixFile,hWilMappedFile:THandle;
  dwReadLen:dword;
  szWixFile:array[0..MAX_PATH] of char;
  stWilFileHeader:WILFILEHEADER;
  Temp:dword;

begin
  if m_bIsMemMapped then begin
    ReSult:=true;
    exit;
  end;

  m_bIsCompressed:=bComp;

  strcopy(szWixFile, pchar(szWilFile));
  strcopy(szWixFile + length(szWilFile) - 3, 'wix');

  m_sWilFileName:=szWilFile;
  m_sWixFileName:=string(szWixFile);



  hWixFile:= CreateFile(szWixFile, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if hWixFile <> INVALID_HANDLE_VALUE then begin
    ReadFile(hWixFile, m_stWixImgaeInfo,sizeof(WIXIMAGEINFO)-sizeof(integer), dwReadLen, 0);
    setLength(m_stWixImgaeInfo.pnPosition,m_stWixImgaeInfo.nIndexCount + 2);

    //这里真TMD的特殊。delphi这个毛编译器真TMD的垃圾。
    ReadFile(hWixFile,Pointer(pdword(dword(m_stWixImgaeInfo.pnPosition))^), sizeof(integer)*m_stWixImgaeInfo.nIndexCount, dwReadLen,0);
    CloseHandle(hWixFile);

    hWilFile := CreateFile(pchar(szWilFile), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
    if hWilFile <> INVALID_HANDLE_VALUE then begin
      m_WilFileSize:=GetFileSize(hWilFile, nil);
      ReadFile(hWilFile, stWilFileHeader, sizeof(WILFILEHEADER), dwReadLen, 0);
      if  dwReadLen > 0 then begin
        hWilMappedFile := CreateFileMapping(hWilFile, nil, PAGE_READONLY, 0, 0, nil);
        m_pbStartData := MapViewOfFile(hWilMappedFile, FILE_MAP_READ, 0, 0, 0);
        if m_pbStartData=nil then begin
					CloseHandle(hWilFile);
					CloseHandle(hWixFile);
					m_bIsMemMapped := FALSE;
					ReSult:=FALSE;
          exit;
        end;
				m_bIsMemMapped := TRUE;
				CloseHandle(hWilMappedFile);
      end else begin
        GetMem(m_pbStartData,m_WilFileSize);

        //真TMD的疯了,要不会汇编,怎么死都不知道。delphi这个毛编译器真TMD的垃圾。
        ReadFile(hWilFile, Pointer(pdword(dword(m_pbStartData))^), m_WilFileSize, dwReadLen, 0);
      end;
			SetIndex(0);
			CloseHandle(hWilFile);
			ReSult:=true;
      Exit;
    end;
  end;
  ReSult:=false;
end;

procedure  TWilImageData.Clear;
var pwwmi:PWHOLEWILIMAGEINFO;
begin
  if  m_pbStartData<>nil then begin
    if m_bIsMemMapped then begin
      UnmapViewOfFile(m_pbStartData);
			m_pbStartData := nil;
			m_bIsMemMapped := FALSE;
    end else begin
      FreeAndNil(m_pbStartData);
    end;
  end;

  while m_ImgContainer.Count > 0 do begin
    pwwmi:=PWHOLEWILIMAGEINFO(m_ImgContainer.Objects[0]);
    if pwwmi.bValid then FreeMem(pwwmi.pData);
    Dispose(pwwmi);
    m_ImgContainer.Delete(0);
  end;

  m_WilFileSize:=0;
  ZeroMemory(@m_nErrors[0],sizeof(Integer) * gnErrors);

  SetLength(m_stWixImgaeInfo.pnPosition,0);
  ZeroMemory(@m_stWixImgaeInfo,sizeof(WIXIMAGEINFO));
  Init;  
end;

procedure  TWilImageData.InstClear;
begin
  try
  if  m_pbStartData<>nil then begin
    if m_bIsMemMapped then begin
      UnmapViewOfFile(m_pbStartData);
			m_pbStartData := nil;
			m_bIsMemMapped := FALSE;
    end else begin
      FreeAndNil(m_pbStartData);
    end;
  end;
  except
  end;
end;

procedure  TWilImageData.Init;
begin
  m_bIsMemMapped  := FALSE;
  m_bIsCompressed := FALSE;
  m_pbStartData   := nil;
  ZeroMemory(@m_stWixImgaeInfo,sizeof(WIXIMAGEINFO));

  m_nCurrImageIdx := -1;
  m_pbCurrImage   := nil;
  m_lpstCurrWilImageInfo := nil;
  m_WilFileSize:=0;
  ZeroMemory(@m_nErrors[0],sizeof(Integer) * gnErrors);

  m_nOpenMode:=NrmMode;
  m_bRebuilding:=true;

end;

destructor TWilImageData.Destroy;
begin
  Clear;
  m_ImgContainer.Free;
  m_BreakTimer.Free;
  inherited Destroy;
end;

constructor TWilImageData.Create(bSusp:boolean);
begin
  inherited Create(bSusp);
  m_ImgContainer:=List32.Create;

  m_BreakTimer:=TTimer.Create(nil);
  m_BreakTimer.Enabled:=false;
  m_BreakTimer.Interval:=5000;
  m_BreakTimer.OnTimer:=BreakTimerTimer;
  StatusBar:=nil;

  Init;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -