Skip to content

Commit dffa769

Browse files
committed
Merge branch '1.4'
2 parents 75716ad + 727b69a commit dffa769

31 files changed

Lines changed: 723 additions & 145 deletions

IronPythonPlugin/IronPythonConfig.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,22 @@ public class IronPythonConfig
2121
public static string ControlPermission = "python.control";
2222
[JsonProperty("execute_permission")]
2323
public static string ExecutePermission = "python.execute";
24+
[JsonProperty("iron_python_path")]
25+
public static string IronPythonPath = "C:\\Program Files\\IronPython 3.4";
2426
[JsonProperty("default_environment")]
2527
public static string DefaultEnvironment;
2628
[JsonProperty("untrusted_environment")]
2729
public static string UntrustedEnvironment;
30+
//language=regex
31+
[JsonProperty("inline_code_regex")]
32+
public static string InlineCodeRegex = "<<(.*?)>>";
33+
//language=regex
34+
[JsonProperty("inline_code_with_script_regex")]
35+
public static string InlineCodeWithScriptRegex = "<<<(.*?)>>>";
36+
[JsonProperty("inline_output_color_hex")]
37+
public static string InlineOutputColorHEX = "ff5500";
38+
[JsonProperty("inline_script_color_hex")]
39+
public static string InlineScriptColorHEX = "ffffff";
2840
[JsonProperty("environments")]
2941
public static Dictionary<string, IronPythonEnvironment> Environments;
3042

@@ -66,8 +78,6 @@ public static void Load()
6678

