<commit_before>﻿using System.Reflection;
using ColossalFramework;
using UnityEngine;

namespace BuildingThemes.Detour
{
    public class DistrictManagerDetour : DistrictManager
    {

        private static bool deployed = false;

        private static RedirectCallsState _DistrictManager_ReleaseDistrictImplementation_state;
        private static MethodInfo _DistrictManager_ReleaseDistrictImplementation_original;
        private static MethodInfo _DistrictManager_ReleaseDistrictImplementation_detour;

        public static void Deploy()
        {
            if (!deployed)
            {
                _DistrictManager_ReleaseDistrictImplementation_original = typeof(DistrictManager).GetMethod("ReleaseDistrictImplementation", BindingFlags.Instance | BindingFlags.NonPublic);
                _DistrictManager_ReleaseDistrictImplementation_detour = typeof(DistrictManagerDetour).GetMethod("ReleaseDistrictImplementation", BindingFlags.Instance | BindingFlags.NonPublic);
                _DistrictManager_ReleaseDistrictImplementation_state = RedirectionHelper.RedirectCalls(_DistrictManager_ReleaseDistrictImplementation_original, _DistrictManager_ReleaseDistrictImplementation_detour);

                deployed = true;

                Debugger.Log("Building Themes: DistrictManager Methods detoured!");
            }
        }

        public static void Revert()
        {
            if (deployed)
            {
                RedirectionHelper.RevertRedirect(_DistrictManager_ReleaseDistrictImplementation_original, _DistrictManager_ReleaseDistrictImplementation_state);
                _DistrictManager_ReleaseDistrictImplementation_original = null;
                _DistrictManager_ReleaseDistrictImplementation_detour = null;

                deployed = false;

                Debugger.Log("Building Themes: DistrictManager Methods restored!");
            }
        }

        // Detour

        // The original ReleaseDistrictImplementation method.
        // It's same as vanilla implementation with only difference that out BuildingThemesManager#ToggleThemeManagement() method gets called

        private void ReleaseDistrictImplementation(byte district, ref District data)
        {
            BuildingThemesManager.instance.ToggleThemeManagement(district, false);
            if (data.m_flags == District.Flags.None)
                return;
            Singleton<InstanceManager>.instance.ReleaseInstance(new InstanceID()
            {
                District = district
            });
            data.m_flags = District.Flags.None;
            data.m_totalAlpha = 0U;
            this.m_districts.ReleaseItem(district);
            this.m_districtCount = (int)this.m_districts.ItemCount() - 1;
        } 
    }
}<commit_msg>This way the method won't be called for non-existing districts.<commit_after>﻿using System.Reflection;
using ColossalFramework;
using UnityEngine;

namespace BuildingThemes.Detour
{
    public class DistrictManagerDetour : DistrictManager
    {

        private static bool deployed = false;

        private static RedirectCallsState _DistrictManager_ReleaseDistrictImplementation_state;
        private static MethodInfo _DistrictManager_ReleaseDistrictImplementation_original;
        private static MethodInfo _DistrictManager_ReleaseDistrictImplementation_detour;

        public static void Deploy()
        {
            if (!deployed)
            {
                _DistrictManager_ReleaseDistrictImplementation_original = typeof(DistrictManager).GetMethod("ReleaseDistrictImplementation", BindingFlags.Instance | BindingFlags.NonPublic);
                _DistrictManager_ReleaseDistrictImplementation_detour = typeof(DistrictManagerDetour).GetMethod("ReleaseDistrictImplementation", BindingFlags.Instance | BindingFlags.NonPublic);
                _DistrictManager_ReleaseDistrictImplementation_state = RedirectionHelper.RedirectCalls(_DistrictManager_ReleaseDistrictImplementation_original, _DistrictManager_ReleaseDistrictImplementation_detour);

                deployed = true;

                Debugger.Log("Building Themes: DistrictManager Methods detoured!");
            }
        }

        public static void Revert()
        {
            if (deployed)
            {
                RedirectionHelper.RevertRedirect(_DistrictManager_ReleaseDistrictImplementation_original, _DistrictManager_ReleaseDistrictImplementation_state);
                _DistrictManager_ReleaseDistrictImplementation_original = null;
                _DistrictManager_ReleaseDistrictImplementation_detour = null;

                deployed = false;

                Debugger.Log("Building Themes: DistrictManager Methods restored!");
            }
        }

        // Detour

        // The original ReleaseDistrictImplementation method.
        // It's same as vanilla implementation with only difference that out BuildingThemesManager#ToggleThemeManagement() method gets called

        private void ReleaseDistrictImplementation(byte district, ref District data)
        {
            if (data.m_flags == District.Flags.None)
                return;
            BuildingThemesManager.instance.ToggleThemeManagement(district, false);
            Singleton<InstanceManager>.instance.ReleaseInstance(new InstanceID()
            {
                District = district
            });
            data.m_flags = District.Flags.None;
            data.m_totalAlpha = 0U;
            this.m_districts.ReleaseItem(district);
            this.m_districtCount = (int)this.m_districts.ItemCount() - 1;
        } 
    }
}
{'fraction_non_alphanumeric': 0.05881294964028777, 'fraction_numerical': 0.0007194244604316547, 'mean_word_length': 2.8725626740947074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5208251', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1211, 'n_words': 241}