-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* FEATURE - API to support easier manual translations of TextAsset resources * FEATURE - Support for not loading every translated image into memory permanently, but instead loading them on demand (new configuration option) * FEATURE - Better support for variations of Chinese as output language. 'zh-CN', 'zh-TW', 'zh-Hans', 'zh-Hant', 'zh' can now all be used by all translators if supported * FEATURE - Better support for variations of Chinese as input language. Plugin wont complain if 'zh-Hans', 'zh-Hant' or 'zh' is used * MISC - Support for older versions of UTAGE * MISC - New option that enables ignoring rules for calling virtual methods when setting the text of a text component: 'IgnoreVirtualTextSetterCallingRules' * MISC - Updated version of MonoMod distributed with the plugin * BUG FIX - Potential errors caused by using weak references incorrectly causing a race condition * BUG FIX - Potential error during initialization if Resource Redirector is not present
- Loading branch information
randoman
committed
Feb 28, 2020
1 parent
812a41f
commit e52a507
Showing
9 changed files
with
231 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/XUnity.AutoTranslator.Plugin.Core/Utilities/VirtcallRuleBreaker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
using MonoMod.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace XUnity.AutoTranslator.Plugin.Core.Utilities | ||
{ | ||
static class VirtcallRuleBreaker | ||
{ | ||
public static Delegate GenerateDelegate( MethodBase method, bool useCallVirt ) | ||
{ | ||
var parameterTypes = GetActualParameters( method ); | ||
var actionParameters = GetActionParameters( method ); | ||
var returnType = method is MethodInfo mi ? mi.ReturnType : typeof( void ); | ||
|
||
var dynamicMethod = new DynamicMethodDefinition( "DynamicMethod_" + method.Name, returnType, actionParameters ); | ||
|
||
var il = dynamicMethod.GetILProcessor(); | ||
if( actionParameters.Length >= 1 ) | ||
{ | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Ldarg_0 ); | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Castclass, parameterTypes[ 0 ] ); | ||
if( actionParameters.Length >= 2 ) | ||
{ | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Ldarg_1 ); | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Castclass, parameterTypes[ 1 ] ); | ||
if( actionParameters.Length >= 3 ) | ||
{ | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Ldarg_2 ); | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Castclass, parameterTypes[ 2 ] ); | ||
if( actionParameters.Length >= 4 ) | ||
{ | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Ldarg_3 ); | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Castclass, parameterTypes[ 3 ] ); | ||
} | ||
} | ||
} | ||
} | ||
il.Emit( useCallVirt ? Mono.Cecil.Cil.OpCodes.Callvirt : Mono.Cecil.Cil.OpCodes.Call, method ); | ||
il.Emit( Mono.Cecil.Cil.OpCodes.Ret ); | ||
|
||
var delegateType = returnType == typeof( void ) ? GetGenericActionType( actionParameters ) : GetGenericFuncType( actionParameters, returnType ); | ||
var actionOrFunc = dynamicMethod.Generate().CreateDelegate( delegateType ); | ||
return actionOrFunc; | ||
} | ||
|
||
static Type[] GetActionParameters( MethodBase method ) | ||
{ | ||
if( method.IsStatic ) | ||
{ | ||
return method.GetParameters().Select( x => typeof( object ) ).ToArray(); | ||
} | ||
else | ||
{ | ||
return new Type[] { typeof( object ) }.Concat( method.GetParameters().Select( x => typeof( object ) ) ).ToArray(); | ||
} | ||
} | ||
|
||
static Type[] GetActualParameters( MethodBase method ) | ||
{ | ||
if( method.IsStatic ) | ||
{ | ||
return method.GetParameters().Select( x => x.ParameterType ).ToArray(); | ||
} | ||
else | ||
{ | ||
return new Type[] { method.DeclaringType }.Concat( method.GetParameters().Select( x => x.ParameterType ) ).ToArray(); | ||
} | ||
} | ||
|
||
static Type GetGenericActionType( Type[] parameterTypes ) | ||
{ | ||
return GetActionType( parameterTypes.Length ).MakeGenericType( parameterTypes ); | ||
} | ||
|
||
static Type GetGenericFuncType( Type[] parameterTypes, Type returnType ) | ||
{ | ||
return GetFuncType( parameterTypes.Length ).MakeGenericType( parameterTypes.Concat( new[] { returnType } ).ToArray() ); | ||
} | ||
|
||
static Type GetActionType( int parameterCount ) | ||
{ | ||
switch( parameterCount ) | ||
{ | ||
case 0: | ||
return typeof( Action ); | ||
case 1: | ||
return typeof( Action<> ); | ||
case 2: | ||
return typeof( Action<,> ); | ||
case 3: | ||
return typeof( Action<,,> ); | ||
case 4: | ||
return typeof( Action<,,,> ); | ||
default: | ||
throw new ArgumentException( nameof( parameterCount ) ); | ||
} | ||
} | ||
|
||
static Type GetFuncType( int parameterCount ) | ||
{ | ||
switch( parameterCount ) | ||
{ | ||
case 0: | ||
return typeof( Func<> ); | ||
case 1: | ||
return typeof( Func<,> ); | ||
case 2: | ||
return typeof( Func<,,> ); | ||
case 3: | ||
return typeof( Func<,,,> ); | ||
case 4: | ||
return typeof( Func<,,,,> ); | ||
default: | ||
throw new ArgumentException( nameof( parameterCount ) ); | ||
} | ||
} | ||
|
||
//static string GetFuncTypeName( int parameterCount ) | ||
//{ | ||
// return "System.Func`" + ( parameterCount + 1 ); | ||
//} | ||
|
||
//static string GetActionTypeName( int parameterCount ) | ||
//{ | ||
// if( parameterCount == 0 ) | ||
// { | ||
// return "System.Action"; | ||
// } | ||
// else | ||
// { | ||
// return "System.Action`" + parameterCount; | ||
// } | ||
//} | ||
} | ||
} |