Skip to content

Commit

Permalink
fix external hook bug
Browse files Browse the repository at this point in the history
updated version
  • Loading branch information
randoman committed Oct 16, 2018
1 parent 7fa8103 commit 8505340
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 27 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
### 2.15.1
### 2.15.2
* BUG FIX - Fixed bug that could cause hooking not to work when hooks were overriden by external plugin

### 2.15.1
* BUG FIX - Updated TKK retrieval logic. Improved error message if it cannot be retrieved.

### 2.15.0
Expand Down
2 changes: 1 addition & 1 deletion src/XUnity.AutoTranslator.Patcher/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string Version
{
get
{
return "2.15.1";
return "2.15.2";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<Version>2.15.1</Version>
<Version>2.15.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
44 changes: 29 additions & 15 deletions src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,18 @@ public string Hook_TextChanged_WithResult( object ui, string text )

if( _hooksEnabled && !_temporarilyDisabled )
{
return TranslateOrQueueWebJob( ui, text );
return TranslateOrQueueWebJob( ui, text, false );
}
return null;
}

public string ExternalHook_TextChanged_WithResult( object ui, string text )
{
if( !ui.IsKnownType() ) return null;

if( _hooksEnabled && !_temporarilyDisabled )
{
return TranslateOrQueueWebJob( ui, text, true );
}
return null;
}
Expand All @@ -668,7 +679,7 @@ public void Hook_TextChanged( object ui )
{
if( _hooksEnabled && !_temporarilyDisabled )
{
TranslateOrQueueWebJob( ui, null );
TranslateOrQueueWebJob( ui, null, false );
}
}

Expand Down Expand Up @@ -766,7 +777,7 @@ private bool IsShortText( string str )
return str.Length <= ( Settings.MaxCharactersPerTranslation / 2 );
}

public bool ShouldTranslate( object ui )
public bool ShouldTranslate( object ui, bool ignoreComponentState )
{
var component = ui as Component;
if( component != null )
Expand All @@ -779,10 +790,13 @@ public bool ShouldTranslate( object ui )
return false;
}

var behaviour = component as Behaviour;
if( behaviour?.isActiveAndEnabled == false )
if( !ignoreComponentState )
{
return false;
var behaviour = component as Behaviour;
if( behaviour?.isActiveAndEnabled == false )
{
return false;
}
}

var inputField = component.gameObject.GetFirstComponentInSelfOrAncestor( Constants.Types.InputField )
Expand All @@ -794,26 +808,26 @@ public bool ShouldTranslate( object ui )
return true;
}

private string TranslateOrQueueWebJob( object ui, string text )
private string TranslateOrQueueWebJob( object ui, string text, bool ignoreComponentState )
{
var info = ui.GetTranslationInfo();

if( _ongoingOperations.Contains( ui ) )
{
return TranslateImmediate( ui, text, info );
return TranslateImmediate( ui, text, info, ignoreComponentState );
}

var supportsStabilization = ui.SupportsStabilization();
if( Settings.Delay == 0 || !supportsStabilization )
{
return TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization );
return TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization, ignoreComponentState );
}
else
{
StartCoroutine(
DelayForSeconds( Settings.Delay, () =>
{
TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization );
TranslateOrQueueWebJobImmediate( ui, text, info, supportsStabilization, ignoreComponentState );
} ) );
}

Expand All @@ -827,12 +841,12 @@ public static bool IsCurrentlySetting( TranslationInfo info )
return info.IsCurrentlySettingText;
}

