From e569710774b550111e979f7f497c820c8d70da8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20Ege=20Ko=C3=A7?= <67821857+yekoc@users.noreply.github.com> Date: Tue, 9 Jan 2024 16:36:58 +0000 Subject: [PATCH] ContentManagement: Climb Stack for contentpack asm (#515) * ContentManagement: Climb Stack for contentpack asm Fixes #494 * ContentAddition: Use cheaper StackFrame ctor --- R2API.ContentManagement/ContentAddition.cs | 66 +++++++++++++--------- R2API.ContentManagement/README.md | 3 + R2API.ContentManagement/thunderstore.toml | 2 +- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/R2API.ContentManagement/ContentAddition.cs b/R2API.ContentManagement/ContentAddition.cs index c59d9ae1..66efc298 100644 --- a/R2API.ContentManagement/ContentAddition.cs +++ b/R2API.ContentManagement/ContentAddition.cs @@ -30,7 +30,7 @@ public static class ContentAddition /// true if valid and added, false if one of the requirements is not met public static bool AddBody(GameObject bodyPrefab) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!HasComponent(bodyPrefab)) @@ -52,7 +52,7 @@ public static bool AddBody(GameObject bodyPrefab) /// true if valid and added, false if one of the requirements is not met public static bool AddMaster(GameObject masterPrefab) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!HasComponent(masterPrefab)) @@ -75,7 +75,7 @@ public static bool AddMaster(GameObject masterPrefab) /// true if valid and added, false if one of the requirements is not met public static bool AddProjectile(GameObject projectilePrefab) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!HasComponent(projectilePrefab)) @@ -102,7 +102,7 @@ public static bool AddProjectile(GameObject projectilePrefab) /// true if valid and added, false if one of the requirements is not met public static bool AddGameMode(GameObject gameModePrefab) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!HasComponent(gameModePrefab)) @@ -125,7 +125,7 @@ public static bool AddGameMode(GameObject gameModePrefab) /// true if valid and added, false if one of the requirements is not met public static bool AddNetworkedObject(GameObject networkedObject) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!HasComponent(networkedObject)) @@ -148,7 +148,7 @@ public static bool AddNetworkedObject(GameObject networkedObject) /// true if valid and added, false if one of the requirements is not met. public static bool AddEffect(GameObject effectPrefab) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!HasComponent(effectPrefab)) @@ -178,7 +178,7 @@ public static bool AddEffect(GameObject effectPrefab) /// true if valid and added, false if one of the requirements is not met public static bool AddSkillDef(SkillDef skillDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (skillDef.activationState.stateType == null) @@ -205,7 +205,7 @@ public static bool AddSkillDef(SkillDef skillDef) /// true if valid and added, false if one of the requirements is not met public static bool AddSkillFamily(SkillFamily skillFamily) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (skillFamily.variants.Any(v => v.skillDef == null)) @@ -228,7 +228,7 @@ public static bool AddSkillFamily(SkillFamily skillFamily) public static bool AddSceneDef(SceneDef sceneDef) { //Add stuff here, i dont know what qualifies as a "valid" sceneDef, then again, people should just use ROS for handling sceneDefs, r2api just lets you add them this way for the sake of completion - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is trying to add a SceneDef, R2API does not support weaving of Scenes, Use RainOfStages instead for weaving SceneDefs."); @@ -247,7 +247,7 @@ public static bool AddSceneDef(SceneDef sceneDef) /// true if valid and added, false if one of the requirements is not met public static bool AddItemDef(ItemDef itemDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {itemDef} via {nameof(ContentAddition)}.{nameof(AddItemDef)}()" + @@ -267,7 +267,7 @@ public static bool AddItemDef(ItemDef itemDef) public static bool AddItemTierDef(ItemTierDef itemTierDef) { //Todo: finds what makes an itemTierDef invalid - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, itemTierDef); @@ -285,7 +285,7 @@ public static bool AddItemTierDef(ItemTierDef itemTierDef) public static bool AddItemRelationshipProvider(ItemRelationshipProvider itemRelationshipProvider) { //Todo: Find what makes an ItemRelationshipProvider invalid - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, itemRelationshipProvider); @@ -303,7 +303,7 @@ public static bool AddItemRelationshipProvider(ItemRelationshipProvider itemRela public static bool AddItemRelationshipType(ItemRelationshipType itemRelationshipType) { //Todo: Find what makes an ItemRelationshipType invalid - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, itemRelationshipType); @@ -322,7 +322,7 @@ public static bool AddItemRelationshipType(ItemRelationshipType itemRelationship /// true if valid and added, false if one of the requirements is not met public static bool AddEquipmentDef(EquipmentDef equipmentDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {equipmentDef} via {nameof(ContentAddition)}.{nameof(AddEquipmentDef)}()" + @@ -343,7 +343,7 @@ public static bool AddEquipmentDef(EquipmentDef equipmentDef) /// true if valid and added, false if one of the requirements is not met public static bool AddBuffDef(BuffDef buffDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (buffDef.eliteDef && buffDef.eliteDef.eliteEquipmentDef && buffDef.eliteDef.eliteEquipmentDef.passiveBuffDef != buffDef) @@ -369,7 +369,7 @@ public static bool AddBuffDef(BuffDef buffDef) /// true if valid and added, false if one of the requirements is not met public static bool AddEliteDef(EliteDef eliteDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {eliteDef} via {nameof(ContentAddition)}.{nameof(AddEliteDef)}()" + @@ -389,7 +389,7 @@ public static bool AddEliteDef(EliteDef eliteDef) /// true if valid and added, false if one of the requirements is not met public static bool AddUnlockableDef(UnlockableDef unlockableDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, unlockableDef); @@ -408,7 +408,7 @@ public static bool AddUnlockableDef(UnlockableDef unlockableDef) /// true if valid and added, false if one of the requirements is not met public static bool AddSurvivorDef(SurvivorDef survivorDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (!survivorDef.bodyPrefab) @@ -439,7 +439,7 @@ public static bool AddSurvivorDef(SurvivorDef survivorDef) /// true if valid and added, false if one of the requirements is not met public static bool AddArtifactDef(ArtifactDef artifactDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (artifactDef.smallIconDeselectedSprite == null || artifactDef.smallIconSelectedSprite == null) @@ -461,7 +461,7 @@ public static bool AddArtifactDef(ArtifactDef artifactDef) /// true if valid and added, false if one of the requirements is not met public static bool AddSurfaceDef(SurfaceDef surfaceDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (surfaceDef.impactEffectPrefab == null || surfaceDef.footstepEffectPrefab == null) @@ -483,7 +483,7 @@ public static bool AddSurfaceDef(SurfaceDef surfaceDef) /// true if valid and added, false if one of the requirements is not met public static bool AddNetworkSoundEventDef(NetworkSoundEventDef networkSoundEventDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { if (string.IsNullOrEmpty(networkSoundEventDef.eventName) || string.IsNullOrWhiteSpace(networkSoundEventDef.eventName)) @@ -505,7 +505,7 @@ public static bool AddNetworkSoundEventDef(NetworkSoundEventDef networkSoundEven /// true if valid and added, false if one of the requirements is not met public static bool AddMusicTrackDef(MusicTrackDef musicTrackDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, musicTrackDef); @@ -521,7 +521,7 @@ public static bool AddMusicTrackDef(MusicTrackDef musicTrackDef) /// true if valid and added, false if one of the requirements is not met public static bool AddGameEndingDef(GameEndingDef gameEndingDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, gameEndingDef); @@ -539,7 +539,7 @@ public static bool AddGameEndingDef(GameEndingDef gameEndingDef) /// true if valid and added, false if one of the requirements is not met public static bool AddEntityStateConfiguration(EntityStateConfiguration entityStateConfiguration) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { Type type = Type.GetType(entityStateConfiguration.targetType.assemblyQualifiedName); @@ -569,7 +569,7 @@ public static bool AddEntityStateConfiguration(EntityStateConfiguration entitySt public static bool AddExpansionDef(ExpansionDef expansionDef) { //Todo: Find what makes an ExpansionDef invalid - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, expansionDef); @@ -587,7 +587,7 @@ public static bool AddExpansionDef(ExpansionDef expansionDef) public static bool AddEntitlementDef(EntitlementDef entitlementDef) { //Todo: Find what makes an EntitlementDef invalid - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, entitlementDef); @@ -604,7 +604,7 @@ public static bool AddEntitlementDef(EntitlementDef entitlementDef) /// true if valid and added, false if one of the requirements is not met public static bool AddMiscPickupDef(MiscPickupDef miscPickupDef) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if (CatalogBlockers.GetAvailability()) { R2APIContentManager.HandleContentAddition(asm, miscPickupDef); @@ -635,7 +635,7 @@ public static SerializableEntityStateType AddEntityState(out bool wasAdded) w /// A SerializableEntityStateType, the StateType will be null if wasAdded is false. public static SerializableEntityStateType AddEntityState(Type entityStateType, out bool wasAdded) { - var asm = Assembly.GetCallingAssembly(); + var asm = GetNonAPICaller(); if(CatalogBlockers.GetAvailability()) { @@ -671,5 +671,15 @@ private static void RejectContent(object content, Assembly assembly, string cont catch (Exception e) { ContentManagementPlugin.Logger.LogError(e); } } private static bool HasComponent(GameObject obj) where T : Component => obj.GetComponent(); + private static Assembly GetNonAPICaller(){ + var R2ASM = Assembly.GetExecutingAssembly(); + for(int i = 0; i < 99 ; i++){//Empty frame will stop loop early when the stack runs out. + var asm = new System.Diagnostics.StackFrame(i,false).GetMethod()?.DeclaringType.Assembly; + if(asm != R2ASM){ + return asm; + } + } + return null; + } #endregion } diff --git a/R2API.ContentManagement/README.md b/R2API.ContentManagement/README.md index 0cc40557..e6d7af4e 100644 --- a/R2API.ContentManagement/README.md +++ b/R2API.ContentManagement/README.md @@ -23,6 +23,9 @@ R2API.ContentManaged is used for mods that would like to have R2API handle the c ## Changelog +### '1.0.4' +* Fix content getting erroneously attributed to R2API's contentpack. + ### '1.0.3' * Re-opt-in to system-initializers,fixing behavior related to late-registration of content. diff --git a/R2API.ContentManagement/thunderstore.toml b/R2API.ContentManagement/thunderstore.toml index 3f1ae63f..19305541 100644 --- a/R2API.ContentManagement/thunderstore.toml +++ b/R2API.ContentManagement/thunderstore.toml @@ -5,7 +5,7 @@ schemaVersion = "0.0.1" [package] namespace = "RiskofThunder" name = "R2API_ContentManagement" -versionNumber = "1.0.3" +versionNumber = "1.0.4" description = "API for adding content to the game" websiteUrl = "https://github.com/risk-of-thunder/R2API" containsNsfwContent = false