Skip to content

Commit

Permalink
Version 4.14.0
Browse files Browse the repository at this point in the history
 * FEATURE - New version of GoogleTranslate endpoint that supports new API. May replace replace old GoogleTranslate endpoint in the future
 * MISC - Made more log statements affected by silent mode
 * MISC - Changed behaviour of the copy to clipboard feature. Now it will always copy to clipboard when text is being translated on a text component
 * MISC - Support for older versions of UTAGE
 * MISC - Fixed some log statements to no longer indicate error in case an error did not occur during hooking
 * MISC - Updated user agents for API requests
 * MISC - Improved SpriteRenderer support
 * BUG FIX - Fixed a bug that could cause crash in older versions of the Unity Engine
 * BUG FIX - Fixed a bug that could occur during dynamic code generation in certain situations
  • Loading branch information
randoman committed Feb 1, 2021
1 parent f2dd6c6 commit efa4a59
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* MISC - Support for older versions of UTAGE
* MISC - Fixed some log statements to no longer indicate error in case an error did not occur during hooking
* MISC - Updated user agents for API requests
* MISC - Improved SpriteRenderer support
* BUG FIX - Fixed a bug that could cause crash in older versions of the Unity Engine
* BUG FIX - Fixed a bug that could occur during dynamic code generation in certain situations

Expand Down
6 changes: 4 additions & 2 deletions src/Translators/GoogleTranslate/GoogleTranslateEndpointV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private string FixLanguage( string lang )

