public static I GetImplementationTypeForMod <O, F, I>(GameObject objTarget, string dllName, string dllMinVersion) where O : MonoBehaviour, I where F : MonoBehaviour, I
        {
            if (Singleton <PluginManager> .instance.GetPluginsInfo().Where((PluginManager.PluginInfo pi) =>
                                                                           pi.assemblyCount > 0 &&
                                                                           pi.isEnabled &&
                                                                           (pi.GetAssemblies().Where(x => (x.GetName().Name == dllName) && x.GetName().Version.CompareTo(new Version(dllMinVersion)) >= 0).Count() > 0)

                                                                           ).Count() > 0)
            {
                try
                {
                    LogUtils.DoWarnLog($"Using {typeof(O).Name} as implementation of {typeof(I).Name}");
                    return(objTarget.AddComponent <O>());
                }
                catch (Exception e)
                {
                    LogUtils.DoWarnLog($"Failed loading integration with {dllName}. Fallback class was loaded. Check for a more recent version of that or this mod to fix this.\nDetails:\n{e}");
                    return(objTarget.AddComponent <F>());
                }
            }
            else
            {
                LogUtils.DoWarnLog($"Using {typeof(F).Name}  (fallback) as implementation of {typeof(I).Name}");
                return(objTarget.AddComponent <F>());
            }
        }
