コード例 #1
0
        void OnLevelWasLoaded(int level)
        {
            VRCModLogger.Log("[AvatarFav] OnLevelWasLoaded (" + level + ")");
            if (level == (Application.platform == RuntimePlatform.WindowsPlayer ? 1 : 2) && !alreadyLoaded)
            {
                alreadyLoaded = true;

                if (instance != null)
                {
                    Debug.LogWarning("[AvatarFav] Trying to load the same plugin two time !");
                    return;
                }
                instance = this;
                VRCModLogger.Log("[AvatarFav] Adding button to UI - Looking up for Change Button");
                // Add a "Favorite" / "Unfavorite" button over the "Choose" button of the AvatarPage
                Transform changeButton = null;
                pageAvatar = Resources.FindObjectsOfTypeAll <PageAvatar>().First(p => (changeButton = p.transform.Find("Change Button")) != null);

                VRCModLogger.Log("[AvatarFav] Adding avatar check on Change button");

                baseChooseEvent = changeButton.GetComponent <Button>().onClick;

                changeButton.GetComponent <Button>().onClick = new Button.ButtonClickedEvent();
                changeButton.GetComponent <Button>().onClick.AddListener(() =>
                {
                    VRCModLogger.Log("[AvatarFav] Fetching avatar releaseStatus for " + pageAvatar.avatar.apiAvatar.name + " (" + pageAvatar.avatar.apiAvatar.id + ")");
                    ModManager.StartCoroutine(CheckAndWearAvatar());
                });



                VRCModLogger.Log("[AvatarFav] Adding favorite button to UI - Duplicating Button");
                favButton      = UnityUiUtils.DuplicateButton(changeButton, "Favorite", new Vector2(0, 80));
                favButton.name = "ToggleFavorite";
                favButton.gameObject.SetActive(false);
                favButtonText = favButton.Find("Text").GetComponent <Text>();
                favButton.GetComponent <Button>().interactable = false;

                favButton.GetComponent <Button>().onClick.AddListener(ToggleAvatarFavorite);

                VRCModLogger.Log("[AvatarFav] Storing default AvatarModel position");
                avatarModel             = pageAvatar.transform.Find("AvatarModel");
                baseAvatarModelPosition = avatarModel.localPosition;


                FileInfo[] files = new DirectoryInfo(Environment.CurrentDirectory).GetFiles("Avatars.txt", SearchOption.AllDirectories);
                VRCModLogger.Log("[AvatarFavMod] Found " + files.Length + " Avatars.txt");
                if (files.Length > 0)
                {
                    VRCModLogger.Log("[AvatarFav] Adding import button to UI - Duplicating Button");
                    Transform importButton = UnityUiUtils.DuplicateButton(changeButton, "Import Avatars", new Vector2(0, 0));
                    importButton.name = "ImportAvatars";

                    importButton.GetComponent <RectTransform>().anchoredPosition = new Vector2(560, 371);

                    importButton.GetComponent <Button>().onClick.AddListener(() =>
                    {
                        VRCUiPopupManagerUtils.ShowPopup("AvatarFav", "Do you want to import the public avatars from your VRCheat avatar list ?",
                                                         "Yes", () =>
                        {
                            ModManager.StartCoroutine(VRCheatAvatarfileImporter.ImportAvatarfile());
                        },
                                                         "No", () =>
                        {
                            VRCUiPopupManagerUtils.GetVRCUiPopupManager().HideCurrentPopup();
                        });
                        VRCheatAvatarfileImporter.ImportAvatarfile();
                    });
                }


                favList = AvatarPageHelper.AddNewList("Favorite Avatar List (Unofficial)", 1);



                // Get Getter of VRCUiContentButton.PressAction
                applyAvatarField = typeof(VRCUiContentButton).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).First((field) => field.FieldType == typeof(Action));

                VRCModLogger.Log("[AvatarFav] Registering VRCModNetwork events");
                VRCModNetworkManager.OnAuthenticated += () =>
                {
                    RequestAvatars();
                };

                VRCModNetworkManager.SetRPCListener("slaynash.avatarfav.serverconnected", (senderId, data) => { if (waitingForServer)
                                                                                                                {
                                                                                                                    RequestAvatars();
                                                                                                                }
                                                    });
                VRCModNetworkManager.SetRPCListener("slaynash.avatarfav.error", (senderId, data) => addError = data);
                VRCModNetworkManager.SetRPCListener("slaynash.avatarfav.avatarlistupdated", (senderId, data) =>
                {
                    lock (favoriteAvatarList)
                    {
                        // Update Ui
                        favButton.GetComponent <Button>().interactable = true;
                        SerializableApiAvatar[] serializedAvatars      = SerializableApiAvatar.ParseJson(data);
                        favoriteAvatarList.Clear();
                        foreach (SerializableApiAvatar serializedAvatar in serializedAvatars)
                        {
                            favoriteAvatarList.Add(serializedAvatar.id);
                        }

                        avatarAvailables = true;
                    }
                });



                VRCModLogger.Log("[AvatarFav] Adding avatar search list");

                if (pageAvatar != null)
                {
                    VRCUiPageHeader pageheader = VRCUiManagerUtils.GetVRCUiManager().GetComponentInChildren <VRCUiPageHeader>(true);
                    if (pageheader != null)
                    {
                        searchbar = pageheader.searchBar;
                        if (searchbar != null)
                        {
                            VRCModLogger.Log("[AvatarFav] creating avatar search list");
                            avatarSearchList = AvatarPageHelper.AddNewList("Search Results", 0);
                            avatarSearchList.ClearAll();
                            avatarSearchList.gameObject.SetActive(false);
                            avatarSearchList.collapsedCount   = 50;
                            avatarSearchList.expandedCount    = 50;
                            avatarSearchList.collapseRows     = 5;
                            avatarSearchList.extendRows       = 5;
                            avatarSearchList.contractedHeight = 850f;
                            avatarSearchList.expandedHeight   = 850f;
                            avatarSearchList.GetComponent <LayoutElement>().minWidth = 1600f;
                            avatarSearchList.GetComponentInChildren <GridLayoutGroup>(true).constraintCount = 5;
                            avatarSearchList.expandButton.image.enabled = false;

                            VRCModLogger.Log("[AvatarFav] Overwriting search button");
                            VRCUiManagerUtils.OnPageShown += (page) =>
                            {
                                if (page.GetType() == typeof(PageAvatar))
                                {
                                    UiVRCList[] lists = page.GetComponentsInChildren <UiVRCList>(true);
                                    foreach (UiVRCList list in lists)
                                    {
                                        if (list != avatarSearchList && (list.GetType() != typeof(UiAvatarList) || ((int)categoryField.GetValue(list)) != 0))
                                        {
                                            list.gameObject.SetActive(true);
                                        }
                                        else
                                        {
                                            list.gameObject.SetActive(false);
                                        }
                                    }
                                    VRCModLogger.Log("[AvatarFav] PageAvatar shown. Enabling searchbar next frame");
                                    ModManager.StartCoroutine(EnableSearchbarNextFrame());
                                }
                            };

                            VRCModNetworkManager.SetRPCListener("slaynash.avatarfav.searchresults", (senderid, data) =>
                            {
                                AddMainAction(() =>
                                {
                                    if (data.StartsWith("ERROR"))
                                    {
                                        VRCUiPopupManagerUtils.ShowPopup("AvatarFav", "Unable to fetch avatars: Server returned error: " + data.Substring("ERROR ".Length), "Close", () => VRCUiPopupManagerUtils.GetVRCUiPopupManager().HideCurrentPopup());
                                    }
                                    else
                                    {
                                        avatarSearchList.ClearSpecificList();
                                        if (!avatarSearchList.gameObject.activeSelf)
                                        {
                                            UiVRCList[] lists = pageAvatar.GetComponentsInChildren <UiVRCList>(true);
                                            foreach (UiVRCList list in lists)
                                            {
                                                if (list != avatarSearchList)
                                                {
                                                    list.gameObject.SetActive(false);
                                                }
                                            }
                                        }

                                        SerializableApiAvatar[] serializedAvatars = SerializableApiAvatar.ParseJson(data);

                                        string[] avatarsIds = new string[serializedAvatars.Length];

                                        for (int i = 0; i < serializedAvatars.Length; i++)
                                        {
                                            avatarsIds[i] = serializedAvatars[i].id;
                                        }

                                        avatarSearchList.specificListIds = avatarsIds;
                                        if (avatarSearchList.gameObject.activeSelf)
                                        {
                                            avatarSearchList.Refresh();
                                        }
                                        else
                                        {
                                            avatarSearchList.gameObject.SetActive(true);
                                        }
                                    }
                                });
                            });
                        }
                        else
                        {
                            VRCModLogger.LogError("[AvatarFav] Unable to find search bar");
                        }
                    }
                    else
                    {
                        VRCModLogger.LogError("[AvatarFav] Unable to find page header");
                    }
                }
                else
                {
                    VRCModLogger.LogError("[AvatarFav] Unable to find avatar page");
                }



                VRCModLogger.Log("[AvatarFav] AvatarFav Initialised !");
                initialised = true;
            }
        }
