Skip to content

Commit

Permalink
Merge pull request #19 from gwdevhub/master
Browse files Browse the repository at this point in the history
reassemble c++ code from assembly
  • Loading branch information
Jonathan-Greve authored Sep 5, 2024
2 parents 49a12b4 + 8dea921 commit 50357ce
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 253 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

.idea

# User-specific files
*.rsuser
*.suo
Expand Down
2 changes: 2 additions & 0 deletions GuildWarsMapBrowser.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<ClInclude Include="peglib\peglib.h" />
<ClInclude Include="SourceFiles\AMAT_file.h" />
<ClInclude Include="SourceFiles\AtexAsm.h" />
<ClInclude Include="SourceFiles\AtexDecompress.h" />
<ClInclude Include="SourceFiles\AtexReader.h" />
<ClInclude Include="SourceFiles\BlendStateManager.h" />
<ClInclude Include="SourceFiles\Box.h" />
Expand Down Expand Up @@ -395,6 +396,7 @@
</ClCompile>
<ClCompile Include="SourceFiles\AMAT_file.cpp" />
<ClCompile Include="SourceFiles\AtexAsm.cpp" />
<ClCompile Include="SourceFiles\AtexDecompress.cpp" />
<ClCompile Include="SourceFiles\AtexReader.cpp" />
<ClCompile Include="SourceFiles\BlendStateManager.cpp" />
<ClCompile Include="SourceFiles\Box.cpp" />
Expand Down
266 changes: 30 additions & 236 deletions SourceFiles/AtexAsm.cpp
Original file line number Diff line number Diff line change
@@ -1,210 +1,6 @@
#include "pch.h"
#include "pch.h"
#include "AtexAsm.h"

void AtexSubCode1_(unsigned int a, unsigned int b, unsigned int c);
void AtexSubCode2_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f);
void AtexSubCode3_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f);
void AtexSubCode4_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f);
void AtexSubCode5_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f, unsigned int g);
void AtexSubCode6();
void AtexSubCode7_(unsigned int a, unsigned int b);

unsigned int ImageFormats[] = {0x0B2, 0x12, 0x0B2, 0x72, 0x12, 0x12, 0x12, 0x100,
0x1A4, 0x1A4, 0x1A4, 0x104, 0x0A2, 0x78, 0x400, 0x71,
0x0B1, 0x0B1, 0x0B1, 0x0B1, 0x0A1, 0x11, 0x201};

unsigned char byte_79053C[] = {
0x6, 0x10, 0x6, 0x0F, 0x6, 0x0E, 0x6, 0x0D, 0x6, 0x0C, 0x6, 0x0B, 0x6, 0x0A, 0x6, 0x9,
0x6, 0x8, 0x6, 0x7, 0x6, 0x6, 0x6, 0x5, 0x6, 0x4, 0x6, 0x3, 0x6, 0x2, 0x6, 0x1,
0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11,
0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11,
0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0,
0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0,
0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0,
0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0};

unsigned char byte_79053D[] = {
0x10, 0x6, 0x0F, 0x6, 0x0E, 0x6, 0x0D, 0x6, 0x0C, 0x6, 0x0B, 0x6, 0x0A, 0x6, 0x9, 0x6,
0x8, 0x6, 0x7, 0x6, 0x6, 0x6, 0x5, 0x6, 0x4, 0x6, 0x3, 0x6, 0x2, 0x6, 0x1, 0x2,
0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2,
0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x2, 0x11, 0x1,
0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1,
0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1,
0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1,
0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0};

int ImgFmt(unsigned int Format)
{
if (Format >= 0x17)
{
printf("ERROR: bad image format (%d)!", Format);
exit(0);
}
return ImageFormats[Format];
}

struct SImageData
{
unsigned int *DataPos, *EndPos, _44, _40, _3C, xres, yres;
};

