summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/minecraft/skins/SkinModel.cpp
blob: 875399197fa5b77c8973a07af5ad9ec080c4a23d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  Copyright (C) 2026 Project Tick
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * === Upstream License Block (Do Not Modify) ==============================
 *
 *
 *
 *  Prism Launcher - Minecraft Launcher
 *  Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ======================================================================== */

#include "SkinModel.h"
#include <QFileInfo>
#include <QPainter>

#include "FileSystem.h"
#include "Json.h"

static QImage improveSkin(QImage skin)
{
	int height = skin.height();
	int width	= skin.width();
	if (width != 64 || (height != 32 && height != 64))
	{
		return skin;
	}

	// It seems some older skins may use this format, which can't be drawn onto
	// https://github.com/PrismLauncher/PrismLauncher/issues/4032
	// https://doc.qt.io/qt-6/qpainter.html#begin
	if (skin.format() <= QImage::Format_Indexed8 || !skin.hasAlphaChannel())
	{
		skin = skin.convertToFormat(QImage::Format_ARGB32);
	}
	if (skin.size() == QSize(64, 32))
	{
		// old format
		auto newSkin = QImage(QSize(64, 64), skin.format());
		newSkin.fill(Qt::transparent);
		QPainter p(&newSkin);
		p.drawImage(QPoint(0, 0), skin.copy(QRect(0, 0, 64, 32))); // copy head

		auto leg = skin.copy(QRect(0, 16, 16, 16));
		p.drawImage(QPoint(16, 48), leg); // copy leg

		auto arm = skin.copy(QRect(40, 16, 16, 16));
		p.drawImage(QPoint(32, 48), arm); // copy arm
		return newSkin;
	}
	return skin;
}
static QImage getSkin(const QString path)
{
	return improveSkin(QImage(path));
}

static QImage generatePreviews(QImage texture, bool slim)
{
	QImage preview(36, 36, QImage::Format_ARGB32);
	preview.fill(Qt::transparent);
	QPainter paint(&preview);

	// head
	paint.drawImage(4, 2, texture.copy(8, 8, 8, 8));
	paint.drawImage(4, 2, texture.copy(40, 8, 8, 8));
	// torso
	paint.drawImage(4, 10, texture.copy(20, 20, 8, 12));
	paint.drawImage(4, 10, texture.copy(20, 36, 8, 12));
	// right leg
	paint.drawImage(4, 22, texture.copy(4, 20, 4, 12));
	paint.drawImage(4, 22, texture.copy(4, 36, 4, 12));
	// left leg
	paint.drawImage(8, 22, texture.copy(4, 52, 4, 12));
	paint.drawImage(8, 22, texture.copy(20, 52, 4, 12));

	auto armWidth = slim ? 3 : 4;
	auto armPosX  = slim ? 1 : 0;
	// right arm
	paint.drawImage(armPosX, 10, texture.copy(44, 20, armWidth, 12));
	paint.drawImage(armPosX, 10, texture.copy(44, 36, armWidth, 12));
	// left arm
	paint.drawImage(12, 10, texture.copy(36, 52, armWidth, 12));
	paint.drawImage(12, 10, texture.copy(52, 52, armWidth, 12));

	// back
	// head
	paint.drawImage(24, 2, texture.copy(24, 8, 8, 8));
	paint.drawImage(24, 2, texture.copy(56, 8, 8, 8));
	// torso
	paint.drawImage(24, 10, texture.copy(32, 20, 8, 12));
	paint.drawImage(24, 10, texture.copy(32, 36, 8, 12));
	// right leg
	paint.drawImage(24, 22, texture.copy(12, 20, 4, 12));
	paint.drawImage(24, 22, texture.copy(12, 36, 4, 12));
	// left leg
	paint.drawImage(28, 22, texture.copy(12, 52, 4, 12));
	paint.drawImage(28, 22, texture.copy(28, 52, 4, 12));

	// right arm
	paint.drawImage(armPosX + 20, 10, texture.copy(48 + armWidth, 20, armWidth, 12));
	paint.drawImage(armPosX + 20, 10, texture.copy(48 + armWidth, 36, armWidth, 12));
	// left arm
	paint.drawImage(32, 10, texture.copy(40 + armWidth, 52, armWidth, 12));
	paint.drawImage(32, 10, texture.copy(56 + armWidth, 52, armWidth, 12));

	return preview;
}
SkinModel::SkinModel(QString path) : m_path(path), m_texture(getSkin(path)), m_model(Model::CLASSIC)
{
	m_preview = generatePreviews(m_texture, false);
}

SkinModel::SkinModel(QDir skinDir, QJsonObject obj)
	: m_capeId(Json::ensureString(obj, "capeId")),
	  m_model(Model::CLASSIC),
	  m_url(Json::ensureString(obj, "url"))
{
	auto name = Json::ensureString(obj, "name");

	if (auto model = Json::ensureString(obj, "model"); model == "SLIM")
	{
		m_model = Model::SLIM;
	}
	m_path	  = skinDir.absoluteFilePath(name) + ".png";
	m_texture = getSkin(m_path);
	m_preview = generatePreviews(m_texture, m_model == Model::SLIM);
}

QString SkinModel::name() const
{
	return QFileInfo(m_path).completeBaseName();
}

bool SkinModel::rename(QString newName)
{
	auto info	  = QFileInfo(m_path);
	auto new_path = FS::PathCombine(info.absolutePath(), newName + ".png");
	if (QFileInfo::exists(new_path))
	{
		return false;
	}
	m_path = new_path;
	return FS::move(info.absoluteFilePath(), m_path);
}

QJsonObject SkinModel::toJSON() const
{
	QJsonObject obj;
	obj["name"]	  = name();
	obj["capeId"] = m_capeId;
	obj["url"]	  = m_url;
	obj["model"]  = getModelString();
	return obj;
}

QString SkinModel::getModelString() const
{
	switch (m_model)
	{
		case CLASSIC: return "CLASSIC";
		case SLIM: return "SLIM";
	}
	return {};
}

bool SkinModel::isValid() const
{
	return !m_texture.isNull() && (m_texture.size().height() == 32 || m_texture.size().height() == 64)
		&& m_texture.size().width() == 64;
}
void SkinModel::refresh()
{
	m_texture = getSkin(m_path);
	m_preview = generatePreviews(m_texture, m_model == Model::SLIM);
}
void SkinModel::setModel(Model model)
{
	m_model	  = model;
	m_preview = generatePreviews(m_texture, m_model == Model::SLIM);
}