Skip to content

Commit

Permalink
Fix custom achievements (#391)
Browse files Browse the repository at this point in the history
* fix custom achievements for current patch

* bump game ver

* always use latest package for game libs

* use foreach
  • Loading branch information
xiaoxiao921 authored Apr 23, 2022
1 parent 752ee2e commit 9012b33
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 57 deletions.
24 changes: 13 additions & 11 deletions R2API/R2API.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using BepInEx;
using BepInEx.Logging;
using MonoMod.RuntimeDetour;
using MonoMod.RuntimeDetour.HookGen;
using R2API.ContentManagement;
using R2API.Utils;
using RoR2;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using MonoMod.RuntimeDetour;
using MonoMod.RuntimeDetour.HookGen;
using R2API.ContentManagement;
using R2API.Utils;
using RoR2;
using UnityEngine;

namespace R2API {
Expand All @@ -26,9 +26,9 @@ public class R2API : BaseUnityPlugin {
public const string PluginName = "R2API";
public const string PluginVersion = "0.0.1";

private const string GameBuildId = "1.2.2.0";
private const string GameBuildId = "1.2.3.1";

internal new static ManualLogSource Logger { get; set; }
internal static new ManualLogSource Logger { get; set; }
public static bool DebugMode { get; private set; } = false;

internal static DetourModManager ModManager;
Expand Down Expand Up @@ -88,8 +88,9 @@ public static void LogDebug(object debugText, [CallerMemberName] string caller =
private static void CheckIfUsedOnRightGameVersion() {
var buildId = Application.version;

if (GameBuildId == buildId)
if (GameBuildId == buildId) {
return;
}

Logger.LogWarning($"This version of R2API was built for build id \"{GameBuildId}\", you are running \"{buildId}\".");
Logger.LogWarning("Should any problems arise, please check for a new version before reporting issues.");
Expand Down Expand Up @@ -173,8 +174,9 @@ private static void CheckForIncompatibleAssemblies() {
, RegexOptions.Compiled | RegexOptions.IgnoreCase))
.Select(x => x.Name));

if (info.Count == countEmpty)
if (info.Count == countEmpty) {
return;
}

Logger.LogBlockError(info);
}
Expand Down
10 changes: 7 additions & 3 deletions R2API/R2API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.0.*">
<PackageReference Include="BepInEx.Analyzers" Version="*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -28,9 +28,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="RiskOfRain2.GameLibs" Version="1.2.3.1-r.0" />
<PackageReference Include="RiskOfRain2.GameLibs" Version="*-*" />
<PackageReference Include="UnityEngine.Modules" Version="2019.4.26" />
<PackageReference Include="MMHOOK.RoR2" Version="2022.4.19">
<PackageReference Include="MMHOOK.RoR2" Version="*">
<NoWarn>NU1701</NoWarn>
</PackageReference>
</ItemGroup>
Expand All @@ -40,6 +40,10 @@
<HintPath>libs\Facepunch.Steamworks.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="RoR2BepInExPack">
<HintPath>libs\RoR2BepInExPack.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="SimpleJSON">
<HintPath>libs\SimpleJSON.dll</HintPath>
<Private>false</Private>
Expand Down
63 changes: 20 additions & 43 deletions R2API/UnlockableAPI.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using Mono.Cecil.Cil;
using MonoMod.Cil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using R2API.ContentManagement;
using R2API.Utils;
using RoR2;
using RoR2.Achievements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using RoR2BepInExPack.VanillaFixes;
using UnityEngine;

using BF = System.Reflection.BindingFlags;

namespace R2API {

// Original code from Rein and Rob
Expand Down Expand Up @@ -151,44 +148,24 @@ public static bool Loaded {

[R2APISubmoduleInit(Stage = InitStage.SetHooks)]
internal static void SetHooks() {
IL.RoR2.AchievementManager.CollectAchievementDefs += AddCustomAchievements;
}

[R2APISubmoduleInit(Stage = InitStage.UnsetHooks)]
internal static void UnsetHooks() {
IL.RoR2.AchievementManager.CollectAchievementDefs -= AddCustomAchievements;
SaferAchievementManager.OnCollectAchievementDefs += AddOurDefs;
}

private static void AddCustomAchievements(ILContext il) {
var achievementIdentifierField = typeof(AchievementManager).GetField(nameof(AchievementManager.achievementIdentifiers), BF.Public | BF.Static | BF.NonPublic);
if (achievementIdentifierField is null) {
throw new NullReferenceException($"Could not find field in {nameof(AchievementManager)}");
}

var cursor = new ILCursor(il);
cursor.GotoNext(MoveType.After,
x => x.MatchEndfinally(),
x => x.MatchLdloc(1)
);

void AddOurDefs(List<AchievementDef> achievementDefs, Dictionary<string, AchievementDef> stringToAchievementDef, List<string> identifiers) {
for (var i = 0; i < Achievements.Count; i++) {
var achievement = Achievements[i];

if (achievement is null) {
continue;
}

identifiers.Add(achievement.identifier);
achievementDefs.Add(achievement);
stringToAchievementDef.Add(achievement.identifier, achievement);
private static void AddOurDefs(List<string> identifiers, Dictionary<string, AchievementDef> stringToAchievementDef, List<AchievementDef> achievementDefs) {
foreach (var achievement in Achievements) {
if (achievement is null) {
continue;
}

identifiers.Add(achievement.identifier);
achievementDefs.Add(achievement);
stringToAchievementDef.Add(achievement.identifier, achievement);
}
}

cursor.Emit(OpCodes.Ldarg_0);
cursor.Emit(OpCodes.Ldsfld, achievementIdentifierField);
cursor.EmitDelegate<Action<List<AchievementDef>, Dictionary<string, AchievementDef>, List<string>>>(AddOurDefs);
cursor.Emit(OpCodes.Ldloc_1);
[R2APISubmoduleInit(Stage = InitStage.UnsetHooks)]
internal static void UnsetHooks() {
SaferAchievementManager.OnCollectAchievementDefs -= AddOurDefs;
}

internal static UnlockableDef CreateNewUnlockable(UnlockableInfo unlockableInfo) {
Expand Down Expand Up @@ -284,7 +261,7 @@ private static UnlockableDef AddUnlockableInternal(Type unlockableType, Assembly
throw new InvalidOperationException($"Too late ! Tried to add unlockable: {instance.UnlockableIdentifier} after the UnlockableCatalog");
}

string unlockableIdentifier = instance.UnlockableIdentifier;
var unlockableIdentifier = instance.UnlockableIdentifier;

if (!UnlockableIdentifiers.Add(unlockableIdentifier)) {
throw new InvalidOperationException($"The unlockable identifier '{unlockableIdentifier}' is already used by another mod.");
Expand All @@ -294,7 +271,7 @@ private static UnlockableDef AddUnlockableInternal(Type unlockableType, Assembly
unlockableDef = ScriptableObject.CreateInstance<UnlockableDef>();
}

UnlockableInfo unlockableInfo = new UnlockableInfo {
var unlockableInfo = new UnlockableInfo {
Name = instance.UnlockableIdentifier,
HowToUnlockString = instance.GetHowToUnlock,
UnlockedString = instance.GetUnlocked,
Expand Down
Binary file added R2API/libs/RoR2BepInExPack.dll
Binary file not shown.

0 comments on commit 9012b33

Please sign in to comment.