Exemplo n.º 1
0
        public override object ReadJson(JsonReader reader, Type type, object existing, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            JObject jObject    = JObject.Load(reader);
            int     instanceId = (int)jObject["InstanceId"];
            var     result     = BlueprintUtil.FindObjectByInstanceId(instanceId, type);

            if (result == null)
            {
                throw new System.Exception($"Couldn't find object with InstanceId {instanceId}");
            }
            return(result);
        }
Exemplo n.º 2
0
        static void OnGUI(UnityModManager.ModEntry modEntry)
        {
            try
            {
                if (!enabled)
                {
                    return;
                }
#if (DEBUG)
                if (GUILayout.Button("DumpClassRaceBlueprints"))
                {
                    AssetsDump.DumpQuick();
                }
                if (GUILayout.Button("DumpSampleOfBlueprints"))
                {
                    AssetsDump.DumpBlueprints();
                }
                if (GUILayout.Button("DumpAllBlueprints"))
                {
                    AssetsDump.DumpAllBlueprints();
                }
                if (GUILayout.Button("DumpEquipmentEntities"))
                {
                    AssetsDump.DumpEquipmentEntities();
                }
                if (GUILayout.Button("DumpUnitViews"))
                {
                    AssetsDump.DumpUnitViews();
                }
                if (GUILayout.Button("DumpList"))
                {
                    AssetsDump.DumpList();
                }
                if (GUILayout.Button("TestLoad"))
                {
                    //var bp = JsonBlueprints.Load<BlueprintCharacterClass>("mods/customraces/data/slayerclass.json");
                    //DebugLog("Loaded " + (bp?.name ?? "NULL"));
                    //var info = BlueprintInfo.Load();
                    //DebugLog("Loaded " + info.Classes[0].name);
                    var vp = JsonBlueprints.Load <BlueprintRaceVisualPreset>("mods/customraces/data/TestPreset.json");
                    DebugLog("Loaded " + vp.name);
                }

                /*
                 * UnityEngine.Networking.NetworkTransport.GetAssetId(go) //returns ""
                 * internal static extern bool Object.DoesObjectWithInstanceIDExist(int instanceID); //returns true
                 * internal static extern Object Object.FindObjectFromInstanceID(int instanceID); // returns CR_Hair_VioletDark_U_HM
                 * Resources.FindObjectsOfTypeAll<Texture2D>() // returns CR_Hair_VioletDark_U_HM after it has been loaded with Resource.Load
                 */
                if (GUILayout.Button("FindObject"))
                {
                    var go = BlueprintUtil.FindObjectByInstanceId <GameObject>(270194);
                    DebugLog("FindByID " + go == null ? "NULL" : go.name); //OH_LongswordThieves

                    var sprite = BlueprintUtil.FindObjectByInstanceId <Sprite>(45820);
                    DebugLog(sprite == null ? "NULL" : sprite.name); //OH_LongswordThieves

                    var texture1 = BlueprintUtil.FindObjectByInstanceId <Texture2D>(552466);
                    DebugLog(texture1 == null ? "NULL" : texture1.name);                                                   //CR_Hair_VioletDark_U_HM

                    var humanHair = ResourcesLibrary.TryGetResource <EquipmentEntity>("a9558cfc0705d4e48af7ecd2ebd75411"); //EE_Hair_HairLongWavy_M_HM

                    var texture2 = BlueprintUtil.FindObjectByInstanceId <Texture2D>(552466);
                    DebugLog(texture2 == null ? "NULL" : texture2.name); //CR_Hair_VioletDark_U_HM
                }
                if (GUILayout.Button("FindObject2"))
                {
                    var doesExist = Traverse.Create <UnityEngine.Object>().Method("DoesObjectWithInstanceIDExist", new object[] { 552466 }).GetValue <bool>();
                    DebugLog($"Does resource exist first {doesExist}");
                    var tex1 = Traverse.Create <UnityEngine.Object>().Method("FindObjectFromInstanceID", new object[] { 552466 }).GetValue <UnityEngine.Object>();
                    DebugLog(tex1 == null ? "NULL" : tex1.name);                                                           //CR_Hair_VioletDark_U_HM

                    var humanHair = ResourcesLibrary.TryGetResource <EquipmentEntity>("a9558cfc0705d4e48af7ecd2ebd75411"); //EE_Hair_HairLongWavy_M_HM

                    doesExist = Traverse.Create <UnityEngine.Object>().Method("DoesObjectWithInstanceIDExist", new object[] { 552466 }).GetValue <bool>();
                    DebugLog($"Does resource exist second {doesExist}");
                    var tex2 = Traverse.Create <UnityEngine.Object>().Method("FindObjectFromInstanceID", new object[] { 552466 }).GetValue <UnityEngine.Object>();
                    DebugLog(tex2 == null ? "NULL" : tex2.name); //CR_Hair_VioletDark_U_HM


                    var go = (GameObject)BlueprintUtil.FindObjectByInstanceId <GameObject>(270194);
                    DebugLog("FindByID " + go == null ? "NULL" : go.name); //OH_LongswordThieves

                    var assetId = UnityEngine.Networking.NetworkTransport.GetAssetId(go);
                    if (assetId == null)
                    {
                        assetId = "NULL";
                    }
                    if (assetId == "")
                    {
                        assetId = "Empty";
                    }
                    DebugLog($"AssetId: {assetId}");
                }

                if (GUILayout.Button("Reload"))
                {
                    BlueprintManager.Reload();
                }
                int newTorso = (int)GUILayout.HorizontalSlider(torso, -1, MeshTestRace.testAssets.Length - 1, GUILayout.Width(300));
                GUILayout.Label("Torso: " + newTorso);
                if (torso != newTorso)
                {
                    torso = newTorso;
                    MeshTestRace.ChooseTorso(torso);
                }
#endif
            } catch (Exception e)
            {
                DebugLog(e.ToString() + " " + e.StackTrace);
            }
        }
        public override object ReadJson(JsonReader reader, Type type, object existing, JsonSerializer serializer)
        {
            JArray  a = null;
            JObject o = null;

            if (reader.TokenType == JsonToken.StartArray)
            {
                a = JArray.Load(reader);
            }
            else if (reader.TokenType == JsonToken.StartObject)
            {
                o = JObject.Load(reader);
            }
            else if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            if (type == typeof(Vector2))
            {
                return(new Vector2((float)a[0], (float)a[1]));
            }
            if (type == typeof(Vector3))
            {
                return(new Vector3((float)a[0], (float)a[1], (float)a[2]));
            }
            if (type == typeof(Vector4))
            {
                return(new Vector4((float)a[0], (float)a[1], (float)a[2], (float)a[3]));
            }
            if (type == typeof(Vector2Int))
            {
                return(new Vector2Int((int)a[0], (int)a[1]));
            }
            if (type == typeof(Vector3Int))
            {
                return(new Vector3Int((int)a[0], (int)a[1], (int)a[2]));
            }
            if (type == typeof(Rect))
            {
                return(new Rect((float)a[0], (float)a[1], (float)a[2], (float)a[3]));
            }
            if (type == typeof(RectInt))
            {
                return(new RectInt((int)a[0], (int)a[1], (int)a[2], (int)a[3]));
            }
            if (type == typeof(Color))
            {
                return(new Color((float)a[0], (float)a[1], (float)a[2], (float)a[3]));
            }
            if (type == typeof(Color32))
            {
                return(new Color32((byte)a[0], (byte)a[1], (byte)a[2], (byte)a[3]));
            }
            if (type == typeof(Matrix4x4))
            {
                var row0 = (JArray)a[0];
                var row1 = (JArray)a[1];
                var row2 = (JArray)a[2];
                var row3 = (JArray)a[3];
                return(new Matrix4x4()
                {
                    m00 = (float)row0[0],
                    m01 = (float)row0[1],
                    m02 = (float)row0[2],
                    m03 = (float)row0[3],
                    m10 = (float)row1[0],
                    m11 = (float)row1[1],
                    m12 = (float)row1[2],
                    m13 = (float)row1[3],
                    m20 = (float)row2[0],
                    m21 = (float)row2[1],
                    m22 = (float)row2[2],
                    m23 = (float)row2[3],
                    m30 = (float)row3[0],
                    m31 = (float)row3[1],
                    m32 = (float)row3[2],
                    m33 = (float)row3[3],
                });
            }
            if (type == typeof(Bounds))
            {
                var a1 = (JArray)a[0];
                var a2 = (JArray)a[1];
                return(new Bounds(
                           new Vector3((float)a1[0], (float)a1[1], (float)a1[2]),
                           new Vector3((float)a2[0], (float)a2[1], (float)a2[2])
                           ));
            }
            if (type == typeof(BoundsInt))
            {
                var a1 = (JArray)a[0];
                var a2 = (JArray)a[1];
                return(new BoundsInt(
                           new Vector3Int((int)a1[0], (int)a1[1], (int)a1[2]),
                           new Vector3Int((int)a2[0], (int)a2[1], (int)a2[2])
                           ));
            }
            if (type == typeof(Texture2D) || type == typeof(Sprite) || type == typeof(Mesh) || type == typeof(Material))
            {
                int instanceId = (int)o["InstanceId"];
                var result     = BlueprintUtil.FindObjectByInstanceId(instanceId, type);
                if (result == null)
                {
                    Main.DebugLog($"Couldn't find resource {type.Name}({instanceId}) {o["name"]}");
                }
                else
                {
                    Main.DebugLog($"Found resource {type.Name}({instanceId}) {result.name}");
                }
                return(result);
            }
            return(null);
        }