<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

// TODO: Change this namespace to something specific to your plugin here.
//EG:
// namespace MyPlugin_ARPWrapper
namespace ARPWrapper
{

    ///////////////////////////////////////////////////////////////////////////////////////////
    // BELOW HERE SHOULD NOT BE EDITED - this links to the loaded ARP module without requiring a Hard Dependancy
    ///////////////////////////////////////////////////////////////////////////////////////////
    
    /// <summary>
    /// The Wrapper class to access ARP from another plugin
    /// </summary>
    public class ARPWrapper
    {
        protected static System.Type KSPARPType;
        protected static System.Type KSPARPResourceType;
        //protected static System.Type KSPARPResourceListType;

        protected static Object actualARP=null;

        /// <summary>
        /// This is the Alternate Resource Panel object
        /// 
        /// SET AFTER INIT
        /// </summary>
        public static KSPARPAPI KSPARP=null;

        /// <summary>
        /// Whether we found the KSPAlternateResourcePanel assembly in the loadedassemblies. 
        /// 
        /// SET AFTER INIT
        /// </summary>
        public static Boolean AssemblyExists { get { return (KSPARPType != null); } }
        /// <summary>
        /// Whether we managed to hook the running Instance from the assembly. 
        /// 
        /// SET AFTER INIT
        /// </summary>
        public static Boolean InstanceExists { get { return (KSPARP != null); } }
        /// <summary>
        /// Whether we managed to wrap all the methods/functions from the instance. 
        /// 
        /// SET AFTER INIT
        /// </summary>
        private static Boolean _KSPARPWrapped = false;

        /// <summary>
        /// Whether the object has been wrapped and the APIReady flag is set in the real ARP
        /// </summary>
        public static Boolean APIReady { get { return _KSPARPWrapped && KSPARP.APIReady; } }

        /// <summary>
        /// This method will set up the KSPARP object and wrap all the methods/functions
        /// </summary>
        /// <param name="Force">This option will force the Init function to rebind everything</param>
        /// <returns></returns>
        public static Boolean InitKSPARPWrapper()
        {
            //if (!_KSPARPWrapped )
            //{
                //reset the internal objects
                _KSPARPWrapped = false;
                actualARP = null;
                KSPARP = null;
                LogFormatted("Attempting to Grab KSPARP Types...");

                //find the base type
                KSPARPType = AssemblyLoader.loadedAssemblies
                    .Select(a => a.assembly.GetExportedTypes())
                    .SelectMany(t => t)
                    .FirstOrDefault(t => t.FullName == "KSPAlternateResourcePanel.KSPAlternateResourcePanel");

                if (KSPARPType == null)
                {
                    return false;
                }

                //now the resource Type
                KSPARPResourceType = AssemblyLoader.loadedAssemblies
                    .Select(a => a.assembly.GetExportedTypes())
                    .SelectMany(t => t)
                    .FirstOrDefault(t => t.FullName == "KSPAlternateResourcePanel.ARPResource");

                if (KSPARPResourceType == null)
                {
                    return false;
                }

                //now grab the running instance
                LogFormatted("Got Assembly Types, grabbing Instance");
                actualARP = KSPARPType.GetField("APIInstance", BindingFlags.Public | BindingFlags.Static).GetValue(null);

                if (actualARP==null)
                {
                    LogFormatted("Failed grabbing Instance");
                    return false;
                }

                //If we get this far we can set up the local object and its methods/functions
                LogFormatted("Got Instance, Creating Wrapper Objects");
                KSPARP = new KSPARPAPI(actualARP);
            //}
            _KSPARPWrapped = true;
            return true;
        }


