c# 脚本引擎 脚本编辑器

SuperScript是C#脚本引擎,它为您的WinForms和WPF应用程序带来了高效的代码编辑功能。

它提供了代码编辑功能,如语法高亮显示、智能代码提示和代码完成、语法错误,

它支持类库引用,编译及导出等等,使用它好比您在Visual Studio中编码一样,速度和便利性相媲美。

使用它,可以极大的发挥您的应用程序扩展和开放性,

使得您的程序在发布后,也可支持用户自定义编辑和运行脚本。

码云地址:https://gitee.com/dev2022/super-script-community.git

以下是扩展简单示例:

比如您的应用程序有个计算功能,它允许用户根据用户输入值,通过编辑脚本自定义输出结果功能,如实现字符串倒叙功能。

 

如何调试脚本

程序发布后,如何进行脚本代码调试,目前脚本编辑器调试版本还在研发测试中。不过,可以通过下面这种方式进行调试!

只要电脑上安装visual studio即可(vs2010及以上版本等都可以),这样就可以使用vs的强大调试功能了。

 

//如果要调试代码,需要打开Visual Studio,然后附加应用程序的进程exe,
//然后在脚本编辑器界面勾选右下角【调试模式】,并在脚本中添加和启用下面这个代码,即可使用vs进行代码调试!
if (System.Diagnostics.Debugger.IsAttached)
{
System.Diagnostics.Debugger.Break();
}

 

如何使用Visual Studio进行调试:

(1)启用调试模式

 

 

(2)打开VS附加程序进程

 

 

 

(3)运行程序,即可命中断点跳转到vs并指向代码断点位置

 

 

 

 

 

示例代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.IO;
 11 using BCL.SuperScript;
 12 using BCL.SuperScript.Engine;
 13 using System.Diagnostics;
 14 
 15 namespace TestScript
 16 {
 17     public partial class FrmTest : Form
 18     {
 19         /// <summary>脚本对应的基类</summary>
 20         private CalcBase calcBase = null;
 21 
 22         //每个脚本文件都要对应一个基类,下面定义一个计算的基类
 23         public class CalcBase
 24         {
 25             //这里定义了一个计算的方法,当然这里还可以定义其他更多方法
 26 
 27             /// <summary>计算</summary>
 28             /// <param name="input">输入</param>
 29             /// <returns>输出</returns>
 30             public virtual string Calc(string input)
 31             {
 32                 string output = input;
 33                 return output;
 34             }
 35         }
 36 
 37 
 38         /// <summary>脚本构造器</summary>
 39         private WagScriptBuild<CalcBase> build = null;
 40 
 41         //脚本可以存储到文件,数据库表中,等等,下面以脚本存储到文件为示例
 42         //程序目录下放了2个脚本文件:calc_template.script 和 calc_custom.script
 43         //脚本文件的即是基类的默认实现子类,格式和内容可查看2个示例文件
 44 
 45         /// <summary>默认初始化脚本读取和存储的文件路径</summary>
 46 
 47         private string _file_script_template = Path.Combine(Application.StartupPath, "calc_template.script");
 48 
 49         /// <summary>自定义脚本读取和存储的文件路径</summary>
 50         private string _file_script_custom = Path.Combine(Application.StartupPath, "calc_custom.script");
 51 
 52 
 53 
 54         /// <summary>脚本内容</summary>
 55         private string _script = "";
 56 
 57         public FrmTest()
 58         {
 59             InitializeComponent();
 60 
 61             //软件启动的地方加上脚本组件的注册(社区版免费版授权)
 62             bool regSuccess = false;
 63             string regMessage = "";
 64             regSuccess = WagScriptLic.Reg("community", "117faef434b61d2f1e906ba261877025302fa9f9c8e6701ae2e5d6a32436df02dbfb99cab52e908bf3a52ec33d989b4862d116ee5b1ab5a2b6ba2cf2ad0eaf582f870d56966c3fce6452b781f6cfb37558eac161ce592eebf7362aea010c8cd8d879f1c1242ac1573dff9cad14f8fbdc98fb92f78002e6e3c009c9752b18e1b0fc1d1aafec259bf668402652aa2c6abb49264b278562175f24b95168da61d96b3d5ea460b9fb94dd396ddb1eb898dd6008f9efdad7ceb978ba82b884fe0d69cf42a0d3f2d10236cc521a94ef068e38533045f945fb601424b8aa729360f9dcb13b0b87d95be61b9a87f2b384a5080feeddb0faa28a30b4c78449769de83ddfa1490d783893d20a29f9a61d5e6b7494d62d90b271ec8f606db1d5de7b4035845c2f5bcbcec0d487449d97dafd14d0a7f8", ref regMessage);
 65             if (!regSuccess)
 66             {
 67                 MessageBox.Show(regMessage, "Reg");
 68             }
 69 
 70             //读取文件中的脚本内容
 71 
 72             //如果用户首次使用脚本,则加载默认脚本模板,否则使用上次存储的脚本
 73             if (!File.Exists(_file_script_custom))
 74             {
 75                 if (!File.Exists(_file_script_template))
 76                 {
 77                     MessageBox.Show($"未找到脚本模板(模板脚本),请检查:{_file_script_template}");
 78                     this.btn_srcipt.Enabled = false;
 79                     return;
 80                 }
 81 
 82                 File.Copy(_file_script_template, _file_script_custom);
 83             }
 84 
 85             string data = File.ReadAllText(_file_script_custom, Encoding.UTF8);
 86             _script = data;
 87 
 88         }
 89 
 90         private void _Load(object sender, EventArgs e)
 91         {
 92             if (!string.IsNullOrWhiteSpace(_script))
 93             {
 94                 build = new WagScriptBuild<CalcBase>(_script);
 95 
 96                 calcBase = build.BuildScript(false);//编译脚本
 97 
 98                 if (calcBase == null)
 99                 {
100                     MessageBox.Show($"脚本编译异常: {build.m_sScriptError}");
101                     calcBase = new CalcBase();
102                 }
103             }
104             else
105             {
106                 calcBase = new CalcBase();
107             }
108         }
109 
110 
111         /// <summary>修改脚本</summary>
112         private void btn_srcipt_Click(object sender, EventArgs e)
113         {
114             build.EditExistingScript(true);
115 
116             string s = build.m_WagScriptSupport.SaveToStr("");
117             _script = s;
118 
119             File.WriteAllText(_file_script_custom, s, Encoding.UTF8);//存储修改后的脚本到文件
120 
121             calcBase = build.BuildScript(false);//再次编译脚本
122 
123         }
124 
125         /// <summary>计算</summary>
126         private void btn_run_Click(object sender, EventArgs e)
127         {
128             string input = this.txt_input.Text.Trim();
129             string output = this.calcBase.Calc(input);
130             this.txt_output.Text = output;
131         }
132 
133     }
134 }

 

posted @ 2022-05-23 22:50  Wagwei  阅读(3544)  评论(0编辑  收藏  举报