예제 #1
0
        public static void Initialize(ToolController toolController)
        {
            if (sm_initialized)
            {
                return;
            }

            Debug.Log("Traffic++: Initializing Transport Tool.\n");

            TransportTool       originalTransportTool = toolController.GetComponent <TransportTool>();
            CustomTransportTool customTransportTool   = toolController.gameObject.AddComponent <CustomTransportTool>();

            // contributed by Japa
            FieldInfo toolControllerField = typeof(ToolController).GetField("m_tools", BindingFlags.Instance | BindingFlags.NonPublic);

            if (toolControllerField != null)
            {
                toolControllerField.SetValue(toolController, toolController.GetComponents <ToolBase>());
            }
            FieldInfo toolModifierDictionary = typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic);

            if (toolModifierDictionary != null)
            {
                toolModifierDictionary.SetValue(null, null);
            }

            sm_initialized = true;

            Debug.Log("Traffic++: Transport Tool initialized.\n");
        }
        // contributed by Japa
        private static void UpdateTools(ToolController toolController)
        {
            FieldInfo toolControllerField = typeof(ToolController).GetField("m_tools", BindingFlags.Instance | BindingFlags.NonPublic);

            if (toolControllerField != null)
            {
                toolControllerField.SetValue(toolController, toolController.GetComponents <ToolBase>());
            }

            FieldInfo toolModifierDictionary = typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic);

            if (toolModifierDictionary != null)
            {
                toolModifierDictionary.SetValue(null, null); // to force a refresh
            }
            ToolsModifierControl.SetTool <DefaultTool>();
        }
        public override void OnLevelLoaded(LoadMode mode)
        {
            //Turns out this is the wrong way to do it.
            //ReplacePanels();


            //And finally with the road options panel.
            RoadsOptionPanel[] roadsOptionPanels = UnityEngine.Object.FindObjectsOfType <RoadsOptionPanel>();
            foreach (RoadsOptionPanel roadsOptionPanel in roadsOptionPanels)
            {
                GameObject           roadsOptionsPanelObject = roadsOptionPanel.gameObject;
                RoadsOptionFinePanel roadsOptionFinePanel    = roadsOptionsPanelObject.AddComponent <RoadsOptionFinePanel>();
                Object.Destroy(roadsOptionPanel);
            }
            //Find the tool controller, re-make its tool list, and force a re-make of the ToolsModifierControl tool list.
            ToolController toolController = UnityEngine.Object.FindObjectOfType <ToolController>();

            if (toolController != null)
            {
                NetToolFine netToolFine         = toolController.gameObject.AddComponent <NetToolFine>();
                NetTool     netTool             = toolController.gameObject.GetComponent <NetTool>();
                FieldInfo   toolControllerField = typeof(ToolController).GetField("m_tools", BindingFlags.Instance | BindingFlags.NonPublic);
                if (toolControllerField != null)
                {
                    toolControllerField.SetValue(toolController, toolController.GetComponents <ToolBase>());
                }
                FieldInfo toolModifierDictionary = typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic);
                if (toolModifierDictionary != null)
                {
                    toolModifierDictionary.SetValue(null, null);
                }
            }

            //if(mode == LoadMode.LoadAsset || mode == LoadMode.NewAsset) // || mode == LoadMode.LoadMap || mode == LoadMode.NewMap)
            {
                GameObject panelReplacer = UnityEngine.Object.Instantiate(new GameObject());
                panelReplacer.AddComponent <PanelReplacer>();
                panelReplacer.name = "PanelReplacer";
            }
        }
예제 #4
0
        void AddTool <T>(ToolController toolController) where T : ToolBase
        {
            if (toolController.GetComponent <T>() != null)
            {
                return;
            }

            toolController.gameObject.AddComponent <T>();

            // contributed by Japa
            FieldInfo toolControllerField = typeof(ToolController).GetField("m_tools", BindingFlags.Instance | BindingFlags.NonPublic);

            if (toolControllerField != null)
            {
                toolControllerField.SetValue(toolController, toolController.GetComponents <ToolBase>());
            }
            FieldInfo toolModifierDictionary = typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic);

            if (toolModifierDictionary != null)
            {
                toolModifierDictionary.SetValue(null, null); // to force a refresh
            }
        }
