Skip to content

Commit

Permalink
disable IMGUI hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gravydevsupreme committed May 4, 2018
1 parent b8efba1 commit 2320985
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
<Version>2.4.0</Version>
<AssemblyVersion>2.4.1.0</AssemblyVersion>
<FileVersion>2.4.1.0</FileVersion>
<Version>2.4.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
81 changes: 40 additions & 41 deletions src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ private void LoadTranslations()

if( !string.IsNullOrEmpty( key ) && !string.IsNullOrEmpty( value ) )
{
AddTranslation( key, value );
var translationKey = new TranslationKeys( key );
AddTranslation( translationKey, value );
}
}
}
Expand All @@ -212,75 +213,68 @@ private void LoadTranslations()
}
}

private TranslationJob GetOrCreateTranslationJobFor( string untranslatedText )
private TranslationJob GetOrCreateTranslationJobFor( TranslationKeys key )
{
if( _unstartedJobs.TryGetValue( untranslatedText, out TranslationJob job ) )
if( _unstartedJobs.TryGetValue( key.ForcedRelevantKey, out TranslationJob job ) )
{
return job;
}

foreach( var completedJob in _completedJobs )
{
if( completedJob.UntranslatedText == untranslatedText )
if( completedJob.Keys.ForcedRelevantKey == key.ForcedRelevantKey )
{
return completedJob;
}
}

job = new TranslationJob( untranslatedText );
_unstartedJobs.Add( untranslatedText, job );
job = new TranslationJob( key );
_unstartedJobs.Add( key.ForcedRelevantKey, job );

return job;
}

private void AddTranslation( string key, string value )
private void AddTranslation( TranslationKeys key, string value )
{
_translations[ key ] = value;
_translations[ key.OriginalKey ] = value;
_translatedTexts.Add( value );

if( Settings.IgnoreWhitespaceInDialogue )
if( Settings.IgnoreWhitespaceInDialogue && key.IsDialogue )
{
var newKey = key.ChangeToSingleLineForDialogue();
_translations[ newKey ] = value;
_translations[ key.DialogueKey ] = value;
}
}

private void QueueNewUntranslatedForClipboard( string key )
private void QueueNewUntranslatedForClipboard( TranslationKeys key )
{
if( Settings.CopyToClipboard )
{
key = key.ChangeToSingleLineForDialogue();
if( !_textsToCopyToClipboard.Contains( key ) )
if( !_textsToCopyToClipboard.Contains( key.ForcedRelevantKey ) )
{
_textsToCopyToClipboard.Add( key );
_textsToCopyToClipboardOrdered.Add( key );
_textsToCopyToClipboard.Add( key.ForcedRelevantKey );
_textsToCopyToClipboardOrdered.Add( key.ForcedRelevantKey );

_clipboardUpdated = Time.realtimeSinceStartup;
}
}
}

private void QueueNewUntranslatedForDisk( string key )
private void QueueNewUntranslatedForDisk( TranslationKeys key )
{
if( Settings.IgnoreWhitespaceInDialogue )
{
key = key.ChangeToSingleLineForDialogue();
}

_newUntranslated.Add( key );
_newUntranslated.Add( key.RelevantKey );
}

private void QueueNewTranslationForDisk( string key, string value )
private void QueueNewTranslationForDisk( TranslationKeys key, string value )
{
lock( _writeToFileSync )
{
_newTranslations[ key ] = value;
_newTranslations[ key.RelevantKey ] = value;
}
}

private bool TryGetTranslation( string key, out string value )
private bool TryGetTranslation( TranslationKeys key, out string value )
{
return _translations.TryGetValue( key, out value ) || ( Settings.IgnoreWhitespaceInDialogue && _translations.TryGetValue( key.RemoveWhitespace(), out value ) );
return _translations.TryGetValue( key.OriginalKey, out value ) || ( Settings.IgnoreWhitespaceInDialogue && _translations.TryGetValue( key.DialogueKey, out value ) );
}

