Skip to content

Commit

Permalink
Some texture SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
FreezeEngine committed Aug 7, 2024
1 parent 15d6d4f commit ca615dc
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/Client/Events/Render/DrawImageEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "../Event.hpp"
#include "../Cancellable.hpp"
#include "../../../Utils/Utils.hpp"
#include "../../../SDK/Client/Render/Texture2D.hpp"
#include "../../../SDK/Client/Render/TexturePtr.hpp"

class DrawImageEvent : public Event, Cancellable {
private:
TextureData *texturePtr;
TexturePtr *texturePtr;
Vec2<float> &imagePos;
public:
[[nodiscard]] Vec2<float> &getImagePos() const {
Expand All @@ -18,7 +18,7 @@ class DrawImageEvent : public Event, Cancellable {
return this->texturePtr->GetFilePath();
}

explicit DrawImageEvent(TextureData *texturePtr, Vec2<float> &imagePos)
explicit DrawImageEvent(TexturePtr *texturePtr, Vec2<float> &imagePos)
: texturePtr(texturePtr), imagePos(imagePos) {
}
};
6 changes: 3 additions & 3 deletions src/Client/Hook/Hooks/Render/SetupAndRenderHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SetUpAndRenderHook : public Hook

static void drawImageDetour(
MinecraftUIRenderContext* _this,
TextureData* texturePtr,
TexturePtr* texturePtr,
Vec2<float>& imagePos,
Vec2<float>& imageDimension,
Vec2<float>& uvPos,
Expand All @@ -40,7 +40,7 @@ class SetUpAndRenderHook : public Hook
DrawImageEvent event(texturePtr, imagePos);
EventHandler::onDrawImage(event);

Memory::CallFunc<void*, MinecraftUIRenderContext*, TextureData*, Vec2<float>&, Vec2<float>&, Vec2<float>&, Vec2<float>&>(
Memory::CallFunc<void*, MinecraftUIRenderContext*, TexturePtr*, Vec2<float>&, Vec2<float>&, Vec2<float>&, Vec2<float>&>(
oDrawImage,
_this,
texturePtr,
Expand All @@ -67,7 +67,7 @@ class SetUpAndRenderHook : public Hook
static void setUpAndRenderCallback(ScreenView* pScreenView, MinecraftUIRenderContext* muirc) {

SDK::screenView = pScreenView;
SDK::clientInstance = muirc->getclientInstance();
SDK::clientInstance = muirc->getClientInstance();
SDK::hasInstanced = true;

if(funcOriginalText == nullptr || oDrawImage == nullptr)
Expand Down
36 changes: 36 additions & 0 deletions src/SDK/Client/Render/BedrockTexture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <memory>
#include "BedrockTextureData.hpp"

struct BedrockTexture {
public:
std::shared_ptr<BedrockTextureData> bedrockTextureData;

BedrockTexture() = default;

BedrockTexture(const BedrockTextureData& data) {
bedrockTextureData = std::make_shared<BedrockTextureData>(data);
}

void unload() {
if(bedrockTextureData != nullptr) {
bedrockTextureData.reset();

int newState = bedrockTextureData->textureLoadState & ~TextureLoadState::LoadedBit;
bedrockTextureData->textureLoadState = static_cast<TextureLoadState>(newState);

bedrockTextureData->isMissingTexture = IsMissingTexture::No;
}
}

bool operator==(const BedrockTexture& other) const {
if(bedrockTextureData == other.bedrockTextureData) return true;
return bedrockTextureData->clientTexture == other.bedrockTextureData->clientTexture;
}

bool operator!=(const BedrockTexture& other) const {
if(bedrockTextureData != other.bedrockTextureData) return true;
return bedrockTextureData->clientTexture != other.bedrockTextureData->clientTexture;
}
};
22 changes: 22 additions & 0 deletions src/SDK/Client/Render/BedrockTextureData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "TexturePtr.hpp"
#include "Textures/mceTextureDescription.hpp"
#include "Textures/ClientTexture.hpp"
#include "Textures/IsMissingTexture.hpp"
#include "Textures/TextureLoadState.hpp"
#include "Textures/TextureSetImageDescription.hpp"

struct BedrockTextureData {
public:
mce::ClientTexture clientTexture;
mce::TextureDescription textureDescription;
IsMissingTexture isMissingTexture;
TextureLoadState textureLoadState;
cg::TextureSetImageDescription textureSetDescription;

~BedrockTextureData() {
clientTexture.resourcePointerBlock.reset();
textureSetDescription.layerInfo.clear();
};
};
73 changes: 70 additions & 3 deletions src/SDK/Client/Render/MinecraftUIRenderContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,77 @@
#include "../Container/Inventory.hpp"
#include "../../../Utils/Utils.hpp"
#include "../../../Client/GUI/D2D.hpp"
#include "Texture2D.hpp"
#include "TexturePtr.hpp"
#include "TextureGroup.hpp"
#include "../../../Client/Module/Manager.hpp"

class MinecraftUIRenderContext {
public:
BUILD_ACCESS(this, ClientInstance*, clientInstance, GET_OFFSET("MinecraftUIRenderContext::clientInstance"));
BUILD_ACCESS(this, class ScreenContext*, screenContext, GET_OFFSET("MinecraftUIRenderContext::screenContext"));
ClientInstance* getClientInstance() {
return hat::member_at<ClientInstance*>(this, GET_OFFSET("MinecraftUIRenderContext::clientInstance"));
}

class ScreenContext* getScreenContext() {
return hat::member_at<ScreenContext*>(this, GET_OFFSET("MinecraftUIRenderContext::screenContext"));
}

std::shared_ptr<TextureGroup>& getTextureGroup() {
return hat::member_at<std::shared_ptr<TextureGroup>>(this, GET_OFFSET("MinecraftUIRenderContext::textures"));
}

std::map<ResourceLocation, BedrockTexture>& getTextures() {
return getTextureGroup()->getLoadedTextures();
}

TexturePtr* getTexture(TexturePtr* ptr, const ResourceLocation& location, bool forceReload) {
using getTextureFunc = TexturePtr*(__fastcall*)(MinecraftUIRenderContext*, TexturePtr*, const ResourceLocation&, bool);
static auto getTextureOriginal = reinterpret_cast<getTextureFunc>(Memory::findSig(GET_SIG("MinecraftUIRenderContext::getTexture")));
return getTextureOriginal(this, ptr, location, forceReload);
}

TexturePtr* createTexture(const std::string& path, bool external, bool forceReload) {
const auto resource = ResourceLocation(path, external);
TexturePtr texture = TexturePtr();
return getTexture(&texture, resource, forceReload);
}

TexturePtr* createTexture(const ResourceLocation& location, bool forceReload) {
TexturePtr texture = TexturePtr();
return getTexture(&texture, location, forceReload);
}

void reloadTexture(const ResourceLocation& location) {
this->createTexture(location, true);
}

void reloadAllTextures() {
auto& textures = this->getTextures();

for (auto& [resourceLocation, texture] : textures) {
texture.unload();
reloadTexture(resourceLocation);
}
}

void swapTextures(ResourceLocation& from, ResourceLocation& to) {
auto& textures = this->getTextures();

// only reload if texture is not loaded
if(!textures.contains(from)) {
reloadTexture(from);
}

if(!textures.contains(to)) {
reloadTexture(to);
}

if(textures.contains(from) && textures.contains(to) && textures[from] != textures[to]) {
if(textures[to].bedrockTextureData->textureLoadState == TextureLoadState::LoadedBit) {
textures[from] = textures[to];
} else {
auto texture = createTexture(to, false);
textures[from] = *texture->clientTexture;
}
}
}
};
67 changes: 67 additions & 0 deletions src/SDK/Client/Render/ResourceLocation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <string>

enum ResourceFileSystem : int32_t {
UserPackage = 0x0000,
AppPackage = 0x0001,
Raw = 0x0002,
RawPersistent = 0x0003,
SettingsDir = 0x0004,
ExternalDir = 0x0005,
ServerPackage = 0x0006,
DataDir = 0x0007,
UserDir = 0x0008,
ScreenshotsDir = 0x0009,
StoreCache = 0x000a,
Invalid = 0x000b,
};

class ResourceLocation {
public:
ResourceFileSystem fileSystem;
std::string filePath;
uint64_t pathHash{};
uint64_t fullHash{};

ResourceLocation(const std::string& filePath, bool external) {
this->filePath = filePath;
if (external)
this->fileSystem = ResourceFileSystem::Raw;
else this->fileSystem = ResourceFileSystem::UserPackage;

_computeHashes();
};

bool operator==(const ResourceLocation& other) const {
if(this->fileSystem != other.fileSystem || this->pathHash != other.pathHash) return false;
return this->filePath == other.filePath;
}

bool operator<(const ResourceLocation& other) const {
return this->filePath < other.filePath;
}

private:
void _computeHashes()
{
const uint64_t FNV_OFFSET_BASIS = 0xCBF29CE484222325u;
const uint64_t FNV_PRIME = 0x100000001B3u;

uint64_t _pathHash = FNV_OFFSET_BASIS;
if (!this->filePath.empty()) {
for (char c : this->filePath) {
_pathHash *= FNV_PRIME;
_pathHash ^= c;
}
} else {
_pathHash = 0;
}

uint64_t fileSystemHash = FNV_OFFSET_BASIS ^ static_cast<uint8_t>(this->fileSystem);
fileSystemHash *= FNV_PRIME;

this->pathHash = _pathHash;
this->fullHash = _pathHash ^ fileSystemHash;
}
};
19 changes: 0 additions & 19 deletions src/SDK/Client/Render/Texture2D.hpp

This file was deleted.

14 changes: 14 additions & 0 deletions src/SDK/Client/Render/TextureGroup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <map>
#include "ResourceLocation.hpp"
#include "../../../Utils/Memory/Game/SignatureAndOffsetManager.hpp"
#include "libhat/Access.hpp"
#include "BedrockTexture.hpp"

class TextureGroup {
public:
std::map<ResourceLocation, BedrockTexture>& getLoadedTextures() {
return hat::member_at<std::map<ResourceLocation, BedrockTexture>>(this, GET_OFFSET("TextureGroup::base") + GET_OFFSET("TextureGroup::loadedTextures"));
}
};
19 changes: 19 additions & 0 deletions src/SDK/Client/Render/TexturePtr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <string>
#include <utility>
#include "ResourceLocation.hpp"
#include "BedrockTextureData.hpp"


class TexturePtr {
public:
std::shared_ptr<BedrockTextureData> clientTexture;
std::shared_ptr<ResourceLocation> resourceLocation;

TexturePtr() = default;

[[nodiscard]] std::string GetFilePath() const {
return resourceLocation->filePath;
}
};
17 changes: 17 additions & 0 deletions src/SDK/Client/Render/Textures/BindFlagsBit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <cstdint>

namespace mce {
enum BindFlagsBit : uint32_t {
NoBindFlags = 0x0000,
VertexBufferBit = 0x0001,
IndexBufferBit = 0x0002,
ConstantBufferBit = 0x0004,
ShaderResourceBit = 0x0008,
StreamOutputBit = 0x0010,
RenderTargetBit = 0x0020,
DepthStencilBit = 0x0040,
UnorderedAccessBit = 0x0080,
};
}
23 changes: 23 additions & 0 deletions src/SDK/Client/Render/Textures/ClientTexture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <memory>
#include <variant>
#include <string>

namespace mce {
class ClientTexture {
public:
std::shared_ptr<class data> resourcePointerBlock;
public:
virtual ~ClientTexture() = default;

bool operator==(const ClientTexture& other) const {
return resourcePointerBlock == other.resourcePointerBlock;
}

bool operator!=(const ClientTexture& other) const {
return resourcePointerBlock != other.resourcePointerBlock;
}
};
};

10 changes: 10 additions & 0 deletions src/SDK/Client/Render/Textures/Color.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace mce {
class Color { // MCCColor
float r;
float g;
float b;
float a;
};
}
5 changes: 5 additions & 0 deletions src/SDK/Client/Render/Textures/ColorChannel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

class ColorChannel {
float value;
};
13 changes: 13 additions & 0 deletions src/SDK/Client/Render/Textures/ImageDescription.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <cstdint>
#include "TextureFormat.hpp"

namespace cg {
struct ImageDescription {
uint32_t width;
uint32_t height;
mce::TextureFormat textureFormat;
uint32_t arraySize;
};
}
Loading

0 comments on commit ca615dc

Please sign in to comment.