55using System . Linq ;
66using System . Reflection ;
77using System . Text ;
8+ using System . Text . RegularExpressions ;
89using System . Threading . Tasks ;
910using Terraria ;
1011using 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 )
0 commit comments