예제 #1
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));
        }
예제 #3
0
        private void LoadConfig(SMCollection collection)
        {
            var knoPath = collection.GetKnoFilePath();

            // StartupCfg

            StartupConfig = Core.Configuration.Load <StartupCfg>().Result ?? new StartupCfg();

            // CollectionsCfg

            _collectionsCfg  = Core.Configuration.Load <CollectionsCfg>().Result ?? new CollectionsCfg();
            CollectionConfig = _collectionsCfg.CollectionsConfig.SafeGet(knoPath);

            if (CollectionConfig == null)
            {
                CollectionConfig = new CollectionCfg();
                _collectionsCfg.CollectionsConfig[knoPath] = CollectionConfig;
            }
        }
        public bool ValidateCollection(SMCollection collection)
        {
            var knoFilePath = new FilePath(collection.GetKnoFilePath());

            if (knoFilePath.Exists() == false ||
                Directory.Exists(collection.GetRootDirPath()) == false)
            {
                Forge.Forms.Show.Window().For(new Alert("Collection doesn't exist anymore.", "Error"));

                return(false);
            }

            // Check whether collection is locked
            if (knoFilePath.IsLocked())
            {
                Forge.Forms.Show.Window().For(new Alert("Collection is locked. Is SuperMemo already running ?", "Error"));

                return(false);
            }

            return(true);
        }
예제 #5
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));
        }