/// <summary>
        /// Creates the IPC request.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="args">The arguments to the method.</param>
        /// <returns>IpcRequest object</returns>
        protected static IpcRequest CreateIpcRequest(string methodName, params object[] args)
        {
            MethodBase method = null;

            // Try to find the matching method based on name and args
            try
            {
                if (args.All(x => x != null))
                {
                    method = typeof(FactoryOrchestratorClient).GetMethod(methodName, args.Select(x => x.GetType()).ToArray());
                }

                if (method == null)
                {
                    method = typeof(FactoryOrchestratorClient).GetMethod(methodName);
                }
            }
            catch (Exception)
            {
                method = null;
            }

            if (method == null)
            {
                // Multiple methods with the same name were found or no method was found, try to find the unique method via stack trace
                var frame = new StackTrace().GetFrames().Where(x => x.GetMethod()?.Name == methodName);

                if (!frame.Any())
                {
                    throw new Exception(string.Format(CultureInfo.CurrentCulture, Resources.NoMethodFound, methodName));
                }
                if (frame.Count() > 1)
                {
                    throw new Exception(string.Format(CultureInfo.CurrentCulture, Resources.TooManyMethodsFound, methodName));
                }

                method = frame.First().GetMethod();
            }

            var methodParams   = method.GetParameters();
            var parameterTypes = new IpcRequestParameterType[methodParams.Length];

            for (int i = 0; i < methodParams.Length; i++)
            {
                parameterTypes[i] = new IpcRequestParameterType(methodParams[i].ParameterType);
            }

            var request = new IpcRequest()
            {
                MethodName           = methodName,
                Parameters           = args,
                ParameterTypesByName = parameterTypes
            };

            return(request);
        }
Ejemplo n.º 2
0
    public Guard([CallerMemberName] string callerName = null)
    {
        var constructorExpression = (Expression <Func <Prohibited> >)(() => new Prohibited());
        var constructorMethod     = (NewExpression)(constructorExpression.Body);
        var stackFrames           = new StackTrace().GetFrames();

        if (stackFrames.Any(f => f.GetMethod() == constructorMethod.Constructor))
        {
            throw new InvalidOperationException("Aha! you are still trying.");
        }
    }