コード例 #2
0
        public void OnUpdate()
        {
            if (!initialised)
            {
                return;
            }

            lock (actions)
            {
                foreach (Action a in actions)
                {
                    try
                    {
                        a();
                    }
                    catch (Exception e)
                    {
                        VRCModLogger.Log("[AvatarFav] Error while calling action from main thread: " + e);
                    }
                }
                actions.Clear();
            }

            try
            {
                //Update list if element is active
                if (favList.gameObject.activeInHierarchy)
                {
                    lock (favoriteAvatarList)
                    {
                        if (avatarAvailables)
                        {
                            avatarAvailables = false;
                            favList.ClearSpecificList();

                            if (newAvatarsFirst)
                            {
                                List <string> favReversed = favoriteAvatarList.ToList();
                                favReversed.Reverse();
                                favList.specificListIds = favReversed.ToArray();
                            }
                            else
                            {
                                favList.specificListIds = favoriteAvatarList.ToArray();
                            }


                            favList.Refresh();

                            freshUpdate = true;
                        }
                    }
                }

                if (pageAvatar.avatar != null && pageAvatar.avatar.apiAvatar != null && CurrentUserUtils.GetGetCurrentUser().GetValue(null) != null && !currentUiAvatarId.Equals(pageAvatar.avatar.apiAvatar.id) || freshUpdate)
                {
                    currentUiAvatarId = pageAvatar.avatar.apiAvatar.id;

                    bool favorited = favoriteAvatarList.Contains(currentUiAvatarId);

                    if (favorited)
                    {
                        favButtonText.text = "Unfavorite";
                    }
                    else
                    {
                        favButtonText.text = "Favorite";
                    }

                    if ((!pageAvatar.avatar.apiAvatar.releaseStatus.Equals("public") && !favorited) || pageAvatar.avatar.apiAvatar.authorId == APIUser.CurrentUser.id)
                    {
                        favButton.gameObject.SetActive(false);
                        avatarModel.localPosition = baseAvatarModelPosition;
                    }
                    else
                    {
                        favButton.gameObject.SetActive(true);
                        avatarModel.localPosition = baseAvatarModelPosition + new Vector3(0, 60, 0);
                    }
                }

                //Show returned error if exists
                if (addError != null)
                {
                    VRCUiPopupManagerUtils.ShowPopup("Error", addError, "Close", () => VRCUiPopupManagerUtils.GetVRCUiPopupManager().HideCurrentPopup());
                    addError = null;
                }


                if (RoomManager.currentRoom != null && RoomManager.currentRoom.id != null && RoomManager.currentRoom.currentInstanceIdOnly != null)
                {
                    if (currentRoom == null)
                    {
                        currentRoom = RoomManager.currentRoom;

                        if (currentRoom.releaseStatus != "public")
                        {
                            VRCModLogger.Log("[AvatarFav] Current world release status isn't public. Pedestal scan disabled.");
                        }
                        else
                        {
                            VRC_AvatarPedestal[] pedestalsInWorld = GameObject.FindObjectsOfType <VRC_AvatarPedestal>();
                            VRCModLogger.Log("[AvatarFav] Found " + pedestalsInWorld.Length + " VRC_AvatarPedestal in current world");
                            string dataToSend = currentRoom.id;
                            foreach (VRC_AvatarPedestal p in pedestalsInWorld)
                            {
                                if (p.blueprintId == null || p.blueprintId == "")
                                {
                                    continue;
                                }

                                dataToSend += ";" + p.blueprintId;
                            }

                            VRCModNetworkManager.SendRPC("slaynash.avatarfav.avatarsinworld", dataToSend);
                        }
                    }
                }
                else
                {
                    currentRoom = null;
                }
            }
            catch (Exception e)
            {
                VRCModLogger.Log("[AvatarFav] [ERROR] " + e.ToString());
            }
            freshUpdate = false;
        }