private string TranslateImmediate( object ui, string text, TranslationInfo info )
private string TranslateImmediate( object ui, string text, TranslationInfo info, bool ignoreComponentState )
{
// Get the trimmed text
text = ( text ?? ui.GetText() ).TrimIfConfigured();

if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui ) && !IsCurrentlySetting( info ) )
if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui, ignoreComponentState ) && !IsCurrentlySetting( info ) )
{
info?.Reset( text );

Expand All @@ -857,7 +871,7 @@ private string TranslateImmediate( object ui, string text, TranslationInfo info
/// Translates the string of a UI text or queues it up to be translated
/// by the HTTP translation service.
/// </summary>
private string TranslateOrQueueWebJobImmediate( object ui, string text, TranslationInfo info, bool supportsStabilization, TranslationContext context = null )
private string TranslateOrQueueWebJobImmediate( object ui, string text, TranslationInfo info, bool supportsStabilization, bool ignoreComponentState, TranslationContext context = null )
{
// make sure text exists
text = text ?? ui.GetText();
Expand All @@ -870,7 +884,7 @@ private string TranslateOrQueueWebJobImmediate( object ui, string text, Translat
//Logger.Current.Debug( ui.GetType().Name + ": " + text );

// Ensure that we actually want to translate this text and its owning UI element.
if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui ) && !IsCurrentlySetting( info ) )
if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui, ignoreComponentState ) && !IsCurrentlySetting( info ) )
{
info?.Reset( text );
var isSpammer = ui.IsSpammingComponent();
Expand Down Expand Up @@ -1049,7 +1063,7 @@ private string TranslateOrQueueWebJobImmediateByParserResult( object ui, ParserR
{
// incomplete, must start job
var context = new TranslationContext( ui, result );
TranslateOrQueueWebJobImmediate( ui, value, null, false, context );
TranslateOrQueueWebJobImmediate( ui, value, null, false, true, context );
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public static class PluginData

public const string Name = "XUnity Auto Translator";

public const string Version = "2.15.1";
public const string Version = "2.15.2";
}
}
8 changes: 4 additions & 4 deletions src/XUnity.AutoTranslator.Plugin.Core/Hooks/HooksSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void InstallHooks()
{
if( Settings.EnableUGUI || Settings.EnableUtage )
{
success = SetupHook( KnownEvents.OnUnableToTranslateUGUI, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
success = SetupHook( KnownEvents.OnUnableToTranslateUGUI, AutoTranslationPlugin.Current.ExternalHook_TextChanged_WithResult );
if( !success )
{
harmony.PatchAll( UGUIHooks.All );
Expand All @@ -44,7 +44,7 @@ public static void InstallHooks()
{
if( Settings.EnableTextMeshPro )
{
success = SetupHook( KnownEvents.OnUnableToTranslateTextMeshPro, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
success = SetupHook( KnownEvents.OnUnableToTranslateTextMeshPro, AutoTranslationPlugin.Current.ExternalHook_TextChanged_WithResult );
if( !success )
{
harmony.PatchAll( TextMeshProHooks.All );
Expand All @@ -60,7 +60,7 @@ public static void InstallHooks()
{
if( Settings.EnableNGUI )
{
success = SetupHook( KnownEvents.OnUnableToTranslateNGUI, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
success = SetupHook( KnownEvents.OnUnableToTranslateNGUI, AutoTranslationPlugin.Current.ExternalHook_TextChanged_WithResult );
if( !success )
{
harmony.PatchAll( NGUIHooks.All );
Expand All @@ -76,7 +76,7 @@ public static void InstallHooks()
{
if( Settings.EnableIMGUI )
{
success = SetupHook( KnownEvents.OnUnableToTranslateNGUI, AutoTranslationPlugin.Current.Hook_TextChanged_WithResult );
success = SetupHook( KnownEvents.OnUnableToTranslateNGUI, AutoTranslationPlugin.Current.ExternalHook_TextChanged_WithResult );
if( !success )
{
harmony.PatchAll( IMGUIHooks.All );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<Version>2.15.1</Version>
<Version>2.15.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<Version>2.15.1</Version>
<Version>2.15.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<Version>2.15.1</Version>
<Version>2.15.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net40</TargetFramework>
<AssemblyName>SetupReiPatcherAndAutoTranslator</AssemblyName>
<Version>2.15.1</Version>
<Version>2.15.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 8505340

Please sign in to comment.