Beispiel #2
0
 public static UIButton AddHelpButton(UIComponent label, UIComponent field, Action onClicked, bool resizeToCreateSpace = true)
 {
     if (label.parent != field.parent)
     {
         LogUtils.DoWarnLog("Invalid request to add help button - different parents!");
         return(null);
     }
     if (resizeToCreateSpace)
     {
         label.width -= 15;
         field.width -= 15;
     }
     InitCircledButton(label.parent, out UIButton result, CommonsSpriteNames.K45_QuestionMark, (x, y) => onClicked(), "K45_CMNS_HELP", 30);
     result.scaleFactor = 1;
     return(result);
 }
        private void SetProperties(BindProperties propertiesToSet, Func <int, bool> callback)
        {
            m_mainPanel.autoLayout = true;
            if (propertiesToSet.help_isArticle)
            {
                if (!Directory.Exists(propertiesToSet.help_fullPathName))
                {
                    LogUtils.DoErrorLog($"Invalid tutorial path! {propertiesToSet.help_fullPathName}");
                    Close(-1);
                    return;
                }

                string fullText;
                if (File.Exists($"{propertiesToSet.help_fullPathName}{Path.DirectorySeparatorChar}texts_{KlyteLocaleManager.CurrentLanguageId}.txt"))
                {
                    fullText = File.ReadAllText($"{propertiesToSet.help_fullPathName}{Path.DirectorySeparatorChar}texts_{KlyteLocaleManager.CurrentLanguageId}.txt");
                }
                else if (File.Exists($"{propertiesToSet.help_fullPathName}{Path.DirectorySeparatorChar}texts.txt"))
                {
                    fullText = File.ReadAllText($"{propertiesToSet.help_fullPathName}{Path.DirectorySeparatorChar}texts.txt");
                }
                else
                {
                    LogUtils.DoErrorLog($"Corrupted tutorial path! File \"texts.txt\" not found at folder {propertiesToSet.help_fullPathName}.");
                    Close(-1);
                    return;
                }
                string[] tutorialEntries = Regex.Split(fullText, "<BR>", RegexOptions.Multiline | RegexOptions.ECMAScript);

                int      lastPage         = tutorialEntries.Length - 1;
                int      currentPage      = Math.Max(0, Math.Min(lastPage, propertiesToSet.help_currentPage));
                string   targetImg        = $"{propertiesToSet.help_fullPathName}{Path.DirectorySeparatorChar}{currentPage.ToString("D3")}.jpg";
                string   textureImagePath = File.Exists(targetImg) ? targetImg : null;
                string   path             = propertiesToSet.help_fullPathName;
                string   feature          = propertiesToSet.help_featureName;
                string[] formatEntries    = propertiesToSet.help_formatsEntries;
                LogUtils.DoLog($"IMG: {targetImg}");
                propertiesToSet = new BindProperties
                {
                    icon             = propertiesToSet.icon,
                    title            = string.Format(Locale.Get("K45_CMNS_HELP_FORMAT"), propertiesToSet.help_featureName, currentPage + 1, lastPage + 1),
                    message          = string.Format(tutorialEntries[currentPage], formatEntries),
                    imageTexturePath = textureImagePath,

                    showClose   = true,
                    showButton1 = currentPage != 0,
                    textButton1 = "<<<\n" + Locale.Get("K45_CMNS_PREV"),
                    showButton2 = true,
                    textButton2 = Locale.Get("EXCEPTION_OK"),
                    showButton3 = currentPage != lastPage,
                    textButton3 = ">>>\n" + Locale.Get("K45_CMNS_NEXT"),
                };
                callback = (x) =>
                {
                    if (x == 1)
                    {
                        ShowModalHelpAbsolutePath(path, feature, currentPage - 1, formatEntries);
                    }
                    if (x == 3)
                    {
                        ShowModalHelpAbsolutePath(path, feature, currentPage + 1, formatEntries);
                    }
                    return(true);
                };
            }

            m_currentCallback = callback;

            m_properties.FindBinding("title").property.value        = propertiesToSet.title;
            m_properties.FindBinding("icon").property.value         = propertiesToSet.icon ?? CommonProperties.ModIcon;
            m_properties.FindBinding("showClose").property.value    = propertiesToSet.showClose || !(propertiesToSet.showButton1 || propertiesToSet.showButton2 || propertiesToSet.showButton3 || propertiesToSet.showButton4);
            m_properties.FindBinding("message").property.value      = propertiesToSet.message;
            m_properties.FindBinding("messageAlign").property.value = propertiesToSet.messageAlign;
            m_properties.FindBinding("showButton1").property.value  = propertiesToSet.showButton1;
            m_properties.FindBinding("showButton2").property.value  = propertiesToSet.showButton2;
            m_properties.FindBinding("showButton3").property.value  = propertiesToSet.showButton3;
            m_properties.FindBinding("showButton4").property.value  = propertiesToSet.showButton4;
            m_properties.FindBinding("showButton5").property.value  = propertiesToSet.showButton5;
            m_properties.FindBinding("textButton1").property.value  = propertiesToSet.textButton1 ?? "";
            m_properties.FindBinding("textButton2").property.value  = propertiesToSet.textButton2 ?? "";
            m_properties.FindBinding("textButton3").property.value  = propertiesToSet.textButton3 ?? "";
            m_properties.FindBinding("textButton4").property.value  = propertiesToSet.textButton4 ?? "";
            m_properties.FindBinding("textButton5").property.value  = propertiesToSet.textButton5 ?? "";

            m_textField.isVisible = propertiesToSet.showTextField;
            m_textField.text      = propertiesToSet.defaultTextFieldContent ?? "";

            if (m_dropDown == null)
            {
                KlyteMonoUtils.CreateUIElement(out UIPanel DDpanel, m_mainPanel.transform);
                DDpanel.maximumSize            = new Vector2(m_boxText.minimumSize.x - 10, 40);
                DDpanel.anchor                 = UIAnchorStyle.CenterHorizontal;
                DDpanel.zOrder                 = m_textField.zOrder + 1;
                DDpanel.autoLayout             = true;
                m_dropDown                     = UIHelperExtension.CloneBasicDropDownNoLabel(new string[0], (x) => { }, DDpanel);
                m_dropDown.name                = DD_INPUT_ID;
                m_dropDown.minimumSize         = new Vector2(m_boxText.minimumSize.x - 10, 25);
                m_dropDown.size                = new Vector2(m_boxText.minimumSize.x - 10, 40);
                m_dropDown.autoSize            = false;
                m_dropDown.processMarkup       = true;
                m_dropDown.verticalAlignment   = UIVerticalAlignment.Middle;
                m_dropDown.horizontalAlignment = UIHorizontalAlignment.Left;
            }
            m_dropDown.parent.isVisible = propertiesToSet.showDropDown;
            m_dropDown.items            = propertiesToSet.dropDownOptions?.Split(BindProperties.DD_OPTIONS_SEPARATOR.ToCharArray()) ?? new string[0];
            m_dropDown.selectedIndex    = propertiesToSet.dropDownCurrentSelection;


            m_textureSprite.size = default;
            if (!propertiesToSet.imageTexturePath.IsNullOrWhiteSpace())
            {
                if (File.Exists(propertiesToSet.imageTexturePath))
                {
                    byte[] fileData = File.ReadAllBytes(propertiesToSet.imageTexturePath);
                    var    tex      = new Texture2D(2, 2);
                    if (tex.LoadImage(fileData))
                    {
                        m_textureSupContainer.isVisible = true;
                        m_textureSprite.texture         = tex;
                        m_textureSprite.size            = new Vector2(tex.width, tex.height);
                        if (m_textureSprite.height > 400)
                        {
                            float proportion = m_textureSprite.width / m_textureSprite.height;
                            m_textureSprite.height = 400;
                            m_textureSprite.width  = proportion * 400;
                        }
                        m_textureSupContainer.height = m_textureSprite.size.y;
                    }
                    else
                    {
                        LogUtils.DoWarnLog($"Failed loading image: {propertiesToSet.imageTexturePath}");
                        m_textureSupContainer.isVisible = false;
                    }
                }
            }
            else
            {
                m_textureSprite.texture         = null;
                m_textureSupContainer.isVisible = false;
            }

            float width;

            if (propertiesToSet.useFullWindowWidth || m_textureSprite.width > 800)
            {
                width = UIView.GetAView().fixedWidth - 100;
                if (width < m_textureSprite.width)
                {
                    float proportion = m_textureSprite.width / m_textureSprite.height;
                    m_textureSprite.width  = width;
                    m_textureSprite.height = width / proportion;
                }
            }
            else
            {
                width = 800;
            }
            m_mainPanel.width  = width;
            m_closeButton.area = new Vector4(width - 37, 3, 32, 32);
            width -= m_mainPanel.padding.horizontal;
            m_titleContainer.width      = width;
            m_boxText.width             = width;
            m_buttonSupContainer.width  = width;
            m_textureSupContainer.width = width;
            m_mainPanel.autoLayout      = !propertiesToSet.showDropDown;
        }