/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
v2.0.2
src/oscript/ExecuteScriptBehavior.cs
110 строк
3 KB
EvilBeaver
Удален устаревший метод OnAttach и массово исправлены неиспользуемые using
02 дек 2025, 11:20
02 дек 2025, 11:20
0301fed
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using OneScript.StandardLibrary; using ScriptEngine; using ScriptEngine.HostedScript; using ScriptEngine.Hosting; using ScriptEngine.Machine; using ScriptEngine.Machine.Debugger; namespace oscript { class ExecuteScriptBehavior : AppBehavior, IHostApplication, ISystemLogWriter { protected string[] _scriptArgs; protected string _path; public ExecuteScriptBehavior(string path, string[] args) { _scriptArgs = args; _path = path; } public IDebugger DebugController { get; set; } = new DisabledDebugger(); public string CodeStatFile { get; set; } public bool CodeStatisticsEnabled => CodeStatFile != null; public override int Execute() { if (!System.IO.File.Exists(_path)) { Echo($"Script file is not found '{_path}'"); return 2; } SystemLogger.SetWriter(this); var builder = ConsoleHostBuilder.Create(_path); builder.WithDebugger(DebugController); CodeStatProcessor codeStatProcessor = null; if (CodeStatisticsEnabled) { codeStatProcessor = new CodeStatProcessor(); builder.Services.RegisterSingleton<ICodeStatCollector>(codeStatProcessor); } var hostedScript = ConsoleHostBuilder.Build(builder); var source = hostedScript.Loader.FromFile(_path); Process process; try { process = hostedScript.CreateProcess(this, source); } catch (Exception e) { ShowExceptionInfo(e); return 1; } var result = process.Start(); hostedScript.Dispose(); if (codeStatProcessor != null) { codeStatProcessor.EndCodeStat(); var codeStat = codeStatProcessor.GetStatData(); var statsWriter = new CodeStatWriter(CodeStatFile, CodeStatWriterType.JSON); statsWriter.Write(codeStat); } return result; } #region IHostApplication Members public void Echo(string text, MessageStatusEnum status = MessageStatusEnum.Ordinary) { ConsoleHostImpl.Echo(text, status); } public void ShowExceptionInfo(Exception exc) { ConsoleHostImpl.ShowExceptionInfo(exc); } public bool InputString(out string result, string prompt, int maxLen, bool multiline) { return ConsoleHostImpl.InputString(out result, prompt, maxLen, multiline); } public string[] GetCommandLineArguments() { return _scriptArgs; } #endregion public void Write(string text) { Console.Error.WriteLine(text); } } }