        /// <summary>
        /// The Type that is an analogue of the real ARP. This lets you access all the API-able properties and Methods of the ARP
        /// </summary>
        public class KSPARPAPI
        {
            internal KSPARPAPI(Object ARP)
            {
                //store the actual object
                actualARP = ARP;

                //these sections get and store the reflection info and actual objects where required. Later in the properties we then read the values from the actual objects
                //for events we also add a handler
                LogFormatted("Getting APIReady Object");
                APIReadyField = KSPARPType.GetField("APIReady", BindingFlags.Public | BindingFlags.Static);
                LogFormatted("Success: " + (APIReadyField != null).ToString());
                
                LogFormatted("Getting Vessel Resources Object");
                VesselResourcesField = KSPARPType.GetField("lstResourcesVessel", BindingFlags.Public | BindingFlags.Instance);
                actualVesselResources = VesselResourcesField.GetValue(actualARP);
                LogFormatted("Success: " + (actualVesselResources != null).ToString());
                
                LogFormatted("Getting Last Stage Resources Object");
                LastStageResourcesField = KSPARPType.GetField("lstResourcesLastStage", BindingFlags.Public | BindingFlags.Instance);
                actualLastStageResources = VesselResourcesField.GetValue(actualARP);
                LogFormatted("Success: " + (actualLastStageResources != null).ToString());

                LogFormatted("Getting State Change Event");
                OnMonitorStateChangedEvent = KSPARPType.GetEvent("onMonitorStateChanged", BindingFlags.Public | BindingFlags.Instance);
                AddHandler(OnMonitorStateChangedEvent, actualARP, MonitorStateChanged);
                LogFormatted("Getting Acknowledged Event");
                onAlarmStateChangedEvent = KSPARPType.GetEvent("onAlarmStateChanged", BindingFlags.Public | BindingFlags.Instance);
                AddHandler(onAlarmStateChangedEvent, actualARP, AlarmStateChanged);

                LogFormatted("Getting Acknowledge Method");
                AcknowledgeAlarmMethod = KSPARPType.GetMethod("AcknowledgeAlarm", BindingFlags.Public | BindingFlags.Instance);
            }

            private Object actualARP;

            private FieldInfo APIReadyField;
            /// <summary>
            /// Whether the APIReady flag is set in the real ARP
            /// </summary>
            public Boolean APIReady<commit_msg>No longer using VesselResourcesField for last stage resources.<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

// TODO: Change this namespace to something specific to your plugin here.
//EG:
// namespace MyPlugin_ARPWrapper
namespace ARPWrapper
{

    ///////////////////////////////////////////////////////////////////////////////////////////
    // BELOW HERE SHOULD NOT BE EDITED - this links to the loaded ARP module without requiring a Hard Dependancy
    ///////////////////////////////////////////////////////////////////////////////////////////
    
    /// <summary>
    /// The Wrapper class to access ARP from another plugin
    /// </summary>
    public class ARPWrapper
    {
        protected static System.Type KSPARPType;
        protected static System.Type KSPARPResourceType;
        //protected static System.Type KSPARPResourceListType;

        protected static Object actualARP=null;

        /// <summary>
        /// This is the Alternate Resource Panel object
        /// 
        /// SET AFTER INIT
        /// </summary>
        public static KSPARPAPI KSPARP=null;

        /// <summary>
        /// Whether we found the KSPAlternateResourcePanel assembly in the loadedassemblies. 
        /// 
        /// SET AFTER INIT
        /// </summary>
        public static Boolean AssemblyExists { get { return (KSPARPType != null); } }
        /// <summary>
        /// Whether we managed to hook the running Instance from the assembly. 
        /// 
        /// SET AFTER INIT
        /// </summary>
        public static Boolean InstanceExists { get { return (KSPARP != null); } }
        /// <summary>
        /// Whether we managed to wrap all the methods/functions from the instance. 
        /// 
        /// SET AFTER INIT
        /// </summary>
        private static Boolean _KSPARPWrapped = false;

        /// <summary>
        /// Whether the object has been wrapped and the APIReady flag is set in the real ARP
        /// </summary>
        public static Boolean APIReady { get { return _KSPARPWrapped && KSPARP.APIReady; } }

