File: textureset.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (202 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/stark/resources/textureset.h"

#include "engines/stark/debug.h"

#include "engines/stark/formats/dds.h"
#include "engines/stark/formats/tm.h"
#include "engines/stark/formats/xrc.h"

#include "engines/stark/gfx/driver.h"
#include "engines/stark/gfx/texture.h"

#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/settings.h"

#include "common/file.h"
#include "common/compression/unzip.h"
#include "image/png.h"

namespace Stark {
namespace Resources {

TextureSet::~TextureSet() {
	delete _textureSet;
}

TextureSet::TextureSet(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Object(parent, subType, index, name),
		_textureSet(nullptr) {
	_type = TYPE;
}

Gfx::TextureSet *TextureSet::getTexture() {
	return _textureSet;
}

void TextureSet::readData(Formats::XRCReadStream *stream) {
	_filename = Common::Path(stream->readString());
	_archiveName = stream->getArchiveName();
}

void TextureSet::onPostRead() {
	if (StarkSettings->isAssetsModEnabled() && StarkGfx->supportsModdedAssets()) {
		_textureSet = readOverrideDdsArchive();
	}

	if (!_textureSet) {
		ArchiveReadStream *stream = StarkArchiveLoader->getFile(_filename, _archiveName);

		_textureSet = Formats::TextureSetReader::read(stream);

		delete stream;
	}
}

static Common::String stripExtension(const Common::String &filename) {
	if (filename.hasSuffixIgnoreCase(".bmp")) {
		return Common::String(filename.c_str(), filename.size() - 4);
	}
	return filename;
}

void TextureSet::extractArchive() {
	ArchiveReadStream *stream = StarkArchiveLoader->getFile(_filename, _archiveName);
	Formats::BiffArchive *archive = Formats::TextureSetReader::readArchive(stream);

	Common::Array<Formats::Texture *> textures = archive->listObjectsRecursive<Formats::Texture>();
	for (uint i = 0; i < textures.size(); i++) {
		Common::Path filename(Common::String::format(
		            "dump/%s/%s.png",
		            _filename.baseName().c_str(),
		            stripExtension(textures[i]->getName()).c_str()));

		if (Common::File::exists(filename)) {
			continue;
		}

		Common::DumpFile out;
		if (!out.open(filename, true)) {
			warning("Unable to open file '%s' for writing", filename.toString().c_str());
			return;
		}

		Graphics::Surface *surface = textures[i]->getSurface();

		Image::writePNG(out, *surface);

		out.close();

		surface->free();
		delete surface;
	}

	delete archive;
	delete stream;
}

Gfx::TextureSet *TextureSet::readOverrideDdsArchive() {
	Common::Path archiveName = _filename.append(".zip");

	debugC(kDebugModding, "Attempting to load %s", archiveName.toString().c_str());

	Common::Archive *archive = Common::makeZipArchive(archiveName);
	if (!archive) {
		return nullptr;
	}

	Common::ArchiveMemberList files;
	archive->listMatchingMembers(files, "*.dds");
	if (files.empty()) {
		warning("No DDS files found in archive %s", archiveName.toString().c_str());
		delete archive;
		return nullptr;
	}

	uint loadedCount = 0;
	Gfx::TextureSet *textureSet = new Gfx::TextureSet();

	for (Common::ArchiveMemberList::const_iterator it = files.begin(); it != files.end(); it++) {
		const Common::String &name = (*it)->getName();

		debugC(kDebugModding, "Attempting to load texture %s", name.c_str());

		Common::SeekableReadStream *ddsStream = (*it)->createReadStream();
		if (!ddsStream) {
			warning("Unable to open %s for reading in %s", (*it)->getName().c_str(), archiveName.toString().c_str());
			continue;
		}

		Formats::DDS dds;
		if (!dds.load(*ddsStream, name + " in " + archiveName.toString('/'))) {
			delete ddsStream;
			continue;
		}

		const Formats::DDS::MipMaps &mipmaps = dds.getMipMaps();
		if (mipmaps.empty()) {
			warning("No mipmaps in %s", name.c_str());
			delete ddsStream;
			continue;
		}

		Gfx::Texture *texture = StarkGfx->createTexture();
		texture->setLevelCount(mipmaps.size());
		for (uint i = 0; i < mipmaps.size(); i++) {
			texture->addLevel(i, &mipmaps[i]);
		}

		// Remove the .dds extension, add .bmp to match the names
		// used by the models.
		Common::String textureName = Common::String(name.c_str(), name.size() - 4);

		// Fix for loading one of Emma's textures in mods caused by inconsistent zip filename encoding
		//  The original game uses a 1 byte character (\xe6 which is ae in CP1252)
		//	\x91 is ae in CP437 and CP850 which is used by some compression utilities
		//	\xc3\xa6 is the UTF-8 2-byte representation
		// Both get converted to the game's original encoding here
		if (textureName == "pupp\x91r" || textureName == "pupp\xc3\xa6r") {
			textureName = "pupp\xe6r";
		}

		textureSet->addTexture(textureName + ".bmp", texture);

		delete ddsStream;

		loadedCount++;
	}

	debugC(kDebugModding, "Loaded %d textures from %s", loadedCount, archiveName.toString().c_str());

	delete archive;

	return textureSet;
}

void TextureSet::printData() {
	debug("filename: %s", _filename.toString().c_str());
}

} // End of namespace Resources
} // End of namespace Stark