Загрузка данных


using System;
using System.IO;
using System.Reflection;
using System.Text;
using MSCLoader;
using UnityEngine;

namespace MscInspector
{
    public class MscInspectorMod : Mod
    {
        public override string ID { get { return "MscInspector"; } }
        public override string Name { get { return "MSC Inspector"; } }
        public override string Author { get { return "me"; } }
        public override string Version { get { return "1.0"; } }

        public override void ModSetup()
        {
            SetupFunction(Setup.OnLoad, Mod_OnLoad);
        }

        private void Mod_OnLoad()
        {
            GameObject go = new GameObject("MscInspectorRunner");
            go.AddComponent<InspectorBehaviour>();
            UnityEngine.Object.DontDestroyOnLoad(go);
        }
    }

    public class InspectorBehaviour : MonoBehaviour
    {
        private string status = "";
        private float statusUntil;
        private float hintUntil;

        void Start()
        {
            hintUntil = Time.realtimeSinceStartup + 20f;
        }

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.F6)) DumpByNames();
            if (Input.GetKeyDown(KeyCode.F7)) DumpLookedAt();
        }

        void OnGUI()
        {
            float now = Time.realtimeSinceStartup;
            if (now < statusUntil)
                GUI.Label(new Rect(10, 10, 1200, 25), status);
            else if (now < hintUntil)
                GUI.Label(new Rect(10, 10, 1200, 25), "MscInspector: F6 поиск по именам | F7 то, на что смотришь");
        }

        private void SetStatus(string s)
        {
            status = s;
            statusUntil = Time.realtimeSinceStartup + 8f;
        }

        private static string FilePath()
        {
            return Path.Combine(Path.GetDirectoryName(Application.dataPath), "MscInspector.txt");
        }

        private static string GetPath(Transform t)
        {
            string p = t.name;
            while (t.parent != null)
            {
                t = t.parent;
                p = t.name + "/" + p;
            }
            return p;
        }

        private static object Prop(Type t, object o, string name)
        {
            PropertyInfo p = t.GetProperty(name);
            if (p == null) return null;
            return p.GetValue(o, null);
        }

        private void DescribeFsm(Component c, StringBuilder sb)
        {
            try
            {
                Type t = c.GetType();
                sb.AppendLine("    FSM: " + Prop(t, c, "FsmName"));
                object vars = Prop(t, c, "FsmVariables");
                if (vars == null) return;
                Type vt = vars.GetType();
                string[] lists = new string[] { "FloatVariables", "IntVariables", "BoolVariables", "StringVariables", "GameObjectVariables" };
                for (int i = 0; i < lists.Length; i++)
                {
                    Array arr = Prop(vt, vars, lists[i]) as Array;
                    if (arr == null) continue;
                    foreach (object v in arr)
                    {
                        Type et = v.GetType();
                        sb.AppendLine("      " + lists[i].Replace("Variables", "") + " "
                            + Prop(et, v, "Name") + " = " + Prop(et, v, "Value"));
                    }
                }
            }
            catch (Exception e)
            {
                sb.AppendLine("    FSM error: " + e.Message);
            }
        }

        private void Describe(GameObject go, StringBuilder sb)
        {
            sb.AppendLine("OBJECT: " + GetPath(go.transform) + "  [active=" + go.activeInHierarchy + "]");
            Component[] comps = go.GetComponents<Component>();
            for (int i = 0; i < comps.Length; i++)
            {
                if (comps[i] == null) continue;
                string tn = comps[i].GetType().Name;
                sb.AppendLine("  component: " + tn);
                if (tn == "PlayMakerFSM") DescribeFsm(comps[i], sb);
            }
        }

        private void DumpByNames()
        {
            string[] keys = new string[] { "jonnez", "vodka", "booze", "fuel" };
            UnityEngine.Object[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject));
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("==== DumpByNames " + DateTime.Now + " ====");
            int hits = 0;
            for (int i = 0; i < all.Length && hits < 250; i++)
            {
                GameObject go = all[i] as GameObject;
                if (go == null) continue;
                string n = go.name.ToLower();
                bool match = false;
                for (int k = 0; k < keys.Length; k++)
                {
                    if (n.Contains(keys[k])) { match = true; break; }
                }
                if (!match) continue;
                Describe(go, sb);
                hits++;
            }
            File.AppendAllText(FilePath(), sb.ToString());
            SetStatus("MscInspector: найдено объектов " + hits + ", файл MscInspector.txt");
        }

        private void DumpLookedAt()
        {
            Camera cam = Camera.main;
            if (cam == null) cam = (Camera)UnityEngine.Object.FindObjectOfType(typeof(Camera));
            if (cam == null) { SetStatus("MscInspector: камера не найдена"); return; }

            RaycastHit hit;
            Ray ray = new Ray(cam.transform.position, cam.transform.forward);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("==== DumpLookedAt " + DateTime.Now + " ====");
            if (Physics.Raycast(ray, out hit, 6f))
            {
                Transform t = hit.transform;
                int level = 0;
                while (t != null && level < 6)
                {
                    sb.AppendLine("-- level " + level + " --");
                    Describe(t.gameObject, sb);
                    t = t.parent;
                    level++;
                }
            }
            else
            {
                sb.AppendLine("nothing hit within 6 m");
            }
            File.AppendAllText(FilePath(), sb.ToString());
            SetStatus("MscInspector: записано в MscInspector.txt");
        }
    }
}