Esempio n. 1
0
        public void CleanupHooks()
        {
            StopIPCServer();

            IOCallbacks.Clear();
            IOTargetFilePaths.Clear();
        }
Esempio n. 2
0
        //
        // Core hook methods

        public IProcess CreateAndHook(
            SMCollection collection,
            ISMHookSystem systemCallback,
            IEnumerable <ISMHookIO> ioCallbacks)
        {
            try
            {
                SystemCallback = systemCallback;
                IOCallbacks.AddRange(ioCallbacks);

                IOTargetFilePaths.AddRange(IOCallbacks.SelectMany(c => c.GetTargetFilePaths()));

                // Initialize event to non-Signaled
                HookInitEvent = new AutoResetEvent(false);
                SMAInitEvent  = new AutoResetEvent(false);

                HookSuccess   = false;
                HookException = null;

                // Start a new IPC server
                StartIPCServer();

                // Start SuperMemo application with given collection as parameter,
                // and immediatly install hooks
                RemoteHooking.CreateAndInject(
                    SMConst.BinPath,
                    collection.GetKnoFilePath().Quotify(),
                    0,
                    InjectionOptions.Default,
                    SMAConst.Assembly.GetInjectionLibFilePath(),
                    null,
                    out var pId
                    );

                // Wait for Signal from OnHookInstalled with timeout
                HookInitEvent.WaitOne(WaitTimeout);

                if (HookSuccess == false)
                {
                    StopIPCServer();

                    var ex = new HookException("Hook setup failed: " + HookException?.Message,
                                               HookException);
                    HookException = null;

                    throw ex;
                }

                return(new ProcessSharp <SM17Natives>(
                           pId,
                           Process.NET.Memory.MemoryType.Remote,
                           true,
                           SMA.Instance.Config.PatternsHintAddresses));
            }
            finally
            {
                HookInitEvent = null;
            }
        }
        //
        // Core hook methods

        public async Task <IProcess> CreateAndHook(
            SMCollection collection,
            string binPath,
            ISMAHookSystem systemCallback,
            IEnumerable <ISMAHookIO> ioCallbacks)
        {
            LogTo.Debug("Starting and injecting SuperMemo");

            SystemCallback = systemCallback;
            IOCallbacks.AddRange(ioCallbacks);

            IOTargetFilePaths.AddRange(IOCallbacks.SelectMany(c => c.GetTargetFilePaths()));

            HookSuccess   = false;
            HookException = null;

            // Start a new IPC server
            var channelName = StartIPCServer();

            // Start SuperMemo application with given collection as parameter,
            // and immediatly install hooks
            RemoteHooking.CreateAndInject(
                binPath,
                collection.GetKnoFilePath().Quotify(),
                0,
                InjectionOptions.Default,
                SMAFileSystem.InjectionLibFile.FullPath,
                null,
                out var pId,
                channelName
                );

            LogTo.Debug("Waiting for signal from Injected library");

            // Wait for Signal from OnHookInstalled with timeout
            await HookInitEvent.WaitAsync(WaitTimeout);

            if (HookSuccess == false)
            {
                LogTo.Debug("Hook failed, aborting");

                StopIPCServer();

                var ex = new HookException("Hook setup failed: " + HookException?.Message,
                                           HookException);
                HookException = null;

                throw ex;
            }

            LogTo.Debug($"SuperMemo started and injected, pId: {pId}");

            return(new ProcessSharp <SM17Natives>(
                       pId,
                       Process.NET.Memory.MemoryType.Remote,
                       true,
                       SMA.SMA.Instance.StartupConfig.PatternsHintAddresses));
        }
Esempio n. 4
0
        //
        // Core hook methods

        public async Task <ProcessSharp <SMNatives> > CreateAndHookAsync(
            SMCollection collection,
            string binPath,
            IEnumerable <ISMAHookIO> ioCallbacks,
            NativeData nativeData)
        {
            LogTo.Debug("Starting and injecting SuperMemo");

            IOCallbacks.AddRange(ioCallbacks);

            IOTargetFilePaths.AddRange(IOCallbacks.SelectMany(c => c.GetTargetFilePaths()));

            HookSuccess   = false;
            HookException = null;

            // Start a new IPC server
            var channelName = StartIPCServer();

            // Start SuperMemo application with given collection as parameter,
            // and immediately install hooks
            int pId = -1;

            try
            {
                RemoteHooking.CreateAndInject(
                    binPath,
                    collection.GetKnoFilePath().Quotify(),
                    0,
                    InjectionOptions.Default,
                    SMAFileSystem.InjectionLibFile.FullPath,
                    null,
                    out pId,
                    channelName,
                    nativeData
                    );
            }
            catch (ArgumentException ex)
            {
                LogTo.Warning(ex, "Failed to start and inject SuperMemo. Command line: '{BinPath} {V}'", binPath, collection.GetKnoFilePath().Quotify());
            }

            LogTo.Debug("Waiting for signal from Injected library");

            // Wait for Signal from OnHookInstalled with timeout
            await HookInitEvent.WaitAsync(WaitTimeout).ConfigureAwait(false);

            if (HookSuccess == false)
            {
                LogTo.Debug("Hook failed, aborting");

                StopIPCServer();

                var ex = new HookException("Hook setup failed: " + HookException?.Message,
                                           HookException);
                HookException = null;

                throw ex;
            }

            LogTo.Debug("SuperMemo started and injected, pId: {PId}", pId);

            return(new ProcessSharp <SMNatives>(
                       pId,
                       Process.NET.Memory.MemoryType.Remote,
                       true,
                       Core.CoreConfig.SuperMemo.PatternsHintAddresses,
                       nativeData));
        }