Ejemplo n.º 1
0
        //
        // 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));
        }
Ejemplo n.º 2
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));
        }