<commit_before>﻿using Oxide.Core;

using UnityEngine;

namespace Oxide.Unity
{
    /// <summary>
    /// The main MonoBehaviour which calls OxideMod.OnFrame
    /// </summary>
    public class UnityScript : MonoBehaviour
    {
        public static GameObject Instance { get; private set; }

        public static void Create()
        {
            Instance = new GameObject("Oxide.Ext.Unity");
            Object.DontDestroyOnLoad(Instance);
            Instance.AddComponent<UnityScript>();
        }

        private OxideMod oxideMod;

        void Awake()
        {
            oxideMod = Interface.GetMod();
            Application.RegisterLogCallback(HandleException);
        }

        void Update()
        {
            oxideMod.OnFrame(Time.deltaTime);
        }

        void OnDestroy()
        {
            if (oxideMod.IsShuttingDown) return;
            oxideMod.LogWarning("The Oxide Unity Script was destroyed (creating a new instance)");
            oxideMod.NextTick(Create);
        }

        void HandleException(string message, string stack_trace, LogType type)
        {
            if (type == LogType.Exception && stack_trace.Contains("Oxide"))
                RemoteLogger.Exception(message, stack_trace);
        }
    }
}<commit_msg>Detect and support existing Unity 4 log callbacks<commit_after>﻿using System.Reflection;
using Oxide.Core;
using UnityEngine;

namespace Oxide.Unity
{
    /// <summary>
    /// The main MonoBehaviour which calls OxideMod.OnFrame
    /// </summary>
    public class UnityScript : MonoBehaviour
    {
        public static GameObject Instance { get; private set; }

        public static void Create()
        {
            Instance = new GameObject("Oxide.Ext.Unity");
            Object.DontDestroyOnLoad(Instance);
            Instance.AddComponent<UnityScript>();
        }

        private OxideMod oxideMod;

        void Awake()
        {
            oxideMod = Interface.GetMod();
            
            if (typeof(Application).GetEvent("logMessageReceived") == null)
            {
                // Unity 4   
                var log_callback_field = typeof(Application).GetField("s_LogCallback", BindingFlags.Static | BindingFlags.NonPublic);
                var log_callback = log_callback_field.GetValue(null) as Application.LogCallback;
                if (log_callback == null)
                    Interface.Oxide.LogWarning("No Unity application log callback is registered");
                else
                    Application.RegisterLogCallback((message, stack_trace, type) =>
                    {
                        log_callback.Invoke(message, stack_trace, type);
                        LogMessageReceived(message, stack_trace, type);
                    });
            }
            else
            {
                // Unity 5
                Application.logMessageReceived += LogMessageReceived;
            }
        }

        void Update()
        {
            oxideMod.OnFrame(Time.deltaTime);
        }

        void OnDestroy()
        {
            if (oxideMod.IsShuttingDown) return;
            oxideMod.LogWarning("The Oxide Unity Script was destroyed (creating a new instance)");
            oxideMod.NextTick(Create);
        }

        void LogMessageReceived(string message, string stack_trace, LogType type)
        {
            if (type == LogType.Exception && stack_trace.Contains("Oxide"))
                RemoteLogger.Exception(message, stack_trace);
        }
    }
}
{'fraction_non_alphanumeric': 0.08997134670487106, 'fraction_numerical': 0.0008595988538681948, 'mean_word_length': 1.7928, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '5604238', 'n_tokens_mistral': 915, 'n_tokens_neox': 846, 'n_words': 196}