Skip to content

Commit b05eae6

Browse files
authored
Implement TextureUtil and improve Block::staticCtor (#90)
* Implement `TextureUtil`, Improve `Block::staticCtor` * Update Block.cpp * linter * Update Font.h
1 parent 815e028 commit b05eae6

16 files changed

Lines changed: 2232 additions & 1997 deletions

File tree

data/mcswitch_functions.csv

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23890,10 +23890,10 @@ Address,Quality,Size,Name
2389023890
0x000000710034f2a4,U,000072,_ZN14MappedRegistryI16ResourceLocationPK10SoundEventNSt3__14hashIS0_EENS4_8equal_toIS0_EENS5_IS3_EENS7_IS3_EEEC_gen2
2389123891
0x000000710034f2ec,U,042176,_ZN11SoundEvents10staticCtorEv
2389223892
0x00000071003597ac,U,000164,
23893-
0x0000007100359850,U,000096,registerSound__11SoundEventsSFRCQ2_3std78basic_string__tm__58_wQ2_3std20char_traits__tm__2_wQ2_3std18allocator__tm__2_w
23893+
0x0000007100359850,U,000096,_ZN11SoundEvents13registerSoundERKNSt3__112basic_stringIwNS0_11char_traitsIwEENS0_9allocatorIwEEEE
2389423894
0x00000071003598b0,U,000116,
2389523895
0x0000007100359924,U,001192,_ZN9SoundType10StaticCtorEv
23896-
0x0000007100359dcc,U,000024,
23896+
0x0000007100359dcc,U,000024,_ZN9SoundTypeC1EffPK10SoundEventS2_S2_S2_S2_
2389723897
0x0000007100359de4,U,000008,
2389823898
0x0000007100359dec,U,000008,
2389923899
0x0000007100359df4,U,000008,
@@ -39061,9 +39061,9 @@ Address,Quality,Size,Name
3906139061
0x00000071005f571c,U,000396,
3906239062
0x00000071005f58a8,U,000116,
3906339063
0x00000071005f591c,U,000236,
39064-
0x00000071005f5a08,U,000408,_Z11CreateFileAPKcjjPvjji
39064+
0x00000071005f5a08,U,000408,_Z11CreateFileAPKcjjPvjjS1_
3906539065
0x00000071005f5ba0,U,000008,_Z12GetLastErrorv
39066-
0x00000071005f5ba8,U,000064,_Z11GetFileSizeiPj
39066+
0x00000071005f5ba8,U,000064,_Z11GetFileSizePN2nn2fs10FileHandleEPj
3906739067
0x00000071005f5be8,U,000168,_Z8ReadFileiPvjPjP11_OVERLAPPED
3906839068
0x00000071005f5c90,U,000156,_ZN20CConsoleMinecraftApp14FatalLoadErrorEPKwz
3906939069
0x00000071005f5d2c,U,000024,_Z11CloseHandlei
@@ -45820,19 +45820,19 @@ Address,Quality,Size,Name
4582045820
0x00000071007810d0,U,000012,
4582145821
0x00000071007810dc,U,000020,
4582245822
0x00000071007810f0,U,000020,
45823-
0x0000007100781104,U,000080,_ZN11TextureUtil10decodeJpegEPviPiS1_
45824-
0x0000007100781154,U,000308,_ZN11TextureUtil26insertZlibAlphaChannelDataEPviiPvi
45825-
0x0000007100781288,U,000024,TextureUtil__ByteFlipRGBAToARGB
45826-
0x00000071007812a0,U,000204,TextureUtil__LoadTextureData
45827-
0x000000710078136c,U,000104,_ZN8StbiFile3eofEPv
45823+
0x0000007100781104,O,000080,_ZN11TextureUtil10decodeJpegEPviPiS1_
45824+
0x0000007100781154,M,000308,_ZN11TextureUtil26insertZlibAlphaChannelDataEPviiS0_i
45825+
0x0000007100781288,O,000024,_ZN11TextureUtil18ByteFlipRGBAToARGBERj
45826+
0x00000071007812a0,O,000204,_ZN11TextureUtil15LoadTextureDataEPKcPiS2_PS2_
45827+
0x000000710078136c,O,000104,_ZN8StbiFile4readEPvPci
4582845828
0x00000071007813d4,U,000076,_ZN8StbiFile4skipEPvi
45829-
0x0000007100781420,U,000060,_ZN8StbiFile4readEPvPci
45830-
0x000000710078145c,U,000100,
45831-
0x00000071007814c0,U,000016,
45832-
0x00000071007814d0,U,000004,GetFileSize
45833-
0x00000071007814d4,U,000188,ProcessLoadedTextureData
45834-
0x0000007100781590,U,000020,CloseHandle_thunk
45835-
0x00000071007815a4,U,000076,TextureUtil::LoadTextureData
45829+
0x0000007100781420,M,000060,_ZN8StbiFile3eofEPv
45830+
0x000000710078145c,O,000100,_ZN8StbiFile10CreateFileEPKc
45831+
0x00000071007814c0,O,000016,_ZN8StbiFile11IsValidFileEv
45832+
0x00000071007814d0,O,000004,_ZN8StbiFile3GetEv
45833+
0x00000071007814d4,O,000188,_Z24ProcessLoadedTextureDataPhPiS0_PS0_
45834+
0x0000007100781590,O,000020,_ZN8StbiFile9CloseFileEv
45835+
0x00000071007815a4,O,000076,_ZN11TextureUtil15LoadTextureDataEPhjPiS1_PS1_
4583645836
0x00000071007815f0,O,000660,_ZL15stbi__load_mainP13stbi__contextPiS1_S1_iP17stbi__result_infoi
4583745837
0x0000007100781884,O,000184,_ZL21stbi__convert_16_to_8Ptiii
4583845838
0x000000710078193c,O,000092,_ZL15stbi__jpeg_testP13stbi__context
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "NX/Render/TextureUtil.h"
2+
3+
#include "DecompUtils.h"
4+
#include "java/io/File.h"
5+
#include "zlib.h"
6+
#include <cstring>
7+
8+
#define STB_IMAGE_IMPLEMENTATION
9+
#define STBI_ASSERT(x)
10+
#include "stb_image.h"
11+
12+
void TextureUtil::ByteFlipRGBAToARGB(unsigned int& pixel) {
13+
pixel = (pixel & 0xFF00FF00) | (pixel & 0x000000FF) << 16 | (pixel & 0x00FF0000) >> 16;
14+
}
15+
16+
void TextureUtil::decodeJpeg(void* buffer, int len, int* x, int* y) {
17+
int _x, _y, channelsCount;
18+
if (stbi_load_from_memory((const stbi_uc*)buffer, len, &_x, &_y, &channelsCount, 4)) {
19+
*x = _x;
20+
*y = _y;
21+
}
22+
}
23+
24+
// NON_MATCHING: I don't understand the graphic shit too much so this is shitty impl
25+
void TextureUtil::insertZlibAlphaChannelData(void* data, int width, int height, void* sourceData,
26+
int sourceLen) {
27+
unsigned long decompressedSize = width * height;
28+
unsigned char* alphaData = new unsigned char[decompressedSize];
29+
30+
bool success = false;
31+
32+
if (!uncompress(alphaData, &decompressedSize, (unsigned char*)sourceData, sourceLen)) {
33+
if (width > 0 && height > 0)
34+
success = true;
35+
}
36+
37+
if (success) {
38+
unsigned char* currentAlphaPixel = alphaData;
39+
unsigned char* currentColorRow = (unsigned char*)data;
40+
41+
for (int y = 0; y < height; ++y) {
42+
unsigned char* currentColorPixel = currentColorRow;
43+
44+
for (int x = 0; x < width; ++x) {
45+
unsigned char alphaValue = *currentAlphaPixel;
46+
47+
float alphaFactor = (float)alphaValue / 255.0f;
48+
49+
float blue = (float)currentColorPixel[0];
50+
float green = (float)currentColorPixel[1];
51+
float red = (float)currentColorPixel[2];
52+
53+
currentColorPixel[0] = (unsigned char)decomp_fminf(blue / alphaFactor, 255.0f);
54+
currentColorPixel[1] = (unsigned char)decomp_fminf(green / alphaFactor, 255.0f);
55+
currentColorPixel[2] = (unsigned char)decomp_fminf(red / alphaFactor, 255.0f);
56+
57+
currentColorPixel[3] = alphaValue;
58+
59+
currentColorPixel += 4;
60+
currentAlphaPixel++;
61+
}
62+
currentColorRow += width;
63+
}
64+
}
65+
66+
delete[] alphaData;
67+
}
68+
69+
int ProcessLoadedTextureData(unsigned char* data, int* x, int* y, int** pixels) {
70+
if (!data)
71+
return -1;
72+
size_t size = (*x) * (*y) * 4;
73+
unsigned int* ptr = (unsigned int*)malloc(size);
74+
75+
*pixels = (int*)ptr;
76+
memcpy(ptr, data, size);
77+
78+
stbi_image_free(data);
79+
80+
if ((*x) * (*y) >= 1) {
81+
unsigned int* pixel = (unsigned int*)*pixels;
82+
for (int i = 0; i < (*x) * (*y); i++) {
83+
TextureUtil::ByteFlipRGBAToARGB(*(pixel++));
84+
}
85+
}
86+
return 0;
87+
}
88+
89+
static stbi_io_callbacks sCallbacks;
90+
int TextureUtil::LoadTextureData(const char* fileName, int* x, int* y, int** pixels) {
91+
sCallbacks.read = StbiFile::read;
92+
sCallbacks.skip = StbiFile::skip;
93+
sCallbacks.eof = StbiFile::eof;
94+
95+
StbiFile file;
96+
file.CreateFile(fileName);
97+
98+
int result;
99+
if (file.IsValidFile()) {
100+
int channels = 0;
101+
unsigned char* data = stbi_load_from_callbacks(&sCallbacks, file.Get(), x, y, &channels, 4);
102+
result = ProcessLoadedTextureData(data, x, y, pixels);
103+
} else {
104+
result = -1;
105+
}
106+
107+
file.CloseFile();
108+
return result;
109+
}
110+
111+
int TextureUtil::LoadTextureData(unsigned char* data, unsigned int len, int* x, int* y, int** pixels) {
112+
int channels = 0;
113+
unsigned char* stbi_data = stbi_load_from_memory(data, len, x, y, &channels, 4);
114+
return ProcessLoadedTextureData(stbi_data, x, y, pixels);
115+
}
116+
117+
int StbiFile::read(void* user, char* data, int size) {
118+
unsigned int dataRead = 0;
119+
StbiFile* file = StbiFile::fromUser(user);
120+
if ((ReadFile(file->mFileHandle, data, size, &dataRead, nullptr) & 0x80000000) != 0) {
121+
return -1;
122+
}
123+
file->mDataRead += dataRead;
124+
return dataRead;
125+
}
126+
127+
// this one requires nn sdk symbol which I wont't bother to get
128+
// void StbiFile::skip(void* user, int n) {}
129+
130+
// NON_MATCHING: some weird stuff is going on here, but logic should be the same though
131+
int StbiFile::eof(void* user) {
132+
return StbiFile::fromUser(user)->mFileSize <= StbiFile::fromUser(user)->mDataRead;
133+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#pragma once
2+
3+
#include "java/io/File.h"
4+
#include "net/minecraft/world/ArrayWithLength.h"
5+
#include "nn/fs/fs_types.h"
6+
#include "stb_image.h"
7+
8+
// this class is not part of Minecraft.Client nor Minecraft.World so I put it here, if you find any assert or
9+
// anything move this
10+
class TextureUtil {
11+
public:
12+
static void ByteFlipRGBAToARGB(unsigned int& pixel);
13+
14+
// non existant on Switch, symbol from WiiU
15+
// static int generateTextureId();
16+
// static void releaseTextureId(int id);
17+
// static void bind(int id);
18+
// static void prepareImage(int, int, int, int);
19+
// static void prepareImage(int, int, int);
20+
// static void anaglyph(int);
21+
// static void anaglyph(arrayWithLength<int>);
22+
// static void copyToBuffer(arrayWithLength<int>, int, int);
23+
// static void uploadImageChunked(int, arrayWithLength<int>, int, int, int, int, bool, bool, bool);
24+
// static void uploadImage(int, arrayWithLength<int>, int, int);
25+
26+
static void decodeJpeg(void* buffer, int len, int* x, int* y);
27+
static void insertZlibAlphaChannelData(void* data, int width, int height, void* sourceData,
28+
int sourceLen);
29+
static int LoadTextureData(const char* fileName, int* x, int* y, int** pixels);
30+
static int LoadTextureData(unsigned char* data, unsigned int len, int* x, int* y, int** pixels);
31+
};
32+
33+
class StbiFile {
34+
public:
35+
static int read(void* user, char* data, int size);
36+
static void skip(void* user, int n);
37+
static int eof(void* user);
38+
39+
static StbiFile* fromUser(void* user);
40+
41+
// those 4 names are completly made-up; I don't even know if they should be part of StbiFile
42+
void CreateFile(const char* fileName) {
43+
this->mDataRead = 0;
44+
this->mFileHandle = CreateFileA(fileName, 0x80000000, 0, nullptr, 3, 0x80, 0);
45+
46+
this->mFileSize
47+
= this->mFileHandle == (nn::fs::FileHandle*)-1 ? 0 : GetFileSize(this->mFileHandle, nullptr);
48+
}
49+
50+
void CloseFile() {
51+
if (this->mFileHandle != (nn::fs::FileHandle*)-1)
52+
CloseHandle(this->mFileHandle);
53+
}
54+
55+
bool IsValidFile() { return this->mFileHandle != (nn::fs::FileHandle*)-1; }
56+
57+
StbiFile* Get() { return this; }
58+
59+
nn::fs::FileHandle* mFileHandle;
60+
int mFileSize;
61+
int mDataRead;
62+
};

src/DecompUtils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
// this can be depracated if musl is fixed
4+
5+
inline __attribute__((__always_inline__)) float decomp_fmax(float x, float y) {
6+
__asm__("fmaxnm %s0, %s1, %s2" : "=w"(x) : "w"(x), "w"(y));
7+
return x;
8+
}
9+
10+
inline __attribute__((__always_inline__)) float decomp_fminf(float x, float y) {
11+
__asm__("fminnm %s0, %s1, %s2" : "=w"(x) : "w"(x), "w"(y));
12+
return x;
13+
}

src/Minecraft.Client/net/minecraft/client/gui/Font.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ class Options;
66
class Textures;
77
class ResourceLocation;
88
class BufferBuilder;
9+
class Random;
910

1011
class Font {
1112
public:
13+
static ResourceLocation sDefaultFontRsrc;
14+
static ResourceLocation sAlternateFontRsrc;
15+
static unsigned short sDefaultText[644];
16+
1217
Font(Options*, const std::wstring&, Textures*, bool, ResourceLocation*, int, int, int, int,
1318
unsigned short* renderList);
1419
void cacheCharacter(int);
@@ -19,8 +24,18 @@ class Font {
1924
std::wstring sanitize(const std::wstring&, bool);
2025
int width(const std::wstring&);
2126

22-
static ResourceLocation sDefaultFontRsrc;
23-
static ResourceLocation sAlternateFontRsrc;
24-
unsigned char padding[0xE8];
25-
static unsigned short sDefaultText[644];
27+
void* font0;
28+
void* mCachedCharacters;
29+
char gap10[8];
30+
Random* mRandom;
31+
char gap20[128];
32+
Textures* mTextures;
33+
void* qwordA8;
34+
uint16_t wordB0;
35+
int mHorizontalCharsCount;
36+
int mVerticalCharsCount;
37+
int mCharWidth;
38+
int mCharHeight;
39+
ResourceLocation* mFontTextureResourceLoc;
40+
char filler[22];
2641
};

src/Minecraft.Client/net/minecraft/client/renderer/TextureUtil.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Minecraft.Client/net/minecraft/client/renderer/TextureUtil.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Minecraft.Client/net/minecraft/client/renderer/platform/GlStateManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,6 @@ class GlStateManager {
196196
static void disableState(int state);
197197
static void enableState(int state);
198198
static TexGenCoord* getTexGen(GlStateManager::TexGen coord);
199+
static int genTexture();
200+
static void deleteTexture(int texture);
199201
};

src/Minecraft.Client/net/minecraft/core/Direction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class Vec3i;
1010

1111
class Direction : public StringRepresentable {
1212
public:
13+
class Plane : public Predicate<const Direction*> {
14+
public:
15+
static Plane* HORIZONTAL;
16+
static Plane* VERTICAL;
17+
18+
Plane();
19+
};
1320
class AxisDirection {
1421
public:
1522
static AxisDirection* POSITIVE;

src/Minecraft.World/java/io/File.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "nn/fs/fs_types.h"
34
#include <string>
45
#include <vector>
56

@@ -50,7 +51,9 @@ bool isDirectory(const std::wstring&);
5051
bool FindFirstFileA(const char*, _WIN32_FIND_DATAA*);
5152
std::wstring filenametowstring(const char*);
5253
bool FindNextFileA(int, _WIN32_FIND_DATAA*);
53-
bool CloseHandle(int);
54-
bool CreateFileA(const char*, unsigned int, unsigned int, void*, unsigned int, unsigned int, int);
55-
bool GetFileSize(int, unsigned int*);
56-
bool ReadFile(int, void*, unsigned int, unsigned int*, _OVERLAPPED*);
54+
bool CloseHandle(nn::fs::FileHandle*);
55+
// NOTE: this function declaration may be wrong as it seems that last argument differs with WiiU declaration
56+
nn::fs::FileHandle* CreateFileA(const char*, unsigned int, unsigned int, void*, unsigned int, unsigned int,
57+
void*);
58+
int GetFileSize(nn::fs::FileHandle*, unsigned int*);
59+
int ReadFile(nn::fs::FileHandle*, void*, unsigned int, unsigned int*, _OVERLAPPED*);

0 commit comments

Comments
 (0)