Ejemplo n.º 3
0
        public override void Write(string message)
        {
            try
            {
                var frames = new StackTrace().GetFrames();

                // 同じAssemblyのメソッドから呼ばれているか?
                var any = frames.Any(x =>
                                     x.GetMethod().DeclaringType != MethodBase.GetCurrentMethod().DeclaringType&&
                                     x.GetMethod().ReflectedType.FullName.Contains(this.Name));

                if (!any)
                {
                    return;
                }

                var log =
                    DateTime.Now.ToString("[yyyy/MM/dd HH:mm:ss.fff]") + " " +
                    message;

                this.defaultListener.Write(log);

                if (this.LogTextBox != null)
                {
                    this.LogTextBox.AppendText(log);
                }

                // ログファイルに出力する
                lock (this.logBuffer)
                {
                    this.logBuffer.Add(log);

                    if (this.logBuffer.Count >= 64)
                    {
                        var toWrite = string.Join(string.Empty, this.logBuffer.ToArray());
                        File.AppendAllText(this.logFile, toWrite);
                        this.logBuffer.Clear();
                    }
                }
            }
            catch
            {
            }
        }
        public void SetText(object ui, string text)
        {
            var type = _type;

#if MANAGED
            if (UnityTypes.TextWindow != null && UnityTypes.TextMeshPro?.ClrType.IsAssignableFrom(type) == true)
            {
                var textWindow = GameObject.FindObjectOfType(UnityTypes.TextWindow.ClrType);
                if (textWindow != null)
                {
                    var flags    = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
                    var textMesh = textWindow.GetType().GetField("TextMesh", flags)?.GetValue(textWindow);
                    if (textMesh != null)
                    {
                        if (Equals(textMesh, ui))
                        {
                            var frames = new StackTrace().GetFrames();
                            if (frames.Any(x => x.GetMethod().DeclaringType == UnityTypes.TextWindow.ClrType))
                            {
                                // If inline (sync)

                                var previousCurText = textWindow.GetType().GetField("curText", flags).GetValue(textWindow);
                                textWindow.GetType().GetField("curText", flags).SetValue(textWindow, text);

                                Settings.SetCurText = textWindowInner =>
                                {
                                    var translatedText = textWindow.GetType().GetField("curText", flags).GetValue(textWindow);
                                    if (Equals(text, translatedText))
                                    {
                                        textWindowInner.GetType().GetMethod("FinishTyping", flags).Invoke(textWindowInner, null);

                                        textWindowInner.GetType().GetField("curText", flags).SetValue(textWindowInner, previousCurText);

                                        var textMeshInner = textWindowInner.GetType().GetField("TextMesh", flags).GetValue(textWindowInner);
                                        var keyword       = textWindowInner.GetType().GetField("Keyword", flags).GetValue(textWindowInner);
                                        keyword.GetType().GetMethod("UpdateTextMesh", flags).Invoke(keyword, new object[] { textMeshInner, true });
                                    }

                                    Settings.SetCurText = null;
                                };
                            }
                            else
                            {
                                // If delayed by plugin (async)

                                // update the actual text in the text box
                                type.CachedProperty(TextPropertyName)?.Set(ui, text);

                                // obtain the curText in the text window
                                var previousCurText = textWindow.GetType().GetField("curText", flags).GetValue(textWindow);

                                // set curText to OUR text
                                textWindow.GetType().GetField("curText", flags).SetValue(textWindow, text);

                                // call FinishTyping
                                textWindow.GetType().GetMethod("FinishTyping", flags).Invoke(textWindow, null);

                                // reset curText
                                textWindow.GetType().GetField("curText", flags).SetValue(textWindow, previousCurText);

                                var keyword = textWindow.GetType().GetField("Keyword", flags).GetValue(textWindow);
                                keyword.GetType().GetMethod("UpdateTextMesh", flags).Invoke(keyword, new object[] { textMesh, true });
                            }

                            return;
                        }
                    }
                }
            }
#endif

            // fallback to reflective approach
            var property = _property;
            if (property != null)
            {
                property.Set(ui, text);

#if MANAGED
                if (Settings.IgnoreVirtualTextSetterCallingRules)
                {
                    var  newText     = (string)property.Get(ui);
                    Type currentType = type;
                    while (text != newText && currentType != null)
                    {
                        var typeAndMethod = GetTextPropertySetterInParent(currentType);
                        if (typeAndMethod != null)
                        {
                            currentType = typeAndMethod.Type;

                            typeAndMethod.SetterInvoker(ui, text);
                            newText = (string)property.Get(ui);
                        }
                        else
                        {
                            currentType = null;
                        }
                    }
                }
#endif
            }

            // TMPro
            var maxVisibleCharactersProperty = type.CachedProperty("maxVisibleCharacters");
            if (maxVisibleCharactersProperty != null && maxVisibleCharactersProperty.PropertyType == typeof(int))
            {
                var value = (int)maxVisibleCharactersProperty.Get(ui);
                if (0 < value && value < 99999)
                {
                    maxVisibleCharactersProperty.Set(ui, 99999);
                }
            }

#if MANAGED
            if (UnityTypes.TextExpansion_Methods.SetMessageType != null && UnityTypes.TextExpansion_Methods.SkipTypeWriter != null)
            {
                if (UnityTypes.TextExpansion.ClrType.IsAssignableFrom(type))
                {
                    UnityTypes.TextExpansion_Methods.SetMessageType.Invoke(ui, 1);
                    UnityTypes.TextExpansion_Methods.SkipTypeWriter.Invoke(ui);
                }
            }
#endif
        }
        public static void SetText(this object ui, string text)
        {
            if (ui == null)
            {
                return;
            }

            var type = ui.GetType();

            if (ClrTypes.AdvUguiMessageWindow != null && ClrTypes.UguiNovelText?.IsAssignableFrom(type) == true)
            {
                var uguiMessageWindow = GameObject.FindObjectOfType(ClrTypes.AdvUguiMessageWindow);
                if (uguiMessageWindow != null)
                {
                    var uguiNovelText = ClrTypes.AdvUguiMessageWindow_Properties.Text?.Get(uguiMessageWindow)
                                        ?? ClrTypes.AdvUguiMessageWindow_Fields.text?.GetValue(uguiMessageWindow);


                    if (Equals(uguiNovelText, ui))
                    {
                        string previousNameText = null;
                        var    nameText         = ClrTypes.AdvUguiMessageWindow_Fields.nameText.GetValue(uguiMessageWindow) as UnityEngine.Object;
                        if (nameText)
                        {
                            previousNameText = (string)ClrTypes.Text.CachedProperty(TextPropertyName).Get(nameText);
                        }

                        var engine = ClrTypes.AdvUguiMessageWindow_Properties.Engine?.Get(uguiMessageWindow)
                                     ?? ClrTypes.AdvUguiMessageWindow_Fields.engine.GetValue(uguiMessageWindow);
                        var page = ClrTypes.AdvEngine_Properties.Page.Get(engine);

                        var remakeTextData          = ClrTypes.AdvPage_Methods.RemakeTextData;
                        var remakeText              = ClrTypes.AdvPage_Methods.RemakeText;
                        var changeMessageWindowText = ClrTypes.AdvPage_Methods.ChangeMessageWindowText;

                        if (changeMessageWindowText != null)
                        {
                            var nameText0       = (string)page.GetType().GetProperty("NameText")?.GetValue(page, null);
                            var characterLabel0 = (string)page.GetType().GetProperty("CharacterLabel")?.GetValue(page, null);
                            var windowType0     = (string)page.GetType().GetProperty("WindowType")?.GetValue(page, null);

                            changeMessageWindowText.Invoke(page, new object[] { nameText0, characterLabel0, text, windowType0 });
                        }
                        else
                        {
                            var flags    = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
                            var textData = ClrTypes.TextData.GetConstructor(new[] { typeof(string) }).Invoke(new[] { text });
                            var length   = (int)textData.GetType().GetProperty("Length", flags).GetValue(textData, null);

                            if (remakeTextData == null)
                            {
                                try
                                {
                                    Settings.InvokeEvents = false;

                                    page.GetType().GetProperty("TextData", flags).SetValue(page, textData, null);
                                    page.GetType().GetProperty("CurrentTextLengthMax", flags).GetSetMethod(true).Invoke(page, new object[] { length });
                                    page.GetType().GetProperty("Status", flags).GetSetMethod(true).Invoke(page, new object[] { 1 /*SendChar*/ });

                                    var messageWindowManager = engine.GetType().GetProperty("MessageWindowManager", flags).GetValue(engine, null);
                                    messageWindowManager.GetType().GetMethod("OnPageTextChange", flags).Invoke(messageWindowManager, new object[] { page });
                                }
                                finally
                                {
                                    Settings.InvokeEvents = true;
                                }
                            }
                            else
                            {
                                try
                                {
                                    Settings.InvokeEvents   = false;
                                    Settings.RemakeTextData = advPage =>
                                    {
                                        advPage.GetType().GetProperty("TextData", flags).SetValue(page, textData, null);
                                        advPage.GetType().GetProperty("CurrentTextLengthMax", flags).GetSetMethod(true).Invoke(page, new object[] { length });
                                    };

                                    remakeText.Invoke(page);
                                }
                                finally
                                {
                                    Settings.InvokeEvents   = true;
                                    Settings.RemakeTextData = null;
                                }
                            }
                        }

                        if (nameText)
                        {
                            ClrTypes.Text.CachedProperty(TextPropertyName).Set(nameText, previousNameText);
                        }

                        return;
                    }
                }
            }

            if (ClrTypes.TextWindow != null && ClrTypes.TextMeshPro?.IsAssignableFrom(type) == true)
            {
                var textWindow = GameObject.FindObjectOfType(ClrTypes.TextWindow);
                if (textWindow != null)
                {
                    var flags    = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
                    var textMesh = textWindow.GetType().GetField("TextMesh", flags)?.GetValue(textWindow);
                    if (textMesh != null)
                    {
                        if (Equals(textMesh, ui))
                        {
                            var frames = new StackTrace().GetFrames();
                            if (frames.Any(x => x.GetMethod().DeclaringType == ClrTypes.TextWindow))
                            {
                                // If inline (sync)

                                var previousCurText = textWindow.GetType().GetField("curText", flags).GetValue(textWindow);
                                textWindow.GetType().GetField("curText", flags).SetValue(textWindow, text);

                                Settings.SetCurText = textWindowInner =>
                                {
                                    var translatedText = textWindow.GetType().GetField("curText", flags).GetValue(textWindow);
                                    if (Equals(text, translatedText))
                                    {
                                        textWindowInner.GetType().GetMethod("FinishTyping", flags).Invoke(textWindowInner, null);

                                        textWindowInner.GetType().GetField("curText", flags).SetValue(textWindowInner, previousCurText);

                                        var textMeshInner = textWindowInner.GetType().GetField("TextMesh", flags).GetValue(textWindowInner);
                                        var keyword       = textWindowInner.GetType().GetField("Keyword", flags).GetValue(textWindowInner);
                                        keyword.GetType().GetMethod("UpdateTextMesh", flags).Invoke(keyword, new object[] { textMeshInner, true });
                                    }

                                    Settings.SetCurText = null;
                                };
                            }
                            else
                            {
                                // If delayed by plugin (async)

                                // update the actual text in the text box
                                type.CachedProperty(TextPropertyName)?.Set(ui, text);

                                // obtain the curText in the text window
                                var previousCurText = textWindow.GetType().GetField("curText", flags).GetValue(textWindow);

                                // set curText to OUR text
                                textWindow.GetType().GetField("curText", flags).SetValue(textWindow, text);

                                // call FinishTyping
                                textWindow.GetType().GetMethod("FinishTyping", flags).Invoke(textWindow, null);

                                // reset curText
                                textWindow.GetType().GetField("curText", flags).SetValue(textWindow, previousCurText);

                                var keyword = textWindow.GetType().GetField("Keyword", flags).GetValue(textWindow);
                                keyword.GetType().GetMethod("UpdateTextMesh", flags).Invoke(keyword, new object[] { textMesh, true });
                            }

                            return;
                        }
                    }
                }
            }


            if (ui is GUIContent)
            {
                ((GUIContent)ui).text = text;
            }
            else
            {
                // fallback to reflective approach
                type.CachedProperty(TextPropertyName)?.Set(ui, text);

                // TMPro
                var maxVisibleCharactersProperty = type.CachedProperty("maxVisibleCharacters");
                if (maxVisibleCharactersProperty != null && maxVisibleCharactersProperty.PropertyType == typeof(int))
                {
                    var value = (int)maxVisibleCharactersProperty.Get(ui);
                    if (0 < value && value < 99999)
                    {
                        maxVisibleCharactersProperty.Set(ui, 99999);
                    }
                }

                if (ClrTypes.TextExpansion_Methods.SetMessageType != null && ClrTypes.TextExpansion_Methods.SkipTypeWriter != null)
                {
                    if (ClrTypes.TextExpansion.IsAssignableFrom(type))
                    {
                        ClrTypes.TextExpansion_Methods.SetMessageType.Invoke(ui, 1);
                        ClrTypes.TextExpansion_Methods.SkipTypeWriter.Invoke(ui);
                    }
                }
            }
        }
Ejemplo n.º 6
0
 public static bool ContainsUnitTest()
 {
     StackFrame[] frames = new StackTrace().GetFrames();
     return(frames != null && frames.Any(IsTest));
 }
Ejemplo n.º 7
0
 public static bool IsUnitTest()
 {
     StackFrame[] frames = new StackTrace().GetFrames();
     return(frames.Any(frame => IsTestAttribute(frame) || IsInTestSetUp(frame)));
 }