void AtexDecompress(unsigned int* InputBuffer, unsigned int BufferSize, unsigned int ImageFormat,
SImageDescriptor ImageDescriptor, unsigned int* OutBuffer)
{
unsigned int HeaderSize = 12;

SImageData ImageData;

int AlphaDataSize2 = ((ImageFormat && 21) - 1) & 2;

int ColorDataSize = ImgFmt(ImageFormat);
int AlphaDataSize = ColorDataSize;

AlphaDataSize &= 640;
if (AlphaDataSize)
{
AlphaDataSize = 2;
}

ColorDataSize &= 528;
if (ColorDataSize)
{
ColorDataSize = 2;
}

int BlockSize = ColorDataSize + AlphaDataSize2 + AlphaDataSize;

int BlockCount = ImageDescriptor.xres * ImageDescriptor.yres / 16;

if (! BlockCount)
{
printf("BlockCount zero\n");
return;
}

unsigned int* DcmpBuffer1 = new unsigned int[BlockCount];
unsigned int* DcmpBuffer2 = DcmpBuffer1 + BlockCount / 2;
memset(DcmpBuffer1, 0, BlockCount * 4);

ImageData.xres = ImageDescriptor.xres;
ImageData.yres = ImageDescriptor.yres;

unsigned int DataSize = InputBuffer[HeaderSize >> 2];

if (HeaderSize + 8 >= BufferSize)
{
printf("Error 567h\n");
}
if (DataSize <= 8)
{
printf("Error 569h\n");
}
if (DataSize + HeaderSize > BufferSize)
{
printf("Error 56Ah\n");
}

int CompressionCode = InputBuffer[(HeaderSize + 4) >> 2];

ImageData.DataPos = InputBuffer + ((HeaderSize + 8) >> 2);

if (CompressionCode)
{
ImageData._40 = ImageData._44 = ImageData._3C = 0;
ImageData.EndPos = ImageData.DataPos + ((DataSize - 8) >> 2);

if (ImageData.DataPos != ImageData.EndPos)
{
ImageData._40 = ImageData.DataPos[0];
ImageData.DataPos++;
}

if (CompressionCode & 0x10 && ImageData.xres == 256 && ImageData.yres == 256 &&
(ImageFormat == 0x11 || ImageFormat == 0x10))
{
AtexSubCode1_((unsigned int)DcmpBuffer1, (unsigned int)DcmpBuffer2, BlockCount);
}
if (CompressionCode & 1 && ColorDataSize && ! AlphaDataSize && ! AlphaDataSize2)
{
AtexSubCode2_((unsigned int)OutBuffer, (unsigned int)DcmpBuffer1, (unsigned int)DcmpBuffer2,
(unsigned int)&ImageData, BlockCount, BlockSize);
}
if (CompressionCode & 2 && ImageFormat >= 0x10 && ImageFormat <= 0x11)
{
AtexSubCode3_((unsigned int)OutBuffer, (unsigned int)DcmpBuffer1, (unsigned int)DcmpBuffer2,
(unsigned int)&ImageData, BlockCount, BlockSize);
}
if (CompressionCode & 4 && ImageFormat >= 0x12 && ImageFormat <= 0x15)
{
AtexSubCode4_((unsigned int)OutBuffer, (unsigned int)DcmpBuffer1, (unsigned int)DcmpBuffer2,
(unsigned int)&ImageData, BlockCount, BlockSize);
}
if (CompressionCode & 8 && ColorDataSize)
{
AtexSubCode5_((unsigned int)OutBuffer + AlphaDataSize2 + AlphaDataSize * 4,
(unsigned int)DcmpBuffer1, (unsigned int)DcmpBuffer2, (unsigned int)&ImageData,
BlockCount, BlockSize, ImageFormat == 0xf);
}
ImageData.DataPos--;
}

unsigned int* DataEnd = InputBuffer + ((HeaderSize + DataSize) >> 2);

if ((AlphaDataSize || AlphaDataSize2) && BlockCount)
{
unsigned int* BufferVar = OutBuffer;

for (int x = 0; x < BlockCount; x++)
{
if (! (DcmpBuffer1[x >> 5] & 1 << x))
{
BufferVar[0] = ImageData.DataPos[0];
BufferVar[1] = ImageData.DataPos[1];
ImageData.DataPos += 2;
}
BufferVar += BlockSize;
}
}

if (ColorDataSize && BlockCount)
{
unsigned int* BufferVar = OutBuffer + AlphaDataSize2 + AlphaDataSize;

for (int x = 0; x < BlockCount; x++)
{
if (! (DcmpBuffer2[x >> 5] & 1 << x))
{
BufferVar[0] = ImageData.DataPos[0];
ImageData.DataPos++;
}
BufferVar += BlockSize;
}

BufferVar = OutBuffer + AlphaDataSize2 + AlphaDataSize + 1;

for (int x = 0; x < BlockCount; x++)
{
if (! (DcmpBuffer2[x >> 5] & 1 << x))
{
BufferVar[0] = ImageData.DataPos[0];
ImageData.DataPos++;
}
BufferVar += BlockSize;
}
}

if (CompressionCode & 0x10 && ImageData.xres == 256 && ImageData.yres == 256 &&
(ImageFormat == 0x10 || ImageFormat == 0x11))
{
AtexSubCode7_((unsigned int)OutBuffer, BlockCount);
}

delete[] (unsigned char*)DcmpBuffer1;
}
#include "AtexDecompress.h"