        /// <summary>
        /// This method will set up the KSPARP object and wrap all the methods/functions
        /// </summary>
        /// <param name="Force">This option will force the Init function to rebind everything</param>
        /// <returns></returns>
        public static Boolean InitKSPARPWrapper()
        {
            //if (!_KSPARPWrapped )
            //{
                //reset the internal objects
                _KSPARPWrapped = false;
                actualARP = null;
                KSPARP = null;
                LogFormatted("Attempting to Grab KSPARP Types...");

                //find the base type
                KSPARPType = AssemblyLoader.loadedAssemblies
                    .Select(a => a.assembly.GetExportedTypes())
                    .SelectMany(t => t)
                    .FirstOrDefault(t => t.FullName == "KSPAlternateResourcePanel.KSPAlternateResourcePanel");

                if (KSPARPType == null)
                {
                    return false;
                }

                //now the resource Type
                KSPARPResourceType = AssemblyLoader.loadedAssemblies
                    .Select(a => a.assembly.GetExportedTypes())
                    .SelectMany(t => t)
                    .FirstOrDefault(t => t.FullName == "KSPAlternateResourcePanel.ARPResource");

                if (KSPARPResourceType == null)
                {
                    return false;
                }

                //now grab the running instance
                LogFormatted("Got Assembly Types, grabbing Instance");
                actualARP = KSPARPType.GetField("APIInstance", BindingFlags.Public | BindingFlags.Static).GetValue(null);

                if (actualARP==null)
                {
                    LogFormatted("Failed grabbing Instance");
                    return false;
                }

                //If we get this far we can set up the local object and its methods/functions
                LogFormatted("Got Instance, Creating Wrapper Objects");
                KSPARP = new KSPARPAPI(actualARP);
            //}
            _KSPARPWrapped = true;
            return true;
        }


        /// <summary>
        /// The Type that is an analogue of the real ARP. This lets you access all the API-able properties and Methods of the ARP
        /// </summary>
        public class KSPARPAPI
        {
            internal KSPARPAPI(Object ARP)
            {
                //store the actual object
                actualARP = ARP;

                //these sections get and store the reflection info and actual objects where required. Later in the properties we then read the values from the actual objects
                //for events we also add a handler
                LogFormatted("Getting APIReady Object");
                APIReadyField = KSPARPType.GetField("APIReady", BindingFlags.Public | BindingFlags.Static);
                LogFormatted("Success: " + (APIReadyField != null).ToString());
                
                LogFormatted("Getting Vessel Resources Object");
                VesselResourcesField = KSPARPType.GetField("lstResourcesVessel", BindingFlags.Public | BindingFlags.Instance);
                actualVesselResources = VesselResourcesField.GetValue(actualARP);
                LogFormatted("Success: " + (actualVesselResources != null).ToString());
                
                LogFormatted("Getting Last Stage Resources Object");
                LastStageResourcesField = KSPARPType.GetField("lstResourcesLastStage", BindingFlags.Public | BindingFlags.Instance);
                actualLastStageResources = LastStageResourcesField.GetValue(actualARP);
                LogFormatted("Success: " + (actualLastStageResources != null).ToString());

                LogFormatted("Getting State Change Event");
                OnMonitorStateChangedEvent = KSPARPType.GetEvent("onMonitorStateChanged", BindingFlags.Public | BindingFlags.Instance);
                AddHandler(OnMonitorStateChangedEvent, actualARP, MonitorStateChanged);
                LogFormatted("Getting Acknowledged Event");
                onAlarmStateChangedEvent = KSPARPType.GetEvent("onAlarmStateChanged", BindingFlags.Public | BindingFlags.Instance);
                AddHandler(onAlarmStateChangedEvent, actualARP, AlarmStateChanged);

                LogFormatted("Getting Acknowledge Method");
                AcknowledgeAlarmMethod = KSPARPType.GetMethod("AcknowledgeAlarm", BindingFlags.Public | BindingFlags.Instance);
            }

            private Object actualARP;

            private FieldInfo APIReadyField;
            /// <summary>
            /// Whether the APIReady flag is set in the real ARP
            /// </summary>
            public Boolean APIReady
{'fraction_non_alphanumeric': 0.1095920138888889, 'fraction_numerical': 0.0, 'mean_word_length': 1.6829031632058995, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 59, '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': '1546320', 'n_tokens_mistral': 3447, 'n_tokens_neox': 3138, 'n_words': 952}