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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519 |
#include "gfCHogLevelLoaderComponent.h"
#include <chrono>
#include <unordered_map>
#include <fsCore/fsIFile.h>
#include <fsCore/src/debug/fsAssert.h>
#include <fsRenderer/src/fsCBrushManager.h>
#include <fsCore/src/fsCAppSettings.h>
#include <fsAppCore/src/fsILevelBasedAppCoreMain.h>
#include <fsAppCore/src/fsCProfileManager.h>
#if defined fsCollision
#include <fsCollision/src/fsCCollisionComponent.h>
#include <fsCollision/src/fsCImpulseComponent.h>
#include <gfComponents/components/gfCTappableCollectableComponent.h>
#endif
#include <gfMinigameCore/components/gfISimpleCutterComponent.h>
#include <gfComponents/components/gui/gfCAutoMinigameButtonComponent.h>
#include <gfComponents/components/subScene/gfCSubSceneComponent.h>
#include <gfComponents/components/subScene/gfCSubSceneNavigateComponent.h>
#include <gfComponents/components/gfCTappableCollectableComponent.h>
#include <gfComponents/components/gfCTaskComponent.h>
#include "fsIDisplay.h"
#include "fsIResourceNamePathAdjuster.h"
#include "../src/gfCHogEntityFactory.h"
#include "components/gfIMinigameComponent.h"
#include "components/gfIMinigameLogic.h"
#include "gfCParallaxComponent.h"
const fsCUniqueId gfCHogLevelLoaderComponent::mTypeId("fsCHogLevelLoaderComponent");
void gfCHogLevelLoaderComponent::collectablEntitiesInitialiseDo(std::vector<fsCEntity*>& pNewEnts)
{
const fsCAppSettings* const appSettings = fsCAppCoreMain::instanceGet()->settingsGet();
const fsCVariableTable* const levelSettings = fsILevelBasedAppCoreMain::instanceGet()->levelSettingsGet();
if (levelSettings->boolGetWithDefault("postCollectionVisible", false))
{
const fsStr markerFile = levelSettings->strGetWithDefault("postCollectionVisibleMarker",
appSettings->strGetWithDefault(
"diffFoundMarker", ""));
for (fsCEntity* ent : pNewEnts)
{
if (auto* const collectableComp = ent->componentGet<gfCTappableCollectableComponent>())
{
if (!ent->tagExists(gfCHogEntityFactory::bonusPickablePrefix)
&& !ent->tagExists(gfCHogEntityFactory::bonusSilhouettePickablePrefix))
{
collectableComp->visiblePostCollectionSet(markerFile);
}
}
else if (auto* const collectableComp = ent->componentGet<gfCTaskComponent>())
{
collectableComp->visiblePostCollectionSet(markerFile);
}
}
}
}
void gfCHogLevelLoaderComponent::levelLoad(const fsStr& pLevelName, const fsStr& pLevelFolder)
{
mLayersSubFolder = layersSubFolderGet(pLevelFolder);
texturePagesLoad(pLevelFolder + mLayersSubFolder);
std::vector<fsCEntity*> newEnts = entitiesCreateFromCoordsFile(pLevelFolder);
visibleEntitiesInitialise(pLevelFolder, newEnts);
particleEntitiesInitialise(pLevelFolder, newEnts);
auto start = std::chrono::high_resolution_clock::now();
entitiesAddToSubScenes(newEnts);
auto end = (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start));
#if defined fsDEBUG || defined fsDEVELOP
if (end > std::chrono::milliseconds(5000))
{
fsLogError("Particles taking too long to init")
}
#endif
entitesAddToParallax(newEnts);
minigameEntitiesAddToLogic(newEnts);
tappableEntitiesTouchHitSpriteSet(newEnts);
collectablEntitiesInitialise(newEnts);
#if defined fsCollision
collisionTestSetup(newEnts);
#endif
cutterEntitiesInitialise(newEnts, pLevelFolder, parentGet());
subSceneNavigateMarkersBringToFront(newEnts);
fsILevelBasedAppCoreMain::instanceGet()->subScenesStore();
}
void gfCHogLevelLoaderComponent::subSceneNavigateMarkersBringToFront(std::vector<fsCEntity*>& pNewEnts)<--- Parameter 'pNewEnts' can be declared as reference to const
{
for (fsCEntity* ent : pNewEnts)
{
if (auto* const navComp = ent->firstComponentOfFamilyType<gfCSubSceneNavigateComponent>())
{
navComp->bringToFront();
}
}
}
#if defined fsFRAMEWORK_AX
#include <../vendor/axmol/3rdparty/rapidjson/document.h>
#include <../vendor/axmol/3rdparty/rapidjson/prettywriter.h>
#include <../vendor/axmol/3rdparty/rapidjson/writer.h>
#elif defined fsFRAMEWORK_CX
#include <../vendor/cocos2d-x/external/json/document.h>
#include <../vendor/cocos2d-x/external/json/prettywriter.h>
#include <../vendor/cocos2d-x/external/json/writer.h>
#endif
#if defined fsFRAMEWORK_AX || defined fsFRAMEWORK_CX
void printJsonDom(const rapidjson::Document& document) {
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
fsCLogMsg(buffer.GetString());
}
#endif
fsStr gfCHogLevelLoaderComponent::preprocessJson(const fsStr& pJsonString) {
#if defined fsFRAMEWORK_AX || defined fsFRAMEWORK_CX
rapidjson::Document document;
if (document.Parse(pJsonString.utf8Get()).HasParseError()) {
return "Error Parsing JSON\n";
}
if (!document.HasMember("skins") || !document["skins"].HasMember("default")) {
return "Invalid JSON structure\n";
}
rapidjson::Value& skins = document["skins"]["default"];
std::unordered_map<std::string, int> nameCount;
std::unordered_map<std::string, int> backgroundCount;
std::vector<std::pair<std::string, rapidjson::Value>> newMembers;
std::vector<rapidjson::Value::MemberIterator> membersToRemove;
// First pass: collect layer name counts to locate duplicates
for (auto itr = skins.MemberBegin(); itr != skins.MemberEnd(); ++itr) {
std::string originalName = itr->name.GetString();
if (nameCount[originalName] == 0) {
nameCount[originalName] = 1;
backgroundCount[originalName] = 1;
} else {
nameCount[originalName]++;
}
}
// Second pass: generate new names for duplicates
for (auto itr = skins.MemberBegin(); itr != skins.MemberEnd(); ++itr) {
std::string originalName = itr->name.GetString();
if (nameCount[originalName] > 1) {
std::string newName = originalName + "_" + std::to_string(nameCount[originalName]);
// Handle background duplicates differently - they are renamed in ascending order
if (originalName.find("Background") != std::string::npos) {
if (backgroundCount[originalName] == 1) {
backgroundCount[originalName]++;
continue;
}
newName = originalName + "_" + std::to_string(backgroundCount[originalName]);
}
nameCount[originalName]--;
backgroundCount[originalName]++;
rapidjson::Value newNameValue(newName.c_str(), document.GetAllocator());
rapidjson::Value newValue;
newValue.CopyFrom(itr->value, document.GetAllocator());
newMembers.push_back(std::make_pair(newName, std::move(newValue)));
membersToRemove.push_back(itr);
}
}
for (auto itr = membersToRemove.rbegin(); itr != membersToRemove.rend(); ++itr) {
skins.RemoveMember(*itr);
}
for (auto& newMember : newMembers) {
rapidjson::Value newNameValue(newMember.first.c_str(), document.GetAllocator());
skins.AddMember(newNameValue, std::move(newMember.second), document.GetAllocator());
}
// Repoint the background attachment to the unique skin names, but retain the
// exported attachment name so parseJsonToCsv can preserve the image order.
fsU32 count = 0;
rapidjson::Value& slots = document["slots"];
for (rapidjson::SizeType i = 0; i < slots.Size(); ++i) {
rapidjson::Value& slot = slots[i];
std::string name = slot["name"].GetString();
std::string attachment = slot["attachment"].GetString();
if (name.find("Background") != std::string::npos) {
rapidjson::Value originalAttachment(attachment.c_str(), document.GetAllocator());
slot.AddMember("_fissionOriginalAttachment", originalAttachment, document.GetAllocator());
if (count++) {
std::string newName = name + "_" + std::to_string(count);
slot["attachment"].SetString(newName.c_str(), document.GetAllocator());
}
}
}
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
return buffer.GetString();
#endif
return "Only in axmol until rapidjson is added to common deps\n";
}
fsStr gfCHogLevelLoaderComponent::parseJsonToCsv(const fsStr& pJsonString) {
#if defined fsFRAMEWORK_AX || defined fsFRAMEWORK_CX
rapidjson::Document document;
fsStr csvString;
if (document.Parse(pJsonString.utf8Get()).HasParseError())
{
return "Error Parsing JSON\n";
}
const rapidjson::Value& slots = document["slots"];
const rapidjson::Value& skins = document["skins"]["default"];
for (rapidjson::SizeType i = 0; i < slots.Size(); ++i) {
const rapidjson::Value& slot = slots[i];
if (slot.HasMember("name") && slot.HasMember("bone") && slot.HasMember("attachment")) {
fsStr name = slot["name"].GetString();
fsStr attachment = slot["attachment"].GetString();
fsStr imageAttachment = slot.HasMember("_fissionOriginalAttachment")
? slot["_fissionOriginalAttachment"].GetString()
: attachment;
fsStr x = "", y = "";
if (skins.HasMember(attachment.utf8Get())) {
const rapidjson::Value& skinItem = skins[attachment.utf8Get()];
if (skinItem.HasMember(name.utf8Get())) {
const rapidjson::Value& attachmentItem = skinItem[name.utf8Get()];
if (attachmentItem.HasMember("x") && attachmentItem.HasMember("y")) {
x = fsStr(static_cast<fsS32>(attachmentItem["x"].GetFloat() - fsIDisplay::instanceGet()->designCanvasWidthGet() / 2));
y = fsStr(static_cast<fsS32>(attachmentItem["y"].GetFloat() - fsIDisplay::instanceGet()->designCanvasHeightGet() / 2));
(y[0] == '-') ? y.chomp("-") : y = "-" + y;
}
}
}
if (imageAttachment != name)
{
std::string nameStr = name.utf8Get();
std::string attachmentStr = imageAttachment.utf8Get();
std::string suffix = attachmentStr.substr(name.dataSizeGet());
// If suffix starts with underscore, replace it with "-m"
if (!suffix.empty() && suffix[0] == '_') {
if (imageAttachment.find("CMN-") == 0)
{
attachmentStr = nameStr;
}
else
{
attachmentStr = nameStr + suffix.replace(0, 1, "-m");
}
imageAttachment = attachmentStr.c_str();
}
}
if (imageAttachment.find(".") == fsStr::npos) {
i == 0 ? imageAttachment += ".jpg" : imageAttachment += ".png"; // First attachment is the background image
}
csvString += imageAttachment + "," + x + "," + y + "\n";
}
}
return csvString;
#endif
return "Only in axmol until rapidjson is added to common deps\n";
}
std::vector<fsCEntity*> gfCHogLevelLoaderComponent::entitiesCreateFromCoordsFile(const fsStr& pSplitFolderPath)
{
std::vector<fsCEntity*> newEntities;
fsStr coordsFilename = levelSceneFilenameGet(pSplitFolderPath);
coordsFilename.replaceFirst(fsIResourceNamePathAdjuster::mLayersFolder, mLayersSubFolder);
fsStr coordsFileData = fsIFile::strGetFromFile(
fsCResourceName(coordsFilename, fsCResourceName::eRES_LOCATION_ASSETS));
mJsonLevelFile = fsStr::npos != coordsFilename.find(".json");
if (mJsonLevelFile)
{
coordsFileData = parseJsonToCsv(preprocessJson(coordsFileData));
}
coordsFileData.replaceAll("%", "/");
fsStr entName = coordsFileData.chomp(",");
while (!entName.emptyGet())
{
fsCLogMsg(entName.utf8Get());
if (entName == "\r\n" || entName == "\r" || entName == "\n"
// hacked into texture page so ignore as created elsewhere....
|| fsStr::npos != entName.find("tutorial-")
|| fsStr::npos != entName.find("tex-"))
{
entName = coordsFileData.chomp(",");
continue;
}
fsAssert(fsStr::npos != coordsFileData.find( "," ) && fsStr::npos != coordsFileData.find( "\n" ),
"Expecting coods file in the format 'EntName.png,posX,posY\n' per entry.\n")
const fsPoint entPos{coordsFileData.chomp(",").toS32(), coordsFileData.chomp("\n").toS32()};
if (fsCEntity* const newEnt = parentGet()->entityFactoryGet()->entityConstruct(entName))
{
newEnt->posSet(fsVec2d(entPos));
newEntities.emplace_back(newEnt);
}
else
{
fsLogMsg("Couldn't construct %s in %s \r\n", entName.utf8Get(), pSplitFolderPath.utf8Get())
}
entName = coordsFileData.chomp(",");
}
return newEntities;
}
fsStr gfCHogLevelLoaderComponent::sceneImageNameResolve(const fsStr& pSceneFolderPath, const fsStr& pGuessedName)
{
// The Affinity/Spine export omits image extensions, so parseJsonToCsv only guesses
// one (.jpg for the background, .png otherwise). Resolve the real asset here by
// trying .webp (migration target) first, then the guessed format, then the other
// raster format, matching either a texture-page brush or a loose file on disk.
if (fsStr::npos == pGuessedName.rfind("."))
{
return pGuessedName;
}
// CMN- layers are redirected to gui/common by spriteComponentInitialise, so the
// scene folder is the wrong place to probe; leave them on the guessed name.
if (0 == pGuessedName.find("CMN-"))
{
return pGuessedName;
}
fsStr base = pGuessedName;
base.eraseFromLast(".");
const fsBool guessedJpg = (fsStr::npos != pGuessedName.rfind(".jpg"));
const fsStr candidateExts[3] = {
".webp", guessedJpg ? ".jpg" : ".png", guessedJpg ? ".png" : ".jpg"
};
fsCBrushManager* const brushManager = fsCBrushManager::instanceGet();
for (fsS32 i = 0; i < 3; ++i)
{
const fsStr candidate = base + candidateExts[i];
const fsCResourceName resource(pSceneFolderPath + candidate, fsCResourceName::eRES_LOCATION_ASSETS);
if (brushManager->brushCachedGet(resource) || fsIFile::fileExistsGet(resource))
{
return candidate;
}
}
return pGuessedName;
}
fsStr gfCHogLevelLoaderComponent::imageFileNameGet(fsCEntity* const pEntity, const fsStr& pSceneFolderPath)
{
if (mJsonLevelFile)
{
std::string fullName = pEntity->nameGet().utf8Get();
auto matchPos = fullName.rfind("-m");
if (std::string::npos != matchPos &&
fullName[matchPos + 2] >= '1' && fullName[matchPos + 2] <= '9')
{
fullName.replace(matchPos, 2, "_");
}
// Resolve the real image extension the export never provided.
fsStr resolvedName = sceneImageNameResolve(pSceneFolderPath, fullName.c_str());
// Backgrounds fall back to a generic Background.png when nothing resolved
// (e.g. the guessed name matched no brush or file).
if (resolvedName.find("Background") != fsStr::npos)
{
const fsCResourceName backgroundResource(pSceneFolderPath + resolvedName,
fsCResourceName::eRES_LOCATION_ASSETS);
if (!fsCBrushManager::instanceGet()->brushCachedGet(backgroundResource)
&& !fsIFile::fileExistsGet(backgroundResource))
{
return "Background.png";
}
return pSceneFolderPath + resolvedName;
}
return resolvedName;
}
return pEntity->nameGet();
}
void gfCHogLevelLoaderComponent::cutterEntitiesInitialise(std::vector<fsCEntity*>& pNewEnts,<--- Parameter 'pNewEnts' can be declared as reference to const
const fsStr& pLevelFolder, fsCEntity* const pParent)
{
gfIMinigameLogic* currLogic = nullptr;
for (fsCEntity* ent : pNewEnts)
{
if (ent->firstComponentOfFamilyType<gfIMinigameLogic>())
{
currLogic = ent->firstComponentOfFamilyType<gfIMinigameLogic>();
}
if (auto* const cutterComp = ent->firstComponentOfFamilyType<gfICutterComponent>())
{
if (!ent->componentGet<gfCSubSceneNavigateComponent>())
{
const auto autoMiniGameSourceArt = fsCProfileManager::instanceGet()->savedDataGet()->strGet(
gfCAutoMinigameButtonComponent::mSourceArtKey);
cutterComp->minigameEntitiesCreate(*fsILevelBasedAppCoreMain::instanceGet()->levelSettingsGet(),
pLevelFolder,
pParent->firstChildEntityWithComponentsOfFamilyType<
gfCSubSceneComponent>(), autoMiniGameSourceArt, currLogic);
}
}
}
}
#if defined fsCollision
void gfCHogLevelLoaderComponent::collisionTestSetup(std::vector< fsCEntity* >& pNewEnts)<--- Parameter 'pNewEnts' can be declared as reference to const
{
for (fsCEntity* ent : pNewEnts)
{
if (ent->nameGet().find("PCK") != fsStr::npos || ent->nameGet().find("BNS") != fsStr::npos)
{
ent->componentDestroy(gfCTappableCollectableComponent::mTypeId);
if (ent->nameGet().find("BNS") != fsStr::npos)
{
ent->componentAdd<fsNCollision::fsCImpulseComponent>();
}
ent->componentAdd<fsNCollision::fsCCollisionComponent>();
}
}
}
#endif
void gfCHogLevelLoaderComponent::minigameEntitiesAddToLogic(std::vector<fsCEntity*>& pNewEnts) const<--- Parameter 'pNewEnts' can be declared as reference to const
{
fsCEntity* currLogic = nullptr;
for (fsCEntity* ent : pNewEnts)
{
if (ent->firstComponentOfFamilyType<gfIMinigameLogic>())
{
currLogic = ent;
}
else if (currLogic)
{
if (ent->firstComponentOfFamilyType<gfIMinigameComponent>())
{
currLogic->childAdd(ent);
}
}
}
}
void gfCHogLevelLoaderComponent::entitesAddToParallax(std::vector<fsCEntity*>& pNewEnts) const<--- Parameter 'pNewEnts' can be declared as reference to const
{
fsCEntity* currParallax = nullptr;
for (fsCEntity* ent : pNewEnts)
{
if (fsStr::npos != ent->nameGet().find("parallax"))
{
currParallax = ent;
parallaxLayerInit(currParallax);
}
else if (currParallax)
{
ent->posSet(ent->posGet() - currParallax->posGet());
currParallax->childAdd(ent);
}
}
}
void gfCHogLevelLoaderComponent::parallaxLayerInit(fsCEntity* pParallaxEnt) const
{
if (gfCParallaxComponent* const parallaxComp = pParallaxEnt->componentGet<gfCParallaxComponent>())
{
parallaxComp->initialise();
}
}
fsIComponent* gfCHogLevelLoaderComponent::create(fsCEntity* pParent)
{
return new gfCHogLevelLoaderComponent(pParent);
}
gfCHogLevelLoaderComponent::gfCHogLevelLoaderComponent(fsCEntity* pParent) :
gfILevelLoaderComponent(pParent),
mJsonLevelFile(false)
{
}
gfCHogLevelLoaderComponent::~gfCHogLevelLoaderComponent()
{
}
|