void __declspec(naked) AtexSubCode1()
{
Expand Down Expand Up @@ -263,12 +59,12 @@ void __declspec(naked) AtexSubCode1()
}
}

void AtexSubCode1_(unsigned int a, unsigned int b, unsigned int c)
void AtexSubCode1_Asm(uint32_t* array1, uint32_t* array2, unsigned int count)
{
__asm {
mov ecx, a
mov edx, b
push c
mov ecx, array1
mov edx, array2
push count
call AtexSubCode1
}
}
Expand Down Expand Up @@ -504,16 +300,15 @@ void __declspec(naked) AtexSubCode2()
}
}

void AtexSubCode2_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f)
void AtexSubCode2_Asm(uint32_t* outBuffer, uint32_t* dcmpBuffer1, uint32_t* dcmpBuffer2, SImageData* imageData, unsigned int blockCount, unsigned int blockSize)
{
__asm {
mov ecx, a
mov edx, b
push f
push e
push d
push c
mov ecx, outBuffer
mov edx, dcmpBuffer1
push blockSize
push blockCount
push imageData
push dcmpBuffer2
call AtexSubCode2
}
}
Expand Down Expand Up @@ -873,16 +668,15 @@ void __declspec(naked) AtexSubCode3()
}
}

void AtexSubCode3_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f)
void AtexSubCode3_Asm(uint32_t* outBuffer, uint32_t* dcmpBuffer1, uint32_t* dcmpBuffer2, SImageData* imageData, unsigned int blockCount, unsigned int blockSize)
{
__asm {
mov ecx, a
mov edx, b
push f
push e
push d
push c
mov ecx, outBuffer
mov edx, dcmpBuffer1
push blockSize
push blockCount
push imageData
push dcmpBuffer2
call AtexSubCode3
}
}
Expand Down Expand Up @@ -1233,20 +1027,20 @@ void __declspec(naked) AtexSubCode4()
}
}

void AtexSubCode4_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f)
void AtexSubCode4_Asm(uint32_t* outBuffer, uint32_t* dcmpBuffer1, uint32_t* dcmpBuffer2, SImageData* imageData, unsigned int blockCount, unsigned int blockSize)
{
__asm {
mov ecx, a
mov edx, b
push f
push e
push d
push c
mov ecx, outBuffer
mov edx, dcmpBuffer1
push blockSize
push blockCount
push imageData
push dcmpBuffer2
call AtexSubCode4
}
}

void AtexSubCode6();
void __declspec(naked) AtexSubCode5()
{
__asm {
Expand Down Expand Up @@ -1534,7 +1328,7 @@ void __declspec(naked) AtexSubCode5()
}
}

void AtexSubCode5_(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
void AtexSubCode5_Asm(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e,
unsigned int f, unsigned int g)
{
__asm {
Expand Down Expand Up @@ -2032,7 +1826,7 @@ void __declspec(naked) AtexSubCode7()
}
}

void AtexSubCode7_(unsigned int a, unsigned int b)
void AtexSubCode7_Asm(unsigned int a, unsigned int b)
{
__asm {
mov ecx, a
Expand Down
23 changes: 8 additions & 15 deletions SourceFiles/AtexAsm.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#pragma once
#pragma once

struct SImageDescriptor
{
int xres, yres;
unsigned char* Data;
int a;
int b;
unsigned char* image;
int imageformat;
int c;
};

int DecompressAtex(int a, int b, int imageformat, int d, int e, int f, int g);
void AtexDecompress(unsigned int* input, unsigned int unknown, unsigned int imageformat,
SImageDescriptor ImageDescriptor, unsigned int* output);
struct SImageData;
void AtexSubCode1_Asm(uint32_t* array1, uint32_t* array2, unsigned int count);
void AtexSubCode2_Asm(uint32_t* outBuffer, uint32_t* dcmpBuffer1, uint32_t* dcmpBuffer2, SImageData* imageData, unsigned int blockCount, unsigned int blockSize);
void AtexSubCode3_Asm(uint32_t* outBuffer, uint32_t* dcmpBuffer1, uint32_t* dcmpBuffer2, SImageData* imageData, unsigned int blockCount, unsigned int blockSize);
void AtexSubCode4_Asm(uint32_t* outBuffer, uint32_t* dcmpBuffer1, uint32_t* dcmpBuffer2, SImageData* imageData, unsigned int blockCount, unsigned int blockSize);
void AtexSubCode5_Asm(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e, unsigned int f, unsigned int g);
void AtexSubCode7_Asm(unsigned int a, unsigned int b);
Loading

0 comments on commit 50357ce

Please sign in to comment.