Skip to content

Commit

Permalink
Fixed crashes from dynamic avatar materials
Browse files Browse the repository at this point in the history
  • Loading branch information
NSGolova committed Jan 30, 2024
1 parent a642683 commit 1c5bfcf
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 58 deletions.
12 changes: 12 additions & 0 deletions include/Assets/BundleLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "custom-types/shared/macros.hpp"
#include "custom-types/shared/coroutine.hpp"

#include "System/Collections/Generic/Dictionary_2.hpp"

using namespace UnityEngine;
using namespace std;

Expand All @@ -19,7 +21,13 @@ using namespace std;
#define DECLARE_FILE(name, prefix) extern "C" uint8_t _binary_##name##_start[]; extern "C" uint8_t _binary_##name##_end[]; struct prefix##name { static size_t getLength() { return _binary_##name##_end - _binary_##name##_start; } static uint8_t* getData() { return _binary_##name##_start; } };
DECLARE_FILE(bl_bundle,)

typedef System::Collections::Generic::Dictionary_2<StringW, Material *> MaterialsDictionary;
typedef System::Collections::Generic::Dictionary_2<StringW, Sprite *> SpritesDictionary;

DECLARE_CLASS_CODEGEN(BeatLeader, Bundle, MonoBehaviour,

DECLARE_INSTANCE_FIELD(MaterialsDictionary*, materials);
DECLARE_INSTANCE_FIELD(SpritesDictionary*, sprites);
DECLARE_INSTANCE_FIELD(Material*, logoMaterial);
DECLARE_INSTANCE_FIELD(Material*, defaultAvatarMaterial);
DECLARE_INSTANCE_FIELD(Material*, UIAdditiveGlowMaterial);
Expand All @@ -33,6 +41,7 @@ DECLARE_CLASS_CODEGEN(BeatLeader, Bundle, MonoBehaviour,
DECLARE_INSTANCE_FIELD(Material*, accDetailsRowMaterial);
DECLARE_INSTANCE_FIELD(Material*, miniProfileBackgroundMaterial);
DECLARE_INSTANCE_FIELD(Material*, skillTriangleMaterial);
DECLARE_INSTANCE_FIELD(Material*, clanTagBackgroundMaterial);

DECLARE_INSTANCE_FIELD(Sprite*, locationIcon);
DECLARE_INSTANCE_FIELD(Sprite*, rowSeparatorIcon);
Expand Down Expand Up @@ -74,6 +83,9 @@ DECLARE_CLASS_CODEGEN(BeatLeader, Bundle, MonoBehaviour,
DECLARE_INSTANCE_METHOD(void, Init, AssetBundle* bundle);
DECLARE_INSTANCE_METHOD(Material*, GetAvatarMaterial, StringW effectName);
DECLARE_INSTANCE_METHOD(Sprite*, GetCountryIcon, StringW country);

DECLARE_INSTANCE_METHOD(Material*, GetMaterial, StringW name);
DECLARE_INSTANCE_METHOD(Sprite*, GetSprite, StringW name);
)

class BundleLoader {
Expand Down
149 changes: 91 additions & 58 deletions src/Assets/BundleLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Assets/BundleLoader.hpp"

#include "include/Utils/StringUtils.hpp"

#include <utility>
#include "main.hpp"

Expand Down Expand Up @@ -30,71 +32,102 @@ custom_types::Helpers::Coroutine BundleLoader::LoadBundle(UnityEngine::GameObjec
co_return;
}

Material* getMaterial(std::string name, AssetBundle* assetBundle) {
return assetBundle->LoadAsset<Material*>(std::move(name));
}

void BeatLeader::Bundle::Init(AssetBundle* assetBundle) {
logoMaterial = getMaterial("LogoMaterial", assetBundle);
defaultAvatarMaterial = getMaterial("DefaultAvatar", assetBundle);
UIAdditiveGlowMaterial = getMaterial("UIAdditiveGlow", assetBundle);
scoreBackgroundMaterial = getMaterial("ScoreBackgroundMaterial", assetBundle);
scoreUnderlineMaterial = getMaterial("ScoreUnderlineMaterial", assetBundle);
VotingButtonMaterial = getMaterial("VotingButtonMaterial", assetBundle);
handAccIndicatorMaterial = getMaterial("HandAccIndicatorMaterial", assetBundle);
accGridBackgroundMaterial = getMaterial("AccGridBackgroundMaterial", assetBundle);
accuracyGraphMaterial = getMaterial("AccuracyGraphBackground", assetBundle);
accuracyGraphLine = getMaterial("AccuracyGraphLine", assetBundle);
accDetailsRowMaterial = getMaterial("AccDetailsRowMaterial", assetBundle);
miniProfileBackgroundMaterial = getMaterial("UIMiniProfileBackgroundMaterial", assetBundle);
skillTriangleMaterial = getMaterial("UISkillTriangleMaterial", assetBundle);

locationIcon = assetBundle->LoadAsset<Sprite*>("LocationIcon");
rowSeparatorIcon = assetBundle->LoadAsset<Sprite*>("RowSeparatorIcon");
beatLeaderLogoGradient = assetBundle->LoadAsset<Sprite*>("BeatLeaderLogoGradient");
transparentPixel = assetBundle->LoadAsset<Sprite*>("TransparentPixel");
fileError = assetBundle->LoadAsset<Sprite*>("FileError");
modifiersIcon = assetBundle->LoadAsset<Sprite*>("ModifiersIcon");
settingsIcon = assetBundle->LoadAsset<Sprite*>("BL_SettingsIcon");

overview1Icon = assetBundle->LoadAsset<Sprite*>("BL_Overview1Icon");
overview2Icon = assetBundle->LoadAsset<Sprite*>("BL_Overview2Icon");
detailsIcon = assetBundle->LoadAsset<Sprite*>("BL_DetailsIcon");
gridIcon = assetBundle->LoadAsset<Sprite*>("BL_GridIcon");
graphIcon = assetBundle->LoadAsset<Sprite*>("BL_GraphIcon");
websiteLinkIcon = assetBundle->LoadAsset<Sprite*>("BL_Website");
discordLinkIcon = assetBundle->LoadAsset<Sprite*>("BL_Discord");
patreonLinkIcon = assetBundle->LoadAsset<Sprite*>("BL_Patreon");
replayIcon = assetBundle->LoadAsset<Sprite*>("BL_QuestReplayIcon");

twitterIcon = assetBundle->LoadAsset<Sprite*>("BL_TwitterIcon");
twitchIcon = assetBundle->LoadAsset<Sprite*>("BL_TwitchIcon");
youtubeIcon = assetBundle->LoadAsset<Sprite*>("BL_YoutubeIcon");
profileIcon = assetBundle->LoadAsset<Sprite*>("BL_ProfileIcon");
friendsIcon = assetBundle->LoadAsset<Sprite*>("BL_FriendsIcon");
incognitoIcon = assetBundle->LoadAsset<Sprite*>("BL_IncognitoIcon");
defaultAvatar = assetBundle->LoadAsset<Sprite*>("BL_DefaultAvatar");

friendsSelectorIcon = assetBundle->LoadAsset<Sprite*>("BL_FriendsSelectorIcon");
globeIcon = assetBundle->LoadAsset<Sprite*>("BL_GlobeIcon");

generalContextIcon = assetBundle->LoadAsset<Sprite*>("BL_ContextGeneral");
noModifiersIcon = assetBundle->LoadAsset<Sprite*>("BL_ContextNoModifiers");
noPauseIcon = assetBundle->LoadAsset<Sprite*>("BL_ContextNoPause");
golfIcon = assetBundle->LoadAsset<Sprite*>("BL_ContextGolf");
scpmIcon = assetBundle->LoadAsset<Sprite*>("BL_ContextSCPM");

TMP_SpriteCurved = assetBundle->LoadAsset<Shader*>("TMP_SpriteCurved");
Material* BeatLeader::Bundle::GetMaterial(StringW name) {
if (materials->ContainsKey(name)) {
return materials->get_Item(name);
} else {
return NULL;
}
}

Material* BeatLeader::Bundle::GetAvatarMaterial(StringW effectName) {
Material* result;
Material* result = NULL;

if (((string)effectName).find("_") != string::npos) {
result = getMaterial(effectName, BundleLoader::assetBundle);
result = GetMaterial(effectName);
}
return result != NULL ? result : defaultAvatarMaterial;
}

Sprite* BeatLeader::Bundle::GetSprite(StringW name) {
if (sprites->ContainsKey(name)) {
return sprites->get_Item(name);
} else {
return NULL;
}
}

Sprite* BeatLeader::Bundle::GetCountryIcon(StringW country) {
return BundleLoader::assetBundle->LoadAsset<Sprite*>(country);
return GetSprite(toLower((string)country));
}

void BeatLeader::Bundle::Init(AssetBundle* assetBundle) {
auto allnames = assetBundle->GetAllAssetNames();
materials = System::Collections::Generic::Dictionary_2<StringW, Material*>::New_ctor();
sprites = System::Collections::Generic::Dictionary_2<StringW, Sprite*>::New_ctor();

for (size_t i = 0; i < allnames.Length(); i++)
{
StringW name = allnames[i];
auto material = assetBundle->LoadAsset<Material*>(name);
if (material != NULL) {
materials->Add(material->get_name(), material);
}
auto sprite = assetBundle->LoadAsset<Sprite*>(name);
if (sprite != NULL) {
sprites->Add(sprite->get_name(), sprite);
}
}

logoMaterial = GetMaterial("LogoMaterial");
defaultAvatarMaterial = GetMaterial("DefaultAvatar");
UIAdditiveGlowMaterial = GetMaterial("UIAdditiveGlow");
scoreBackgroundMaterial = GetMaterial("ScoreBackgroundMaterial");
scoreUnderlineMaterial = GetMaterial("ScoreUnderlineMaterial");
VotingButtonMaterial = GetMaterial("VotingButtonMaterial");
handAccIndicatorMaterial = GetMaterial("HandAccIndicatorMaterial");
accGridBackgroundMaterial = GetMaterial("AccGridBackgroundMaterial");
accuracyGraphMaterial = GetMaterial("AccuracyGraphBackground");
accuracyGraphLine = GetMaterial("AccuracyGraphLine");
accDetailsRowMaterial = GetMaterial("AccDetailsRowMaterial");
miniProfileBackgroundMaterial = GetMaterial("UIMiniProfileBackgroundMaterial");
skillTriangleMaterial = GetMaterial("UISkillTriangleMaterial");
clanTagBackgroundMaterial = GetMaterial("ClanTagBackgroundMaterial");

locationIcon = GetSprite("LocationIcon");
rowSeparatorIcon = GetSprite("RowSeparatorIcon");
beatLeaderLogoGradient = GetSprite("BeatLeaderLogoGradient");
transparentPixel = GetSprite("TransparentPixel");
fileError = GetSprite("FileError");
modifiersIcon = GetSprite("ModifiersIcon");
settingsIcon = GetSprite("BL_SettingsIcon");

overview1Icon = GetSprite("BL_Overview1Icon");
overview2Icon = GetSprite("BL_Overview2Icon");
detailsIcon = GetSprite("BL_DetailsIcon");
gridIcon = GetSprite("BL_GridIcon");
graphIcon = GetSprite("BL_GraphIcon");
websiteLinkIcon = GetSprite("BL_Website");
discordLinkIcon = GetSprite("BL_Discord");
patreonLinkIcon = GetSprite("BL_Patreon");
replayIcon = GetSprite("BL_QuestReplayIcon");

twitterIcon = GetSprite("BL_TwitterIcon");
twitchIcon = GetSprite("BL_TwitchIcon");
youtubeIcon = GetSprite("BL_YoutubeIcon");
profileIcon = GetSprite("BL_ProfileIcon");
friendsIcon = GetSprite("BL_FriendsIcon");
incognitoIcon = GetSprite("BL_IncognitoIcon");
defaultAvatar = GetSprite("BL_DefaultAvatar");

friendsSelectorIcon = GetSprite("BL_FriendsSelectorIcon");
globeIcon = GetSprite("BL_GlobeIcon");

generalContextIcon = GetSprite("BL_ContextGeneral");
noModifiersIcon = GetSprite("BL_ContextNoModifiers");
noPauseIcon = GetSprite("BL_ContextNoPause");
golfIcon = GetSprite("BL_ContextGolf");
scpmIcon = GetSprite("BL_ContextSCPM");

TMP_SpriteCurved = assetBundle->LoadAsset<Shader*>("TMP_SpriteCurved");
}

0 comments on commit 1c5bfcf

Please sign in to comment.