MinecraftConsoles/Minecraft.Client/FolderTexturePack.cpp
2026-03-21 22:21:17 -07:00

229 lines
6 KiB
C++

#include "stdafx.h"
#include "FolderTexturePack.h"
#include "TexturePackRepository.h"
#include "..\Minecraft.World\StringHelpers.h"
FolderTexturePack::FolderTexturePack(DWORD id, const wstring &name, File *folder, TexturePack *fallback) : AbstractTexturePack(id, folder, name, fallback)
{
// 4J Stu - These calls need to be in the most derived version of the class
cacheFiles(*folder, folder->getPath().length() + 1);
loadColourTable();
loadIcon();
loadName();
loadDescription();
checkTexSize();
bUILoaded = false;
}
DLCPack* FolderTexturePack::getDLCPack()
{
return NULL;
}
void FolderTexturePack::loadName() {
texname = file->getName();
}
void FolderTexturePack::loadIcon()
{
if (hasFile(L"pack.png") && _iconData.empty())
{
app.DebugPrintf("Found pack.png in folder, loading as icon\n");
File iconFile(file->getPath() + L"\\pack.png");
FileInputStream is(iconFile);
_iconData.resize(iconFile.length());
byteArray arr(iconFile.length());
is.read(arr, 0, _iconData.size());
memcpy(_iconData.data(), arr.data, _iconData.size());
delete arr.data;
if (!_iconData.empty()) {
iconImage = new BufferedImage(_iconData.data(), _iconData.size());
m_iconData = _iconData.data();
m_iconSize = _iconData.size();
}
else
app.DebugPrintf("Failed to load pack.png from folder\n");
}
}
void FolderTexturePack::loadDescription()
{
desc1 = L"";
desc2 = L"";
}
bool FolderTexturePack::hasData()
{
return _cachedFiles.find(L"pack.mcmeta") != _cachedFiles.end();
}
bool FolderTexturePack::isLoadingData()
{
return false;
}
void FolderTexturePack::cacheFiles(File &dir, int folderLen)
{
int i = 0;
for (auto file : *dir.listFiles()) {
if (i++ < 2) continue; // skip `.` and `..`
if (file->isDirectory()) {
cacheFiles(*file, folderLen);
} else {
_cachedFiles[file->getPath().substr(folderLen)] = file->getPath().substr(folderLen);
_cachedFiles[file->getName()] = file->getPath().substr(folderLen);
}
}
}
wstring FolderTexturePack::getAnimationString(const wstring& textureName, const wstring& path, bool allowFallback) {
// copy paste of base class method, except I return null and check for existence in getResource
std::wstring result = L"";
InputStream* fileStream = getResource(L"\\" + path + textureName + L".txt", true);
if (fileStream) {
InputStreamReader isr(fileStream);
BufferedReader br(&isr);
wstring line = br.readLine();
while (!line.empty())
{
line = trimString(line);
if (line.length() > 0)
{
result.append(L",");
result.append(line);
}
line = br.readLine();
}
delete fileStream;
}
return result;
}
InputStream *FolderTexturePack::getResourceImplementation(const wstring &name) //throws IOException
{
// Make the content package point to to the UPDATE: drive is needed
std::wstring wDrive = this->file->getPath();
if (!hasFile(name))
return nullptr;
InputStream *resource = InputStream::getResourceAsStream(name);
//InputStream *stream = DefaultTexturePack::class->getResourceAsStream(name);
//if (stream == nullptr)
//{
// throw new FileNotFoundException(name);
//}
//return stream;
return resource;
}
BufferedImage* FolderTexturePack::getImageResource(const wstring& file, bool filenameHasExtension, bool bTitleUpdateTexture, const wstring& drive) {
app.DebugPrintf("FolderPack: image - %ls\n", file.c_str());
if (file == L"terrain.png" && terrainAtlas.get() != nullptr)
return terrainAtlas.release();
if (file == L"items.png" && itemAtlas.get() != nullptr)
return itemAtlas.release();
std::wstring f = file;
auto it = AbstractTexturePack::INDEXED_TO_JAVA_MAP.find(L"res/" + file);
if (it != INDEXED_TO_JAVA_MAP.end()) {
f = L"assets/minecraft/textures/" + it->second;
}
f = replaceAll(f, L"/", L"\\");
if (hasFile(f)) {
f = _cachedFiles[f];
File img(this->file->getPath() + L"\\" + f);
FileInputStream is(img);
byteArray arr(img.length());
is.read(arr, 0, img.length());
return new BufferedImage(arr.data, arr.length);
}
app.DebugPrintf("FolderPack: failed to find %ls fetching from default\n", f.c_str());
TexturePackRepository* repo = Minecraft::GetInstance()->skins;
return repo->getDefault()->getImageResource(file, filenameHasExtension, bTitleUpdateTexture);
}
bool FolderTexturePack::hasFile(const wstring &name)
{
std::wstring f = name;
if (f.find(L"/") != f.npos)
for (int i = 0; i < f.length(); i++)
f[i] == L'/' ? f[i] = L'\\' : 0; // putting zero there just works ig
return _cachedFiles.find(f) != _cachedFiles.end();
}
bool FolderTexturePack::isTerrainUpdateCompatible()
{
#if 0
final File dir = new File(this.file, "textures/");
final boolean hasTexturesFolder = dir.exists() && dir.isDirectory();
final boolean hasOldFiles = hasFile("terrain.png") || hasFile("gui/items.png");
return hasTexturesFolder || !hasOldFiles;
#endif
return true;
}
wstring FolderTexturePack::getPath(bool bTitleUpdateTexture /*= false*/,const char *pchBDPatchFilename)
{
wstring wDrive;
#ifdef _XBOX
wDrive=L"GAME:\\" + file->getPath() + L"\\";
#else
wDrive=L"Common\\" + file->getPath() + L"\\";
#endif
return wDrive;
}
void FolderTexturePack::loadUI()
{
#ifdef _XBOX
//"file://" + Drive + PathToXZP + "#" + PathInsideXZP
//L"file://game:/ui.xzp#skin_default.xur"
// Load new skin
if(hasFile(L"TexturePack.xzp"))
{
const DWORD LOCATOR_SIZE = 256; // Use this to allocate space to hold a ResourceLocator string
WCHAR szResourceLocator[ LOCATOR_SIZE ];
swprintf(szResourceLocator, LOCATOR_SIZE,L"file://%lsTexturePack.xzp#skin_Minecraft.xur",getPath().c_str());
XuiFreeVisuals(L"");
app.LoadSkin(szResourceLocator,nullptr);//L"TexturePack");
bUILoaded = true;
//CXuiSceneBase::GetInstance()->SetVisualPrefix(L"TexturePack");
}
AbstractTexturePack::loadUI();
#endif
}
void FolderTexturePack::unloadUI()
{
#ifdef _XBOX
// Unload skin
if(bUILoaded)
{
XuiFreeVisuals(L"TexturePack");
XuiFreeVisuals(L"");
CXuiSceneBase::GetInstance()->SetVisualPrefix(L"");
CXuiSceneBase::GetInstance()->SkinChanged(CXuiSceneBase::GetInstance()->m_hObj);
}
AbstractTexturePack::unloadUI();
#endif
}