Skip to content

Commit

Permalink
Add NGUI support to TextGetterCompatibilityMode (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco authored Jan 31, 2025
1 parent e347936 commit 700a563
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace XUnity.AutoTranslator.Plugin.Core.Extensions
{
Expand All @@ -11,5 +9,10 @@ public static Type UnwrapNullable( this Type type )
{
return Nullable.GetUnderlyingType( type ) ?? type;
}

public static bool IsAssemblyCsharp( this Assembly originalAssembly)
{
return originalAssembly.GetName().Name.Equals( "Assembly-CSharp" );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal static class TextGetterCompatHooks
public static readonly Type[] All = new[] {
typeof( Text_text_Hook ),
typeof( TMP_Text_text_Hook ),
typeof( NGUI_Text_text_Hook )
};
}

Expand Down Expand Up @@ -86,6 +87,40 @@ static string MM_Detour( object __instance )
return result;
}
}

internal static class NGUI_Text_text_Hook
{
static bool Prepare( object instance )
{
return UnityTypes.UILabel != null;
}

static MethodBase TargetMethod( object instance )
{
return AccessToolsShim.Property( UnityTypes.UILabel.ClrType, "text" )?.GetGetMethod();
}

static void Postfix( object __instance, ref string __result )
{
TextGetterCompatModeHelper.ReplaceTextWithOriginal( __instance, ref __result );
}

static Func<object, string> _original;

static void MM_Init( object detour )
{
_original = detour.GenerateTrampolineEx<Func<object, string>>();
}

static string MM_Detour( object __instance )
{
var result = _original( __instance );

Postfix( __instance, ref result );

return result;
}
}
}

#endif
33 changes: 22 additions & 11 deletions src/XUnity.AutoTranslator.Plugin.Core/TextGetterCompatModeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,32 @@ public static void ReplaceTextWithOriginal( object instance, ref string __result
var tti = instance.GetTextTranslationInfo();
if( tti?.IsTranslated == true )
{
// 0. This method
// 1. Postfix
// 2. _Postfix
// 3. Harmony-related trampoline method/MM_Detour
// 4. Original method
var callingMethod = new StackFrame( 4 ).GetMethod();

var callingAssembly = callingMethod.DeclaringType.Assembly;

var originalAssembly = instance.GetType().Assembly;
if( callingAssembly != originalAssembly )
var withinGameCode = originalAssembly.IsAssemblyCsharp();
if( withinGameCode )
{
// if the assembly is not the same, it may be call from the game or another mod, so replace
// If the UI element is inside Assembly-CSharp it's not possible to tell if it's called by game code or UI code
// This happens in NGUI, in which always replacing doesn't seem to cause any issues
__result = tti.OriginalText;
}
else
{
// BUG: This seems to not be right, at least in the latest BepInEx5 there seems to be one less stack frame
// 0. This method
// 1. Postfix
// 2. _Postfix
// 3. Harmony-related trampoline method/MM_Detour
// 4. Original method
var callingMethod = new StackFrame( 4 ).GetMethod();

var callingAssembly = callingMethod.DeclaringType.Assembly;

if( callingAssembly != originalAssembly )
{
// if the assembly is not the same, it may be call from the game or another mod, so replace
__result = tti.OriginalText;
}
}
}
}
}
Expand Down

0 comments on commit 700a563

Please sign in to comment.