Exemplo n.º 1
0
        internal AsyncMeasure(API api)
        {
            rmAPI = api;
            proxy = new APIProxy(api);

            var rmPSHost = new PSHostProxy();

            rmPSHost.Ui.RainmeterAPI = rmAPI;

            var initSS = CreateSessionState();

            runspace = RunspaceFactory.CreateRunspace(rmPSHost, initSS);

            runspace.Open();

            PrepareEnvironment();

            string filePath = rmAPI.ReadPath("ScriptFile", null);

            if (!string.IsNullOrEmpty(filePath))
            {
                if (!File.Exists(filePath))
                {
                    rmAPI.Log(API.LogType.Error, "Script file does not exist.");
                    return;
                }

                script = new Command(File.ReadAllText(filePath), true);
                return;
            }

            script = GetCommandFromLine();
        }
Exemplo n.º 2
0
        internal ParentMeasure(API api)
        {
            rmAPI = api;
            proxy = new APIProxy(api);

            measureName = api.GetMeasureName().ToLowerInvariant();
            skin        = api.GetSkin();

            var initState = CreateSessionState();

            rmPSHost = new PSHostProxy();

            runspace = RunspaceFactory.CreateRunspace(rmPSHost, initState);
            runspace.Open();

            PrepareEnvironment();

            ParentMeasures.Add(this);

            string filePath = rmAPI.ReadPath("ScriptFile", null);

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            if (!File.Exists(filePath))
            {
                rmAPI.Log(API.LogType.Error, "Script file does not exist.");
                return;
            }

            type = ScriptType.FileNoUpdate;
            string rawScript = File.ReadAllText(filePath);

            Task.Run(() =>
            {
                using (Pipeline pipe = runspace.CreatePipeline())
                {
                    pipe.Commands.AddScript(rawScript);

                    try
                    {
                        pipe.Invoke();

                        CommandInfo updateFuncInfo = runspace.SessionStateProxy
                                                     .InvokeCommand.GetCommand("Update", CommandTypes.Function);

                        if (updateFuncInfo == null)
                        {
                            rmAPI.Log(API.LogType.Debug, "Could not find Update function in script file.");
                        }
                        else
                        {
                            type   = ScriptType.File;
                            script = new Command("Update");
                        }
                    }
                    catch (Exception e)
                    {
                        rmAPI.Log(API.LogType.Error, e.ToString());
                        type = ScriptType.NotValid;
                    }
                }

                state = State.Ready;
            });
        }