blob: f5870b30abe5613e6bef2601e9c98aa105bdc04c (
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
|
#include "2s2h/resource/importer/TextureAnimationFactory.h"
#include "2s2h/resource/type/TextureAnimation.h"
#include "gbi.h"
#include <spdlog/spdlog.h>
namespace SOH {
std::shared_ptr<Ship::IResource>
ResourceFactoryBinaryTextureAnimationV0::ReadResource(std::shared_ptr<Ship::File> file,
std::shared_ptr<Ship::ResourceInitData> initData) {
if (!FileHasValidFormatAndReader(file, initData)) {
return nullptr;
}
auto tAnim = std::make_shared<TextureAnimation>(initData);
auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);
const size_t numEntries = reader->ReadUInt32();
// `e` is a void* and must be allocated and freed with malloc/free
for (size_t i = 0; i < numEntries; i++) {
AnimatedMaterial anim;
anim.segment = reader->ReadInt8();
anim.type = reader->ReadInt16();
switch ((TextureAnimationParamsType)anim.type) {
case TextureAnimationParamsType::SingleScroll: {
auto* e = (AnimatedMatTexScrollParams*)malloc(sizeof(AnimatedMatTexScrollParams));
if (e == nullptr) {
SPDLOG_CRITICAL("Failed to allocate memory for AnimatedMatTexScrollParams");
std::bad_alloc ex;
throw ex;
}
e->xStep = reader->ReadInt8();
e->yStep = reader->ReadInt8();
e->width = reader->ReadUByte();
e->height = reader->ReadUByte();
anim.params = e;
break;
}
case TextureAnimationParamsType::DualScroll: {
auto* e = (AnimatedMatTexScrollParams*)malloc(sizeof(AnimatedMatTexScrollParams) * 2);
if (e == nullptr) {
SPDLOG_CRITICAL("Failed to allocate memory for AnimatedMatTexScrollParams");
std::bad_alloc ex;
throw ex;
}
e[0].xStep = reader->ReadInt8();
e[0].yStep = reader->ReadInt8();
e[0].width = reader->ReadUByte();
e[0].height = reader->ReadUByte();
e[1].xStep = reader->ReadInt8();
e[1].yStep = reader->ReadInt8();
e[1].width = reader->ReadUByte();
e[1].height = reader->ReadUByte();
anim.params = e;
break;
}
case TextureAnimationParamsType::ColorChange:
case TextureAnimationParamsType::ColorChangeLERP:
case TextureAnimationParamsType::ColorChangeLagrange: {
auto* e = (AnimatedMatColorParams*)malloc(sizeof(AnimatedMatColorParams));
if (e == nullptr) {
SPDLOG_CRITICAL("Failed to allocate memory for AnimatedMatColorParams");
std::bad_alloc ex;
throw ex;
}
e->keyFrameLength = reader->ReadUInt16();
e->keyFrameCount = reader->ReadUInt16();
size_t keyFrameSize = reader->ReadUInt16();
if (keyFrameSize != 0) {
e->keyFrames = new uint16_t[keyFrameSize];
for (size_t i = 0; i < keyFrameSize; i++) {
e->keyFrames[i] = reader->ReadUInt16();
}
} else {
e->keyFrames = nullptr;
}
size_t primColorSize = reader->ReadUInt16();
if (primColorSize != 0) {
e->primColors = new F3DPrimColor[primColorSize];
for (size_t i = 0; i < primColorSize; i++) {
e->primColors[i].r = reader->ReadUByte();
e->primColors[i].g = reader->ReadUByte();
e->primColors[i].b = reader->ReadUByte();
e->primColors[i].a = reader->ReadUByte();
e->primColors[i].lodFrac = reader->ReadUByte();
}
} else {
e->primColors = nullptr;
}
size_t envColorSize = reader->ReadUInt16();
if (envColorSize != 0) {
e->envColors = new F3DEnvColor[envColorSize];
for (size_t i = 0; i < envColorSize; i++) {
e->envColors[i].r = reader->ReadUByte();
e->envColors[i].g = reader->ReadUByte();
e->envColors[i].b = reader->ReadUByte();
e->envColors[i].a = reader->ReadUByte();
}
} else {
e->envColors = nullptr;
}
anim.params = e;
break;
}
case TextureAnimationParamsType::TextureCycle: {
auto* e = (AnimatedMatTexCycleParams*)malloc(sizeof(AnimatedMatTexCycleParams));
if (e == nullptr) {
SPDLOG_CRITICAL("Failed to allocate memory for AnimatedMatTexCycleParams");
std::bad_alloc ex;
throw ex;
}
e->keyFrameLength = reader->ReadUInt16();
uint32_t textureListSize = reader->ReadUInt32();
e->textureList = new void*[textureListSize];
e->textureIndexList = new uint8_t[e->keyFrameLength];
tAnim->textureCycleTextures.reserve(textureListSize);
for (size_t i = 0; i < textureListSize; i++) {
tAnim->textureCycleTextures.emplace_back("__OTR__" + reader->ReadString());
e->textureList[i] = (Gfx*)tAnim->textureCycleTextures[i].c_str();
}
for (size_t i = 0; i < e->keyFrameLength; i++) {
e->textureIndexList[i] = reader->ReadUByte();
}
anim.params = e;
break;
}
case TextureAnimationParamsType::Empty: {
anim.params = nullptr;
break;
}
}
tAnim->anims.emplace_back(anim);
}
return tAnim;
}
} // namespace SOH
|