6779
public static void Update()
6880
{
69-
//if (DefaultLua != null && Environments.Count > 0 && !Environments.ContainsKey(DefaultLua))
70-
7181
if (Environments == null)
7282
Environments = new Dictionary<string, IronPythonEnvironment>();
7383
foreach (var pair in Environments)

IronPythonPlugin/IronPythonPlugin.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Reflection;
77
using System.Text;
8+
using System.Text.RegularExpressions;
89
using System.Threading.Tasks;
910
using Terraria;
1011
using TerrariaApi.Server;
@@ -17,14 +18,16 @@ public class IronPythonPlugin : TerrariaPlugin
1718
{
1819
#region Data
1920

20-
public override string Author => "ASgo";
21+
public override string Author => "ASgo & Anzhelika";
2122
public override string Description => "Plugin that provides IronPython to server development";
2223
public override string Name => "IronPythonPlugin";
23-
public override Version Version => new Version(1, 0, 0, 0);
24+
public override Version Version => new Version(2, 0, 0, 0);
2425

2526
public static IronPythonPlugin Instance = null;
2627
public static string[] IronPythonEnv = new string[Main.maxPlayers + 1];
2728
public static Dictionary<string, object> Data = new Dictionary<string, object>();
29+
private static MethodInfo TextFieldSetter = null;
30+
private static MethodInfo CommandFieldSetter = null;
2831

2932
#endregion
3033

@@ -75,13 +78,57 @@ public static void OnServerChat(ServerChatEventArgs args)
7578
if (!player.HasPermission(IronPythonConfig.ExecutePermission) || args.Handled)
7679
return;
7780
args.Handled = CheckIronPythonInput(player, args.Text);
81+
82+
// Replacing script result inside of chat messages
83+
MatchCollection matches;
84+
if (args.Handled)
85+
return;
86+
if (TextFieldSetter == null)
87+
TextFieldSetter = args.GetType().GetProperty("Text").GetSetMethod(true);
88+
try
89+
{
90+
if ((matches = Regex.Matches(args.Text, IronPythonConfig.InlineCodeWithScriptRegex)).Count > 0)
91+
TextFieldSetter.Invoke(args, new object[] { player.PyEnv().ProcessText(args.Text, matches,
92+
IronPythonConfig.InlineOutputColorHEX, true, IronPythonConfig.InlineScriptColorHEX,
93+
new object[] { player }) });
94+
if ((matches = Regex.Matches(args.Text, IronPythonConfig.InlineCodeRegex)).Count > 0)
95+
TextFieldSetter.Invoke(args, new object[] { player.PyEnv().ProcessText(args.Text, matches,
96+
IronPythonConfig.InlineOutputColorHEX, false, IronPythonConfig.InlineScriptColorHEX,
97+
new object[] { player }) });
98+
} catch (Exception e)
99+
{
100+
args.Handled = true;
101+
PrintError(player, player.PyEnv(), e);
102+
}
78103
}
79104

80105
public static void OnServerCommand(CommandEventArgs args)
81106
{
82107
if (args.Handled)
83108
return;
84109
args.Handled = CheckIronPythonInput(TSPlayer.Server, args.Command);
110+
111+
// Replacing script result inside of console commands
112+
MatchCollection matches;
113+
if (args.Handled)
114+
return;
115+
if (CommandFieldSetter == null)
116+
CommandFieldSetter = args.GetType().GetProperty("Command").GetSetMethod(true);
117+
try
118+
{
119+
if ((matches = Regex.Matches(args.Command, IronPythonConfig.InlineCodeWithScriptRegex)).Count > 0)
120+
CommandFieldSetter.Invoke(args,
121+
new object[] { TSPlayer.Server.PyEnv().ProcessText(args.Command, matches,
122+
null, true, null, new object[] { TSPlayer.Server }) });
123+
if ((matches = Regex.Matches(args.Command, IronPythonConfig.InlineCodeRegex)).Count > 0)
124+
CommandFieldSetter.Invoke(args,
125+
new object[] { TSPlayer.Server.PyEnv().ProcessText(args.Command, matches,
126+
null, false, null, new object[] { TSPlayer.Server }) });
127+
} catch (Exception e)
128+
{
129+
args.Handled = true;
130+
PrintError(TSPlayer.Server, TSPlayer.Server.PyEnv(), e);
131+
}
85132
}
86133

87134
public static bool CheckIronPythonInput(TSPlayer player, string text)
@@ -126,7 +173,7 @@ public static bool InitializeEnvironment(IronPythonEnvironment pyEnv, TSPlayer p
126173
{
127174
try
128175
{
129-
pyEnv.Initialize(player);
176+
pyEnv.Initialize(player, IronPythonConfig.IronPythonPath);
130177
return true;
131178
}
132179
catch (Exception e)

IronPythonPlugin/IronPythonPlugin.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<DefineConstants>TRACE</DefineConstants>
3030
<ErrorReport>prompt</ErrorReport>
3131
<WarningLevel>4</WarningLevel>
32+
<PlatformTarget>x86</PlatformTarget>
3233
</PropertyGroup>
3334
<ItemGroup>
3435
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
@@ -65,9 +66,15 @@
6566
<Name>MyIronPython</Name>
6667
</ProjectReference>
6768
</ItemGroup>
69+
<ItemGroup>
70+
<None Include="app.config" />
71+
</ItemGroup>
6872
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6973
<PropertyGroup>
7074
<PostBuildEvent>if not exist "$(ProjectDir)_BuildResult" mkdir "$(ProjectDir)_BuildResult"
71-
copy /Y "$(TargetDir)$(TargetName).dll" "$(ProjectDir)_BuildResult\$(TargetName).dll</PostBuildEvent>
75+
copy /Y "$(TargetDir)$(TargetName).dll" "$(ProjectDir)_BuildResult\$(TargetName).dll
76+
copy /Y "$(TargetDir)IronPython.dll" "$(ProjectDir)_BuildResult\IronPython.dll
77+
copy /Y "$(TargetDir)Microsoft.Dynamic.dll" "$(ProjectDir)_BuildResult\Microsoft.Dynamic.dll
78+
copy /Y "$(TargetDir)Microsoft.Scripting.dll" "$(ProjectDir)_BuildResult\Microsoft.Scripting.dll</PostBuildEvent>
7279
</PropertyGroup>
7380
</Project>
1.99 MB
Binary file not shown.
1 KB
Binary file not shown.
828 KB
Binary file not shown.
139 KB
Binary file not shown.
7.46 MB
Binary file not shown.
169 KB
Binary file not shown.
3 KB
Binary file not shown.

0 commit comments

Comments
 (0)