Skip to content

Commit

Permalink
ContentManagement: Climb Stack for contentpack asm (#515)
Browse files Browse the repository at this point in the history
* ContentManagement: Climb Stack for contentpack asm

Fixes #494

* ContentAddition: Use cheaper StackFrame ctor
  • Loading branch information
yekoc authored Jan 9, 2024
1 parent 38a5d55 commit e569710
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
66 changes: 38 additions & 28 deletions R2API.ContentManagement/ContentAddition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class ContentAddition
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddBody(GameObject bodyPrefab)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<CharacterBody>())
{
if (!HasComponent<CharacterBody>(bodyPrefab))
Expand All @@ -52,7 +52,7 @@ public static bool AddBody(GameObject bodyPrefab)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddMaster(GameObject masterPrefab)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<CharacterMaster>())
{
if (!HasComponent<CharacterMaster>(masterPrefab))
Expand All @@ -75,7 +75,7 @@ public static bool AddMaster(GameObject masterPrefab)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddProjectile(GameObject projectilePrefab)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<ProjectileController>())
{
if (!HasComponent<ProjectileController>(projectilePrefab))
Expand All @@ -102,7 +102,7 @@ public static bool AddProjectile(GameObject projectilePrefab)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddGameMode(GameObject gameModePrefab)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<Run>())
{
if (!HasComponent<Run>(gameModePrefab))
Expand All @@ -125,7 +125,7 @@ public static bool AddGameMode(GameObject gameModePrefab)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddNetworkedObject(GameObject networkedObject)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<NetworkIdentity>())
{
if (!HasComponent<NetworkIdentity>(networkedObject))
Expand All @@ -148,7 +148,7 @@ public static bool AddNetworkedObject(GameObject networkedObject)
/// <returns>true if valid and added, false if one of the requirements is not met.</returns>
public static bool AddEffect(GameObject effectPrefab)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<EffectComponent>())
{
if (!HasComponent<EffectComponent>(effectPrefab))
Expand Down Expand Up @@ -178,7 +178,7 @@ public static bool AddEffect(GameObject effectPrefab)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddSkillDef(SkillDef skillDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<SkillDef>())
{
if (skillDef.activationState.stateType == null)
Expand All @@ -205,7 +205,7 @@ public static bool AddSkillDef(SkillDef skillDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddSkillFamily(SkillFamily skillFamily)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<SkillFamily>())
{
if (skillFamily.variants.Any(v => v.skillDef == null))
Expand All @@ -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<SceneDef>())
{
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.");
Expand All @@ -247,7 +247,7 @@ public static bool AddSceneDef(SceneDef sceneDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddItemDef(ItemDef itemDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<ItemDef>())
{
ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {itemDef} via {nameof(ContentAddition)}.{nameof(AddItemDef)}()" +
Expand All @@ -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<ItemTierDef>())
{
R2APIContentManager.HandleContentAddition(asm, itemTierDef);
Expand All @@ -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<ItemRelationshipProvider>())
{
R2APIContentManager.HandleContentAddition(asm, itemRelationshipProvider);
Expand All @@ -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<ItemRelationshipType>())
{
R2APIContentManager.HandleContentAddition(asm, itemRelationshipType);
Expand All @@ -322,7 +322,7 @@ public static bool AddItemRelationshipType(ItemRelationshipType itemRelationship
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddEquipmentDef(EquipmentDef equipmentDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<EquipmentDef>())
{
ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {equipmentDef} via {nameof(ContentAddition)}.{nameof(AddEquipmentDef)}()" +
Expand All @@ -343,7 +343,7 @@ public static bool AddEquipmentDef(EquipmentDef equipmentDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddBuffDef(BuffDef buffDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<BuffDef>())
{
if (buffDef.eliteDef && buffDef.eliteDef.eliteEquipmentDef && buffDef.eliteDef.eliteEquipmentDef.passiveBuffDef != buffDef)
Expand All @@ -369,7 +369,7 @@ public static bool AddBuffDef(BuffDef buffDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddEliteDef(EliteDef eliteDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<EliteDef>())
{
ContentManagementPlugin.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {eliteDef} via {nameof(ContentAddition)}.{nameof(AddEliteDef)}()" +
Expand All @@ -389,7 +389,7 @@ public static bool AddEliteDef(EliteDef eliteDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddUnlockableDef(UnlockableDef unlockableDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<UnlockableDef>())
{
R2APIContentManager.HandleContentAddition(asm, unlockableDef);
Expand All @@ -408,7 +408,7 @@ public static bool AddUnlockableDef(UnlockableDef unlockableDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddSurvivorDef(SurvivorDef survivorDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<SurvivorDef>())
{
if (!survivorDef.bodyPrefab)
Expand Down Expand Up @@ -439,7 +439,7 @@ public static bool AddSurvivorDef(SurvivorDef survivorDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddArtifactDef(ArtifactDef artifactDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<ArtifactDef>())
{
if (artifactDef.smallIconDeselectedSprite == null || artifactDef.smallIconSelectedSprite == null)
Expand All @@ -461,7 +461,7 @@ public static bool AddArtifactDef(ArtifactDef artifactDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddSurfaceDef(SurfaceDef surfaceDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<SurfaceDef>())
{
if (surfaceDef.impactEffectPrefab == null || surfaceDef.footstepEffectPrefab == null)
Expand All @@ -483,7 +483,7 @@ public static bool AddSurfaceDef(SurfaceDef surfaceDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddNetworkSoundEventDef(NetworkSoundEventDef networkSoundEventDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<NetworkSoundEventDef>())
{
if (string.IsNullOrEmpty(networkSoundEventDef.eventName) || string.IsNullOrWhiteSpace(networkSoundEventDef.eventName))
Expand All @@ -505,7 +505,7 @@ public static bool AddNetworkSoundEventDef(NetworkSoundEventDef networkSoundEven
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddMusicTrackDef(MusicTrackDef musicTrackDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<MusicTrackDef>())
{
R2APIContentManager.HandleContentAddition(asm, musicTrackDef);
Expand All @@ -521,7 +521,7 @@ public static bool AddMusicTrackDef(MusicTrackDef musicTrackDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddGameEndingDef(GameEndingDef gameEndingDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<GameEndingDef>())
{
R2APIContentManager.HandleContentAddition(asm, gameEndingDef);
Expand All @@ -539,7 +539,7 @@ public static bool AddGameEndingDef(GameEndingDef gameEndingDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddEntityStateConfiguration(EntityStateConfiguration entityStateConfiguration)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<EntityStateConfiguration>())
{
Type type = Type.GetType(entityStateConfiguration.targetType.assemblyQualifiedName);
Expand Down Expand Up @@ -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<ExpansionDef>())
{
R2APIContentManager.HandleContentAddition(asm, expansionDef);
Expand All @@ -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<EntitlementDef>())
{
R2APIContentManager.HandleContentAddition(asm, entitlementDef);
Expand All @@ -604,7 +604,7 @@ public static bool AddEntitlementDef(EntitlementDef entitlementDef)
/// <returns>true if valid and added, false if one of the requirements is not met</returns>
public static bool AddMiscPickupDef(MiscPickupDef miscPickupDef)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();
if (CatalogBlockers.GetAvailability<MiscPickupDef>())
{
R2APIContentManager.HandleContentAddition(asm, miscPickupDef);
Expand Down Expand Up @@ -635,7 +635,7 @@ public static SerializableEntityStateType AddEntityState<T>(out bool wasAdded) w
/// <returns>A SerializableEntityStateType, the StateType will be null if wasAdded is false.</returns>
public static SerializableEntityStateType AddEntityState(Type entityStateType, out bool wasAdded)
{
var asm = Assembly.GetCallingAssembly();
var asm = GetNonAPICaller();

if(CatalogBlockers.GetAvailability<EntityState>())
{
Expand Down Expand Up @@ -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<T>(GameObject obj) where T : Component => obj.GetComponent<T>();
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
}
3 changes: 3 additions & 0 deletions R2API.ContentManagement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion R2API.ContentManagement/thunderstore.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e569710

Please sign in to comment.