private string Override_TextChanged( object ui, string text )
Expand Down Expand Up @@ -442,11 +436,13 @@ private string TranslateOrQueueWebJobImmediate( object ui, string text, Translat
{
info?.Reset( text );

var textKey = new TranslationKeys( text );

// if we already have translation loaded in our _translatios dictionary, simply load it and set text
string translation;
if( TryGetTranslation( text, out translation ) )
if( TryGetTranslation( textKey, out translation ) )
{
QueueNewUntranslatedForClipboard( text );
QueueNewUntranslatedForClipboard( textKey );

if( !string.IsNullOrEmpty( translation ) )
{
Expand Down Expand Up @@ -488,12 +484,14 @@ private string TranslateOrQueueWebJobImmediate( object ui, string text, Translat
if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
{
QueueNewUntranslatedForClipboard( text );
var stabilizedTextKey = new TranslationKeys( stabilizedText );
QueueNewUntranslatedForClipboard( stabilizedTextKey );
info?.Reset( stabilizedText );
// once the text has stabilized, attempt to look it up
if( TryGetTranslation( stabilizedText, out translation ) )
if( TryGetTranslation( stabilizedTextKey, out translation ) )
{
if( !string.IsNullOrEmpty( translation ) )
{
Expand All @@ -505,12 +503,12 @@ private string TranslateOrQueueWebJobImmediate( object ui, string text, Translat
// Lets try not to spam a service that might not be there...
if( AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors )
{
var job = GetOrCreateTranslationJobFor( stabilizedText );
var job = GetOrCreateTranslationJobFor( stabilizedTextKey );
job.Components.Add( ui );
}
else
{
QueueNewUntranslatedForDisk( stabilizedText );
QueueNewUntranslatedForDisk( stabilizedTextKey );
}
}
}
Expand All @@ -528,16 +526,16 @@ private string TranslateOrQueueWebJobImmediate( object ui, string text, Translat
{
_startedOperationsForNonStabilizableComponents.Add( text );

QueueNewUntranslatedForClipboard( text );
QueueNewUntranslatedForClipboard( textKey );

// Lets try not to spam a service that might not be there...
if( AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors )
{
GetOrCreateTranslationJobFor( text );
GetOrCreateTranslationJobFor( textKey );
}
else
{
QueueNewUntranslatedForDisk( text );
QueueNewUntranslatedForDisk( textKey );
}
}
}
Expand Down Expand Up @@ -637,7 +635,7 @@ private void KickoffTranslations()
// lets see if the text should still be translated before kicking anything off
if( !job.AnyComponentsStillHasOriginalUntranslatedText() ) continue;

StartCoroutine( AutoTranslateClient.TranslateByWWW( job.UntranslatedDialogueText, Settings.FromLanguage, Settings.Language, translatedText =>
StartCoroutine( AutoTranslateClient.TranslateByWWW( job.Keys.ForcedRelevantKey, Settings.FromLanguage, Settings.Language, translatedText =>
{
_consecutiveErrors = 0;
Expand All @@ -651,7 +649,7 @@ private void KickoffTranslations()
if( !string.IsNullOrEmpty( translatedText ) )
{
QueueNewTranslationForDisk( Settings.IgnoreWhitespaceInDialogue ? job.UntranslatedDialogueText : job.UntranslatedText, translatedText );
QueueNewTranslationForDisk( job.Keys, translatedText );
_completedJobs.Add( job );
}
Expand Down Expand Up @@ -683,14 +681,14 @@ private void FinishTranslations()
{
// update the original text, but only if it has not been chaanged already for some reason (could be other translator plugin or game itself)
var text = component.GetText().Trim();
if( text == job.UntranslatedText )
if( text == job.Keys.OriginalKey )
{
var info = component.GetTranslationInfo( false );
SetTranslatedText( component, job.TranslatedText, info );
}
}

AddTranslation( job.UntranslatedText, job.TranslatedText );
AddTranslation( job.Keys, job.TranslatedText );
}
}
}
Expand All @@ -704,7 +702,8 @@ private void ReloadTranslations()
var info = kvp.Value as TranslationInfo;
if( info != null && !string.IsNullOrEmpty( info.OriginalText ) )
{
if( TryGetTranslation( info.OriginalText, out string translatedText ) && !string.IsNullOrEmpty( translatedText ) )
var key = new TranslationKeys( info.OriginalText );
if( TryGetTranslation( key, out string translatedText ) && !string.IsNullOrEmpty( translatedText ) )
{
SetTranslatedText( kvp.Key, translatedText, info );
}
Expand Down
11 changes: 6 additions & 5 deletions src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class Settings
public static int MaxCharactersPerTranslation;
public static bool EnablePrintHierarchy;
public static string AutoTranslationsFilePath;
public static bool EnableIMGUI;
public static bool EnableIMGUI = false;
public static bool EnableUGUI;
public static bool EnableNGUI;
public static bool EnableTextMeshPro;
Expand All @@ -36,7 +36,8 @@ public static class Settings
public static string BaiduAppId;
public static string BaiduAppSecret;
public static int ForceSplitTextAfterCharacters;
public static bool CopyToClipboard;

public static bool CopyToClipboard = false;
public static int MaxClipboardCopyCharacters;

public static void Configure()
Expand Down Expand Up @@ -68,7 +69,7 @@ public static void Configure()
TranslationDirectory = Config.Current.Preferences[ "Files" ][ "Directory" ].GetOrDefault( @"Translation" );
OutputFile = Config.Current.Preferences[ "Files" ][ "OutputFile" ].GetOrDefault( @"Translation\_AutoGeneratedTranslations.{lang}.txt" );

EnableIMGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableIMGUI" ].GetOrDefault( true );
//EnableIMGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableIMGUI" ].GetOrDefault( false );
EnableUGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableUGUI" ].GetOrDefault( true );
EnableNGUI = Config.Current.Preferences[ "TextFrameworks" ][ "EnableNGUI" ].GetOrDefault( true );
EnableTextMeshPro = Config.Current.Preferences[ "TextFrameworks" ][ "EnableTextMeshPro" ].GetOrDefault( true );
Expand All @@ -79,8 +80,8 @@ public static void Configure()
IgnoreWhitespaceInDialogue = Config.Current.Preferences[ "Behaviour" ][ "IgnoreWhitespaceInDialogue" ].GetOrDefault( true );
MinDialogueChars = Config.Current.Preferences[ "Behaviour" ][ "MinDialogueChars" ].GetOrDefault( 20 );
ForceSplitTextAfterCharacters = Config.Current.Preferences[ "Behaviour" ][ "ForceSplitTextAfterCharacters" ].GetOrDefault( 0 );
CopyToClipboard = Config.Current.Preferences[ "Behaviour" ][ "CopyToClipboard" ].GetOrDefault( false );
MaxClipboardCopyCharacters = Config.Current.Preferences[ "Behaviour" ][ "MaxClipboardCopyCharacters" ].GetOrDefault( 450 );
//CopyToClipboard = Config.Current.Preferences[ "Behaviour" ][ "CopyToClipboard" ].GetOrDefault( false );
//MaxClipboardCopyCharacters = Config.Current.Preferences[ "Behaviour" ][ "MaxClipboardCopyCharacters" ].GetOrDefault( 450 );

BaiduAppId = Config.Current.Preferences[ "Baidu" ][ "BaiduAppId" ].GetOrDefault( "" );
BaiduAppSecret = Config.Current.Preferences[ "Baidu" ][ "BaiduAppSecret" ].GetOrDefault( "" );
Expand Down
2 changes: 1 addition & 1 deletion src/XUnity.AutoTranslator.Plugin.Core/Hooks/HooksSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void InstallHooks( Func<object, string, string> defaultHook )
{
try
{
var harmony = HarmonyInstance.Create( "com.leakim1336.autotranslator" );
var harmony = HarmonyInstance.Create( "gravydevsupreme.xunity.autotranslator" );

bool success = false;
if( Settings.EnableUGUI )
Expand Down
13 changes: 5 additions & 8 deletions src/XUnity.AutoTranslator.Plugin.Core/TranslationJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ namespace XUnity.AutoTranslator.Plugin.Core
{
public class TranslationJob
{
public TranslationJob( string untranslatedText )
public TranslationJob( TranslationKeys key )
{
UntranslatedText = untranslatedText;
UntranslatedDialogueText = untranslatedText.ChangeToSingleLineForDialogue();
Keys = key;

Components = new List<object>();
}

public List<object> Components { get; private set; }

public string UntranslatedText { get; private set; }

public string UntranslatedDialogueText { get; private set; }

public TranslationKeys Keys { get; private set; }

public string TranslatedText { get; set; }

Expand All @@ -33,7 +30,7 @@ public bool AnyComponentsStillHasOriginalUntranslatedText()
foreach( var component in Components )
{
var text = component.GetText().Trim();
if( text == UntranslatedText )
if( text == Keys.OriginalKey )
{
return true;
}
Expand Down
28 changes: 28 additions & 0 deletions src/XUnity.AutoTranslator.Plugin.Core/TranslationKeys.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XUnity.AutoTranslator.Plugin.Core.Configuration;
using XUnity.AutoTranslator.Plugin.Core.Extensions;

namespace XUnity.AutoTranslator.Plugin.Core
{
public struct TranslationKeys
{
public TranslationKeys( string key )
{
OriginalKey = key;
DialogueKey = key.RemoveWhitespace();
}

public string OriginalKey { get; }

public string DialogueKey { get; }

public string ForcedRelevantKey => IsDialogue ? DialogueKey : OriginalKey;

public string RelevantKey => IsDialogue && Settings.IgnoreWhitespaceInDialogue ? DialogueKey : OriginalKey;

public bool IsDialogue => OriginalKey.Length > Settings.MinDialogueChars;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
<Version>2.4.0</Version>
<AssemblyVersion>2.4.1.0</AssemblyVersion>
<FileVersion>2.4.1.0</FileVersion>
<Version>2.4.1</Version>
</PropertyGroup>

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

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
<Version>2.4.0</Version>
<AssemblyVersion>2.4.1.0</AssemblyVersion>
<FileVersion>2.4.1.0</FileVersion>
<Version>2.4.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 2320985

Please sign in to comment.