Esempio n. 1
0
        public void Disable(UnityModManager.ModEntry modEntry, bool unpatch = false)
        {
            _logger = modEntry.Logger;

            using (ProcessLogger process = new ProcessLogger(_logger))
            {
                process.Log("Disabling.");

                Enabled = false;

                // use try-catch to prevent the progression being disrupt by exceptions
                if (_eventHandlers != null)
                {
                    process.Log("Raising events: OnDisable()");
                    for (int i = _eventHandlers.Count - 1; i >= 0; i--)
                    {
                        try { _eventHandlers[i].HandleModDisable(); }
                        catch (Exception e) { Error(e); }
                    }
                    _eventHandlers = null;
                }

                if (unpatch)
                {
                    HarmonyInstance harmonyInstance = HarmonyInstance.Create(modEntry.Info.Id);
                    foreach (MethodBase method in harmonyInstance.GetPatchedMethods().ToList())
                    {
                        Patches             patchInfo = harmonyInstance.GetPatchInfo(method);
                        IEnumerable <Patch> patches   =
                            patchInfo.Transpilers.Concat(patchInfo.Postfixes).Concat(patchInfo.Prefixes)
                            .Where(patch => patch.owner == modEntry.Info.Id);
                        if (patches.Any())
                        {
                            process.Log($"Unpatching: {patches.First().patch.DeclaringType.DeclaringType?.Name}.{method.DeclaringType.Name}.{method.Name}");
                            foreach (Patch patch in patches)
                            {
                                try { harmonyInstance.Unpatch(method, patch.patch); }
                                catch (Exception e) { Error(e); }
                            }
                        }
                    }
                    Patched = false;
                }

                modEntry.OnSaveGUI -= HandleSaveGUI;
                Core     = null;
                Settings = null;
                Version  = null;
                _logger  = null;

                process.Log("Disabled.");
            }
        }
        public void StartReporterThread()
        {
            var settings = new Settings(new SettingsReader("ActivityLogger.ini"));

            ActivityReport  = new ActivityReport();
            _activityLogger = ActivityLogger.Instance(ActivityReport, settings);

            var mouseClickLogger   = MouseClickLogger.Instance();
            var mouseClickReporter = MouseClickReporter.Instance(_activityLogger);

            mouseClickReporter.Subscribe(mouseClickLogger);

            var keyLogger   = KeyLogger.Instance();
            var keyReporter = KeyReporter.Instance(_activityLogger);

            keyReporter.Subscribe(keyLogger);

            Task.Factory.StartNew(() =>
            {
                var activityReporter = new ActivityReporter(_activityReceiver);
                activityReporter.Subscribe(_activityLogger);

                var mouseLogger   = new MouseLogger();
                var mouseReporter = new MouseReporter(_activityLogger);
                mouseReporter.Subscribe(mouseLogger);

                var processLogger   = new ProcessLogger();
                var processReporter = new ProcessReporter(_activityLogger);
                processReporter.Subscribe(processLogger);

                var timeLogger   = new TimeLogger();
                var timeReporter = new TimeReporter(_activityLogger);
                timeReporter.Subscribe(timeLogger);

                var activityTypeLogger   = new ActivityTypeLogger(settings);
                var activityTypeReporter = new ActivityTypeReporter(_activityLogger);
                activityTypeReporter.Subscribe(activityTypeLogger);

                while (true)
                {
                    Thread.Sleep(1000);

                    // KeyLogger & MouseClickLogger will log when keystrokes/clicks are
                    // recorded, so no need to tell it to log here.

                    mouseLogger.Log();
                    processLogger.Log();
                    timeLogger.Log();

                    activityTypeLogger.DetermineActivityType(
                        processReporter.ProcessReport, mouseReporter.MouseReport,
                        MouseClickReporter.Instance().MouseClickReport, KeyReporter.Instance().KeyReport);
                    activityTypeLogger.Log();

                    _activityLogger.Log();
                }
            });
        }