public override void Initialize( IInitializationContext context )
{
var backendOverride = context.GetOrCreateSetting( "GoogleV2", "ServiceUrl", DefaultUserBackend );
var backendOverride = context.GetOrCreateSetting<string>( "GoogleV2", "ServiceUrl" );
if( !backendOverride.IsNullOrWhiteSpace() )
{
_selectedUserBackend = backendOverride;
Expand All @@ -97,7 +97,9 @@ public override void Initialize( IInitializationContext context )
}
else
{
throw new Exception( "ServiceUrl is empty." );
_selectedUserBackend = DefaultUserBackend;

_httpsServicePointTranslateTemplateUrl = _selectedUserBackend + HttpsServicePointTranslateTemplateUrl;
}

_translateRpcId = context.GetOrCreateSetting( "GoogleV2", "RPCID", "MkEWBc" );
Expand Down
118 changes: 100 additions & 18 deletions src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public class AutoTranslationPlugin : MonoBehaviour, IInternalTranslator, ITransl

private bool _isInTranslatedMode = true;
private bool _textHooksEnabled = true;
private bool _imageHooksEnabled = true;

private float _batchOperationSecondCounter = 0;

Expand Down Expand Up @@ -660,10 +659,24 @@ internal void Hook_TextChanged( object ui, bool onEnable )

internal void Hook_ImageChangedOnComponent( object source, ref Texture2D texture, bool isPrefixHooked, bool onEnable )
{
if( !_imageHooksEnabled ) return;
if( !CallOrigin.ImageHooksEnabled ) return;
if( !source.IsKnownImageType() ) return;

HandleImage( source, ref texture, isPrefixHooked );
Sprite _ = null;
HandleImage( source, ref _, ref texture, isPrefixHooked );

if( onEnable )
{
CheckSpriteRenderer( source );
}
}

internal void Hook_ImageChangedOnComponent( object source, ref Sprite sprite, ref Texture2D texture, bool isPrefixHooked, bool onEnable )
{
if( !CallOrigin.ImageHooksEnabled ) return;
if( !source.IsKnownImageType() ) return;

HandleImage( source, ref sprite, ref texture, isPrefixHooked );

if( onEnable )
{
Expand All @@ -673,10 +686,11 @@ internal void Hook_ImageChangedOnComponent( object source, ref Texture2D texture

internal void Hook_ImageChanged( ref Texture2D texture, bool isPrefixHooked )
{
if( !_imageHooksEnabled ) return;
if( !CallOrigin.ImageHooksEnabled ) return;
if( texture == null ) return;

HandleImage( null, ref texture, isPrefixHooked );
Sprite _ = null;
HandleImage( null, ref _, ref texture, isPrefixHooked );
}

private bool DiscoverComponent( object ui, TextTranslationInfo info )
Expand Down Expand Up @@ -852,7 +866,7 @@ private string TranslateOrQueueWebJob( object ui, string text, bool ignoreCompon
tc );
}

private void HandleImage( object source, ref Texture2D texture, bool isPrefixHooked )
private void HandleImage( object source, ref Sprite sprite, ref Texture2D texture, bool isPrefixHooked )
{
if( Settings.EnableTextureDumping )
{
Expand All @@ -870,7 +884,7 @@ private void HandleImage( object source, ref Texture2D texture, bool isPrefixHoo
{
try
{
TranslateTexture( source, ref texture, isPrefixHooked, null );
TranslateTexture( source, ref sprite, ref texture, isPrefixHooked, null );
}
catch( Exception e )
{
Expand All @@ -879,31 +893,45 @@ private void HandleImage( object source, ref Texture2D texture, bool isPrefixHoo
}
}

private void TranslateTexture( object ui, ref Sprite sprite, TextureReloadContext context )
{
if( ui is Texture2D texture2d )
{
TranslateTexture( null, ref sprite, ref texture2d, false, context );
}
else
{
Texture2D _ = null;
TranslateTexture( ui, ref sprite, ref _, false, context );
}
}

private void TranslateTexture( object ui, TextureReloadContext context )
{
Sprite __ = null;
if( ui is Texture2D texture2d )
{
TranslateTexture( null, ref texture2d, false, context );
TranslateTexture( null, ref __, ref texture2d, false, context );
}
else
{
Texture2D _ = null;
TranslateTexture( ui, ref _, false, context );
TranslateTexture( ui, ref __, ref _, false, context );
}
}

private void TranslateTexture( object source, ref Texture2D texture, bool isPrefixHooked, TextureReloadContext context )
private void TranslateTexture( object source, ref Sprite sprite, ref Texture2D texture, bool isPrefixHooked, TextureReloadContext context )
{
try
{
_imageHooksEnabled = false;
CallOrigin.ImageHooksEnabled = false;

var previousTextureValue = texture;
texture = texture ?? source.GetTexture();
if( texture == null ) return;

var tti = texture.GetOrCreateTextureTranslationInfo();
var iti = source.GetOrCreateImageTranslationInfo();
var iti = source.GetOrCreateImageTranslationInfo( texture );
var key = tti.GetKey();
if( string.IsNullOrEmpty( key ) ) return;

Expand All @@ -915,6 +943,52 @@ private void TranslateTexture( object source, ref Texture2D texture, bool isPref
forceReload = context.RegisterTextureInContextAndDetermineWhetherToReload( texture );
}

if( Settings.EnableLegacyTextureLoading
&& Settings.EnableSpriteRendererHooking
&& iti?.IsTranslated == true
&& source is SpriteRenderer sr )
{

var originalTexture = tti.Original.Target;
var translatedTexture = tti.Translated;
if( texture == originalTexture && tti.IsTranslated )
{
// if the texture is the original, we update the sprite
if( tti.TranslatedSprite != null )
{
if( isPrefixHooked )
{
if( sprite != null )
{
sprite = tti.TranslatedSprite;
}
}
else
{
sr.sprite = tti.TranslatedSprite;
}
}
}
else if( texture == translatedTexture ) // can only happen if && tti.IsTranslated
{
// if the texture is the translated, we do not need to do anything
}
else
{
// if the texture is neither the original or the translated, we must reset

iti.Reset( texture );

if( tti.IsTranslated )
{
if( isPrefixHooked && sprite != null && tti.TranslatedSprite != null )
{
sprite = tti.TranslatedSprite;
}
}
}
}

if( TextureCache.TryGetTranslatedImage( key, out var newData ) )
{
if( _isInTranslatedMode )
Expand Down Expand Up @@ -950,7 +1024,15 @@ private void TranslateTexture( object source, ref Texture2D texture, bool isPref
{
if( Settings.EnableLegacyTextureLoading )
{
source.SetTexture( tti.Translated );
var newSprite = source.SetTexture( tti.Translated, sprite, isPrefixHooked );
if( newSprite != null )
{
tti.TranslatedSprite = newSprite;
if( isPrefixHooked && sprite != null )
{
sprite = newSprite;
}
}
}

if( !isPrefixHooked )
Expand Down Expand Up @@ -1006,7 +1088,7 @@ private void TranslateTexture( object source, ref Texture2D texture, bool isPref
var original = tti.Original.Target;
if( Settings.EnableLegacyTextureLoading && original != null )
{
source.SetTexture( original );
source.SetTexture( original, null, isPrefixHooked );
}

if( !isPrefixHooked )
Expand Down Expand Up @@ -1061,7 +1143,7 @@ private void TranslateTexture( object source, ref Texture2D texture, bool isPref
var original = tti.Original.Target;
if( Settings.EnableLegacyTextureLoading && original != null )
{
source.SetTexture( original );
source.SetTexture( original, null, isPrefixHooked );
}

if( !isPrefixHooked )
Expand Down Expand Up @@ -1112,15 +1194,15 @@ private void TranslateTexture( object source, ref Texture2D texture, bool isPref
}
finally
{
_imageHooksEnabled = true;
CallOrigin.ImageHooksEnabled = true;
}
}

private void DumpTexture( object source, Texture2D texture )
{
try
{
_imageHooksEnabled = false;
CallOrigin.ImageHooksEnabled = false;

texture = texture ?? source.GetTexture();
if( texture == null ) return;
Expand Down Expand Up @@ -1151,7 +1233,7 @@ private void DumpTexture( object source, Texture2D texture )
}
finally
{
_imageHooksEnabled = true;
CallOrigin.ImageHooksEnabled = true;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/XUnity.AutoTranslator.Plugin.Core/CallOrigin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
{
internal static class CallOrigin
{
public static bool ImageHooksEnabled = true;
public static bool ExpectsTextToBeReturned = false;
public static IReadOnlyTextTranslationCache TextCache = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,23 @@ public static Texture2D GetTexture( this object ui )
}
}

public static void SetTexture( this object ui, Texture2D texture )
public static Sprite SetTexture( this object ui, Texture2D texture, Sprite sprite, bool isPrefixHooked )
{
if( ui == null ) return;
if( ui == null ) return null;

var currentTexture = ui.GetTexture();

if( currentTexture != texture )
{
if( Settings.EnableSpriteRendererHooking && ui is SpriteRenderer sr )
{
var sprite = sr.sprite;
if( sprite != null )
if( isPrefixHooked )
{
SafeSetSprite( sr, texture );
return SafeCreateSprite( sr, sprite, texture );
}
else
{
return SafeSetSprite( sr, sprite, texture );
}
}
else
Expand All @@ -83,12 +86,21 @@ public static void SetTexture( this object ui, Texture2D texture )
}
}
}

return null;
}

private static void SafeSetSprite( SpriteRenderer sr, Texture2D texture )
private static Sprite SafeSetSprite( SpriteRenderer sr, Sprite sprite, Texture2D texture )
{
var newSprite = Sprite.Create( texture, sr.sprite.rect, Vector2.zero );
var newSprite = Sprite.Create( texture, sprite != null ? sprite.rect : sr.sprite.rect, Vector2.zero );
sr.sprite = newSprite;
return newSprite;
}

private static Sprite SafeCreateSprite( SpriteRenderer sr, Sprite sprite, Texture2D texture )
{
var newSprite = Sprite.Create( texture, sprite != null ? sprite.rect : sr.sprite.rect, Vector2.zero );
return newSprite;
}

public static void SetAllDirtyEx( this object ui )
Expand All @@ -101,7 +113,7 @@ public static void SetAllDirtyEx( this object ui )
{
ClrTypes.Graphic.CachedMethod( SetAllDirtyMethodName ).Invoke( ui );
}
else
else if( ui is not SpriteRenderer )
{
AccessToolsShim.Method( type, MarkAsChangedMethodName )?.Invoke( ui, null );
}
Expand Down
37 changes: 30 additions & 7 deletions src/XUnity.AutoTranslator.Plugin.Core/Hooks/ImageHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using XUnity.AutoTranslator.Plugin.Core.Extensions;
using XUnity.Common.Constants;
using XUnity.Common.Harmony;
using XUnity.Common.Logging;
using XUnity.Common.MonoMod;

namespace XUnity.AutoTranslator.Plugin.Core.Hooks
Expand Down Expand Up @@ -135,24 +136,46 @@ static MethodBase TargetMethod( object instance )
return AccessToolsShim.Property( ClrTypes.SpriteRenderer, "sprite" )?.GetSetMethod();
}

public static void Postfix( object __instance )
public static void Prefix( object __instance, ref Sprite value )
{
Texture2D _ = null;
AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, ref _, false, false );
Texture2D texture;
var prev = CallOrigin.ImageHooksEnabled;
CallOrigin.ImageHooksEnabled = false;
try
{
texture = value.texture;
}
finally
{
CallOrigin.ImageHooksEnabled = prev;
}
AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, ref value, ref texture, true, false );
}

static Action<object, object> _original;
//public static void Postfix( object __instance, ref Sprite value )
//{
// Texture2D _ = null;
// AutoTranslationPlugin.Current.Hook_ImageChangedOnComponent( __instance, ref _, false, false );
//}

static Action<object, Sprite> _original;

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

static void MM_Detour( object __instance, object sprite )
static void MM_Detour( object __instance, Sprite sprite )
{
//var prev = sprite;
Prefix( __instance, ref sprite );

_original( __instance, sprite );

Postfix( __instance );
//if( prev != sprite )
//{
// Postfix( __instance, ref sprite );
//}
}
}

Expand Down
Loading

0 comments on commit efa4a59

Please sign in to comment.