예제 #5
0
        //Last Updated 160622
        private static bool AddExtraToolsToController(ref ToolController toolController, List <ToolBase> extraTools)
        {
            bool _result = false;

            Debug.Log("[PLT]: Begin ToolMan.AddExtraToolsToController().");
            Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Progress: 0/4 [    ]");

            if (toolController == null)
            {
                Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): Failed to append PropLineTool to tool controllers: toolController parameter is null.");
                return(false);
            }
            if (extraTools == null)
            {
                Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): Failed to append PropLineTool to tool controllers: extraTools parameter is null.");
                return(false);
            }
            if (extraTools.Count < 1)
            {
                Debug.LogWarning("[PLT]: ToolMan.AddExtraToolsToController(): No tools were found in the extraTools parameter.");
                return(false);
            }

            Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Progress: 1/4 [=   ]");

            var _toolsFieldInfo = typeof(ToolController).GetField("m_tools", BindingFlags.Instance | BindingFlags.NonPublic);

            var _tools = (ToolBase[])_toolsFieldInfo.GetValue(toolController);

            if (_tools == null)
            {
                //old
                //Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): Failed to append PropLineTool to tool controllers: Could not FieldInfo.GetValue() from ToolController.m_tools.");
                //return false;

                //new
                Debug.LogWarning("[PLT]: ToolMan.AddExtraToolsToController(): toolController.m_tools was detected to be null from FieldInfo.GetValue().");
                Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Attempting to pre-populate toolController.m_tools by calling toolController.GetComponents<ToolBase>().");

                _tools = toolController.GetComponents <ToolBase>();

                var _tools2 = (ToolBase[])_toolsFieldInfo.GetValue(toolController);

                if (_tools2 == null)
                {
                    Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): Failed to pre-populate toolController.m_tools. Returning false...");
                    return(false);
                }
                else if (_tools.Length > 0)
                {
                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): The attempt to pre-populate toolController.m_tools appears to have been successful...");
                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Pre-populated toolController.m_tools with >>" + _tools.Length + "<< tools.");
                }
                else
                {
                    Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): The attempt to pre-populate toolController.m_tools failed. No tools were found. Returning false...");
                    return(false);
                }
            }
            if (_tools.Length < 1)
            {
                Debug.LogWarning("[PLT]: ToolMan.AddExtraToolsToController(): Initial ToolController.m_tools has a length < 1. Its length is: " + _tools.Length + ".");

                if (extraTools.Count < 1)
                {
                    Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): Returning false since both toolController.m_tools and extraTools have a length < 1.");
                    return(false);
                }
            }

            Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Progress: 2/4 [==  ]");

            //Prepare toolController.m_tools for appending new elements
            int _initialLength = _tools.Length;

            Array.Resize <ToolBase>(ref _tools, _initialLength + extraTools.Count);
            var index = 0;

            //find ToolsModifierControl tool dictionary
            var _dictionary = (Dictionary <Type, ToolBase>) typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

            if (_dictionary == null)
            {
                //old
                //Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): Failed to append PropLineTool to tool controllers: Could not find ToolsModifierControl.m_Tools dictionary.");
                //return false;

                //new
                Debug.LogWarning("[PLT]: ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools dictionary was detected to be null from FieldInfo.GetField().");

                Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Attempting to pre-populate ToolsModifierControl.m_Tools dictionary by calling ToolsModifierControl.GetTool<PropTool>()...");
                var _propTool = ToolsModifierControl.GetTool <PropTool>();

                var _dictionary2 = (Dictionary <Type, ToolBase>) typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

                if (_dictionary2 == null)
                {
                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Failed in pre-populating ToolsModifierControl.m_Tools by calling ToolsModifierControl.GetTool<PropTool>()...");

                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Attempting to pre-populate ToolsModifierControl.m_Tools by copying the >>" + _tools.Length + "<< elements of toolController.m_tools.");
                    //Our own version of ToolsModifierControl.CollectTools()
                    _dictionary = new Dictionary <Type, ToolBase>(_initialLength + extraTools.Count);
                    for (int i = 0; i < _tools.Length; i++)
                    {
                        _dictionary.Add(_tools[i].GetType(), _tools[i]);
                    }

                    var _dictionary3 = (Dictionary <Type, ToolBase>) typeof(ToolsModifierControl).GetField("m_Tools", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

                    if (_dictionary3 == null)
                    {
                        Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Failed in pre-populating ToolsModifierControl.m_Tools by copying the >>" + _tools.Length + "<< elements of toolController.m_tools.");

                        Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): All attempts to pre-populate ToolsModifierControl.m_Tools failed. ): Returning false...");
                        return(false);
                    }
                    else
                    {
                        _dictionary = _dictionary3;
                        Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): The attempt to pre-populate ToolsModifierControl.m_Tools dictionary by copying the >>" + _tools.Length + "<< elements of toolController.m_tools appears to have been successful...");
                    }
                }
                else
                {
                    _dictionary = _dictionary2;
                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): The attempt to pre-populate ToolsModifierControl.m_Tools dictionary by calling ToolsModifierControl.GetTool<PropTool>() appears to have been successful...");
                }

                Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Verifying the length of the ToolBase collections for both tool controllers...");
                //check that lengths match
                if (_dictionary == null)
                {
                    Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): _dictionary is null! Returning false...");
                    return(false);
                }
                if (_dictionary.Count == _tools.Length)
                {
                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Successfully pre-populated ToolsModifierControl.m_Tools dictionary. Its Count is: " + _dictionary2.Count + ".");
                }
                else
                {
                    //actually since we resized _tools earlier, _tools should be extraTools.Length (1) longer than _dictionary.
                    Debug.LogWarning("[PLT]: ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools.Count does not equal toolController.m_tools.Length");
                    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools.Count = " + _dictionary2.Count + ", and toolController.m_tools.Length = " + _tools.Length + ".");
                }
            }
            else if (_dictionary.Count < 1)
            {
                Debug.LogWarning("[PLT]: ToolMan.AddExtraToolsToController(): Initial ToolsModifierControl.m_Tools dictionary has a count < 1. Its count is: " + _dictionary.Count + ".");
            }

            //append PropLineTool to ToolBase collections
            foreach (var _currentTool in extraTools)
            {
                _dictionary.Add(_currentTool.GetType(), _currentTool);
                _tools[_initialLength + index] = _currentTool;
                index++;
            }

            Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Progress: 3/4 [=== ]");

            _toolsFieldInfo.SetValue(toolController, _tools);

            PropLineTool _propLineTool = ToolsModifierControl.GetTool <PropLineTool>();

            if (_propLineTool == null)
            {
                Debug.LogError("[PLT]: ToolMan.AddExtraToolsToController(): PropLineTool was not found in ToolsModifierControl after appending to the tool dictionary.");
                return(false);
            }

            Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Progress: 4/4 [====]");
            Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Success in appending PropLineTool to tool controllers!");

            //160815 2306
            //These are only really necessary as I suspect two instances of PropLineTool are being created for some users (NullReferenceException when placing each line).
            //I no longer believe this ^ to be the case, so we comment these out.
            //Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): Outputting toolController lists:");
            //Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): toolController.m_tools array has " + _tools.Length + " members:");
            //for (int i = 0; i < _tools.Length; i++)
            //{
            //    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): toolController.m_tools[" + i + "] = " + _tools[i]);
            //}
            //Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools dictionary has " + _dictionary.Count + " members:");
            //var _keys = _dictionary.Keys.ToList<Type>();
            //foreach (Type _type in _keys)
            //{
            //    Debug.Log("[PLT]: ToolMan.AddExtraToolsToController(): ToolsModifierControl.m_Tools[" + _type + "] = " + _dictionary[_type]);
            //}

            _result = true;
            return(_result);
        }