Esempio n. 3
0
        public void Enable(UnityModManager.ModEntry modEntry, Assembly assembly)
        {
            _logger = modEntry.Logger;

            if (Enabled)
            {
                Debug("Already enabled.");
                return;
            }

            using (ProcessLogger process = new ProcessLogger(_logger))
            {
                try
                {
                    process.Log("Enabling.");

                    process.Log("Loading settings.");
                    modEntry.OnSaveGUI += HandleSaveGUI;
                    Version             = modEntry.Version;
                    Settings            = UnityModManager.ModSettings.Load <TSettings>(modEntry);
                    Core = new TCore();

                    Type[] types = assembly.GetTypes();

                    if (!Patched)
                    {
                        HarmonyInstance harmonyInstance = HarmonyInstance.Create(modEntry.Info.Id);
                        foreach (Type type in types)
                        {
                            List <HarmonyMethod> harmonyMethods = type.GetHarmonyMethods();
                            if (harmonyMethods != null && harmonyMethods.Count() > 0)
                            {
                                process.Log($"Patching: {type.DeclaringType?.Name}.{type.Name}");
                                HarmonyMethod  attributes     = HarmonyMethod.Merge(harmonyMethods);
                                PatchProcessor patchProcessor = new PatchProcessor(harmonyInstance, type, attributes);
                                patchProcessor.Patch();
                            }
                        }
                        Patched = true;
                    }

                    Enabled = true;

                    process.Log("Registering events.");
                    _eventHandlers = types.Where(type => type != typeof(TCore) &&
                                                 !type.IsInterface && !type.IsAbstract && typeof(IModEventHandler).IsAssignableFrom(type))
                                     .Select(type => Activator.CreateInstance(type, true) as IModEventHandler).ToList();
                    if (Core is IModEventHandler core)
                    {
                        _eventHandlers.Add(core);
                    }
                    _eventHandlers.Sort((x, y) => x.Priority - y.Priority);

                    process.Log("Raising events: OnEnable()");
                    for (int i = 0; i < _eventHandlers.Count; i++)
                    {
                        _eventHandlers[i].HandleModEnable();
                    }
                }
                catch (Exception e)
                {
                    Error(e);
                    Disable(modEntry, true);
                    throw;
                }

                process.Log("Enabled.");
            }
        }
Esempio n. 4
0
        public void Enable(UnityModManager.ModEntry modEntry, Assembly assembly)
        {
            _logger = modEntry.Logger;

            if (Enabled)
            {
                Debug("Already enabled.");
                return;
            }

            using (ProcessLogger process = new ProcessLogger(_logger)) {
                try {
                    process.Log("Enabling.");
                    var dict = Harmony.VersionInfo(out var myVersion);
                    process.Log($"Harmony version: {myVersion}");
                    foreach (var entry in dict)
                    {
                        process.Log($"Mod {entry.Key} loaded with Harmony version {entry.Value}");
                    }

                    process.Log("Loading settings.");
                    modEntry.OnSaveGUI += HandleSaveGUI;
                    Version             = modEntry.Version;
                    Settings            = UnityModManager.ModSettings.Load <TSettings>(modEntry);
                    Core = new TCore();

                    Type[] types = assembly.GetTypes();

                    if (!Patched)
                    {
                        Harmony harmonyInstance = new Harmony(modEntry.Info.Id);
                        foreach (Type type in types)
                        {
                            List <HarmonyMethod> harmonyMethods = HarmonyMethodExtensions.GetFromType(type);
                            if (harmonyMethods != null && harmonyMethods.Count() > 0)
                            {
                                process.Log($"Patching: {type.FullName}");
                                try {
                                    PatchClassProcessor patchProcessor = harmonyInstance.CreateClassProcessor(type);
                                    patchProcessor.Patch();
                                }
                                catch (Exception e) {
                                    Error(e);
                                }
                            }
                        }
                        Patched = true;
                    }

                    Enabled = true;

                    process.Log("Registering events.");
                    _eventHandlers = types.Where(type => type != typeof(TCore) &&
                                                 !type.IsInterface && !type.IsAbstract && typeof(IModEventHandler).IsAssignableFrom(type))
                                     .Select(type => Activator.CreateInstance(type, true) as IModEventHandler).ToList();
                    if (Core is IModEventHandler core)
                    {
                        _eventHandlers.Add(core);
                    }
                    _eventHandlers.Sort((x, y) => x.Priority - y.Priority);

                    process.Log("Raising events: OnEnable()");
                    for (int i = 0; i < _eventHandlers.Count; i++)
                    {
                        _eventHandlers[i].HandleModEnable();
                    }
                }
                catch (Exception e) {
                    Error(e);
                    Disable(modEntry, true);
                    throw;
                }

                process.Log("Enabled.");
            }
        }