Skip to content

Commit 5130ff8

Browse files
committed
Added basic support to execute python scripts.
1 parent 51dc1ff commit 5130ff8

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

PythonScriptingPluginExt.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
using System.Drawing;
33
using System.Linq;
44
using System.Windows.Forms;
5+
using IronPython.Hosting;
6+
using Microsoft.Scripting.Hosting;
7+
using ReClassNET;
58
using ReClassNET.Plugins;
69

710
namespace PythonScriptingPlugin
811
{
912
public class PythonScriptingPluginExt : Plugin
1013
{
1114
private IPluginHost host;
15+
private ScriptEngine engine;
1216

1317
public override Image Icon => Properties.Resources.B16x16_Logo;
1418

@@ -28,19 +32,84 @@ public override bool Initialize(IPluginHost pluginHost)
2832

2933
host = pluginHost;
3034

35+
engine = Python.CreateEngine();
36+
engine.Runtime.LoadAssembly(typeof(IntPtr).Assembly);
37+
engine.Runtime.LoadAssembly(typeof(Program).Assembly);
38+
3139
var scriptingMenuItem = new ToolStripMenuItem("Scripts");
3240

3341
var editorMenuItem = new ToolStripMenuItem("Editor");
3442
scriptingMenuItem.DropDownItems.Add(editorMenuItem);
3543

44+
var testMenuItem = new ToolStripMenuItem("Test");
45+
testMenuItem.Click += (sender, args) =>
46+
{
47+
/*var expression = @"data = process.ReadRemoteMemory(IntPtr(0xFFD20000), 4)
48+
49+
logger.Log(LogLevel.Error, str(data[0]))
50+
logger.Log(LogLevel.Error, str(data[1]))";*/
51+
52+
var expression = @"for m in process.Modules:
53+
logger.Log(LogLevel.Error, m.Name)";
54+
55+
try
56+
{
57+
ExecuteScript(expression);
58+
}
59+
catch (Exception e)
60+
{
61+
Program.ShowException(e);
62+
}
63+
};
64+
scriptingMenuItem.DropDownItems.Add(testMenuItem);
65+
3666
host.MainWindow.MainMenu.Items.Insert(3, scriptingMenuItem);
3767

3868
return true;
3969
}
4070

4171
public override void Terminate()
4272
{
73+
engine = null;
74+
4375
host = null;
4476
}
77+
78+
private ScriptScope CreateReClassScope()
79+
{
80+
var scope = engine.CreateScope();
81+
82+
dynamic s = scope;
83+
84+
s.Is64Bit = false;
85+
86+
s.ReClassName = Constants.ApplicationName;
87+
s.ReClassVersion = Constants.ApplicationVersion;
88+
89+
s.logger = host.Logger;
90+
s.process = host.Process;
91+
92+
return scope;
93+
}
94+
95+
private static string CreateExpressionPreamble()
96+
{
97+
return "from System import *\n"
98+
+ "from ReClassNET import *\n"
99+
+ "from ReClassNET.Logger import *\n"
100+
+ "from ReClassNET.Memory import *\n"
101+
+ "from ReClassNET.MemoryScanner import *\n"
102+
+ "from ReClassNET.Nodes import *\n"
103+
+ "from ReClassNET.Util import *\n";
104+
}
105+
106+
private object ExecuteScript(string code)
107+
{
108+
var scope = CreateReClassScope();
109+
110+
code = CreateExpressionPreamble() + code;
111+
112+
return engine.Execute(code, scope);
113+
}
45114
}
46115
}

0 commit comments

Comments
 (0)