1010
1111namespace _7Sharp
1212{
13+ using System . Linq ;
1314 using static ColorConsoleMethods ;
1415 using static Console ;
1516 internal class Program
@@ -21,14 +22,18 @@ internal class Program
2122 public static bool parsed = false ;
2223 public static string input = "" ;
2324 public static bool has_args = false ;
24- public static List < VarInt > ints = new List < VarInt > ( ) ;
25- public static List < VarString > strings = new List < VarString > ( ) ;
25+ public static List < _7sInt > ints = new List < _7sInt > ( ) ;
26+ public static List < _7sString > strings = new List < _7sString > ( ) ;
2627 public static StreamReader srr = null ;
2728 public static List < Assembly > PluginAssemblies = new List < Assembly > ( ) ;
2829 public static int times = 1 ;
2930 public static List < Command > commands = new List < Command > ( ) ;
3031 public static bool echo = true ;
31- static void Main ( string [ ] args )
32+ public static bool RunningEnvCode = false ;
33+ internal static string NoEnvCode = "\0 " ;
34+ public static string [ ] EnvCode = null ;
35+ public static int EnvCodeIndex = 0 ;
36+ static void Main ( string [ ] args )
3237 {
3338 //title
3439 Title = "7Sharp" ;
@@ -70,6 +75,7 @@ static void Main(string[] args)
7075 {
7176 try
7277 {
78+ WriteLineColor ( "Loading plugin " + Path . GetFileName ( path ) , ConsoleColor . Green ) ;
7379 //load dll
7480 Assembly asm = Assembly . LoadFrom ( path ) ;
7581 //load assembly of dll
@@ -79,6 +85,8 @@ static void Main(string[] args)
7985 {
8086 if ( t . IsSubclassOf ( typeof ( Command ) ) )
8187 {
88+ Command instance = ( Command ) Activator . CreateInstance ( t ) ;
89+ WriteLineColor ( "Found command " + t , ConsoleColor . Cyan ) ;
8290 //check for no parameter constructor
8391 if ( t . GetConstructor ( Type . EmptyTypes ) == null )
8492 {
@@ -89,12 +97,19 @@ static void Main(string[] args)
8997 {
9098 throw new PluginIntializationException ( "Plugin " + Path . GetFileName ( path ) + ": Command " + t . Name + " did not have a parse method!" ) ;
9199 }
100+ //check for exisiting call
101+ if ( GetCalls ( ) . Contains ( instance . call ) )
102+ {
103+ throw new PluginIntializationException ( "Plugin " + Path . GetFileName ( path ) + ": Command " + t . Name + " has a conflicting call!" ) ;
104+ }
92105 //create instance
93- commands . Add ( ( Command ) Activator . CreateInstance ( t ) ) ;
106+ commands . Add ( instance ) ;
94107 //woo hoo it works!
95- successes ++ ;
96108 }
97109 }
110+ successes ++ ;
111+ WriteLineColor ( "Successfully registered plugin " + Path . GetFileName ( path ) , ConsoleColor . Green ) ;
112+ WriteLineColor ( "Successfully registered " + successes + " plugin(s)!" , ConsoleColor . Green ) ;
98113 }
99114 catch ( Exception e )
100115 {
@@ -116,27 +131,53 @@ static void Main(string[] args)
116131 UpdateEnvironment ( ) ;
117132 while ( true )
118133 {
119- if ( ! has_args )
134+ if ( ! RunningEnvCode )
120135 {
121- input = ReadLine ( ) ;
122- input_split = input . Split ( ' ' , '\n ' ) ;
123- UpdateEnvironment ( ) ;
124- ParseCommands ( ) ;
136+ if ( ! has_args )
137+ {
138+ input = ReadLine ( ) ;
139+ input_split = input . Split ( ' ' , '\n ' ) ;
140+ UpdateEnvironment ( ) ;
141+ ParseCommands ( ) ;
142+ }
143+ else
144+ {
145+ while ( srr . EndOfStream == false )
146+ {
147+ input = srr . ReadLine ( ) ;
148+ input_split = input . Split ( ' ' , '\n ' ) ;
149+ UpdateEnvironment ( ) ;
150+ ParseCommands ( ) ;
151+ }
152+ srr . Close ( ) ;
153+ UpdateEnvironment ( ) ;
154+ }
125155 }
126156 else
127157 {
128- while ( srr . EndOfStream == false )
158+ while ( EnvCodeIndex < EnvCode . Length )
129159 {
130- input = srr . ReadLine ( ) ;
131- input_split = input . Split ( ' ' , '\n ' ) ;
132- UpdateEnvironment ( ) ;
133160 ParseCommands ( ) ;
161+ EnvCodeIndex ++ ;
162+ UpdateEnvironment ( ) ;
134163 }
135- srr . Close ( ) ;
164+ RunningEnvCode = false ;
165+ EnvCodeIndex = 0 ;
136166 UpdateEnvironment ( ) ;
137167 }
138168 }
139169 }
170+
171+ private static List < string > GetCalls ( )
172+ {
173+ List < string > o = new List < string > ( ) ;
174+ foreach ( Command i in commands )
175+ {
176+ o . Add ( i . call ) ;
177+ }
178+ return o ;
179+ }
180+
140181 private static void ParseCommands ( )
141182 {
142183 foreach ( Command i in commands )
@@ -145,8 +186,12 @@ private static void ParseCommands()
145186 }
146187 }
147188
148- private static void UpdateEnvironment ( )
189+ internal static void UpdateEnvironment ( )
149190 {
191+ if ( RunningEnvCode && EnvCodeIndex < EnvCode . Length )
192+ {
193+ input = EnvCode [ EnvCodeIndex ] ;
194+ }
150195 InternalEnv . commands = commands ;
151196 InternalEnv . echo = echo ;
152197 InternalEnv . exit = exit ;
@@ -160,13 +205,60 @@ private static void UpdateEnvironment()
160205 InternalEnv . strings = strings ;
161206 InternalEnv . times = times ;
162207 InternalEnv . _inputs = _inputs ;
208+ InternalEnv . EnvCode = EnvCode ;
209+ InternalEnv . EnvCodeIndex = EnvCodeIndex ;
210+ InternalEnv . RunningEnvCode = RunningEnvCode ;
163211 _7sEnvironment . Commands = commands ;
164212 _7sEnvironment . Echo = echo ;
165213 _7sEnvironment . Input = input ;
166214 _7sEnvironment . Ints = ints ;
167215 _7sEnvironment . SplitInput = input . Split ( ' ' , '\n ' ) ;
168216 _7sEnvironment . Strings = strings ;
169217 _7sEnvironment . CurrentScriptStream = srr ;
218+ _7sEnvironment . ForceRunningCode = RunningEnvCode ;
219+ _7sEnvironment . CodeBeingForceRun = EnvCode ;
220+ _7sEnvironment . ForceRunningCodeIndex = EnvCodeIndex ;
221+ }
222+ internal static void UpdateEnvironment ( bool force )
223+ {
224+ if ( RunningEnvCode && EnvCodeIndex < EnvCode . Length && ! force )
225+ {
226+ input = EnvCode [ EnvCodeIndex ] ;
227+ }
228+ InternalEnv . commands = commands ;
229+ InternalEnv . echo = echo ;
230+ InternalEnv . exit = exit ;
231+ InternalEnv . has_args = has_args ;
232+ InternalEnv . input = input ;
233+ InternalEnv . input_split = input . Split ( ' ' , '\n ' ) ;
234+ InternalEnv . ints = ints ;
235+ InternalEnv . loop = loop ;
236+ InternalEnv . parsed = parsed ;
237+ InternalEnv . srr = srr ;
238+ InternalEnv . strings = strings ;
239+ InternalEnv . times = times ;
240+ InternalEnv . _inputs = _inputs ;
241+ InternalEnv . EnvCode = EnvCode ;
242+ InternalEnv . EnvCodeIndex = EnvCodeIndex ;
243+ InternalEnv . RunningEnvCode = RunningEnvCode ;
244+ _7sEnvironment . Commands = commands ;
245+ _7sEnvironment . Echo = echo ;
246+ _7sEnvironment . Input = input ;
247+ _7sEnvironment . Ints = ints ;
248+ _7sEnvironment . SplitInput = input . Split ( ' ' , '\n ' ) ;
249+ _7sEnvironment . Strings = strings ;
250+ _7sEnvironment . CurrentScriptStream = srr ;
251+ _7sEnvironment . ForceRunningCode = RunningEnvCode ;
252+ _7sEnvironment . CodeBeingForceRun = EnvCode ;
253+ _7sEnvironment . ForceRunningCodeIndex = EnvCodeIndex ;
254+ }
255+
256+ internal static void UpdateInputFromEnvCode ( )
257+ {
258+ bool last = EnvCodeIndex == EnvCode . Length ;
259+ input = has_args == true ? srr . ReadLine ( ) : ( RunningEnvCode ? ( last ? NoEnvCode : EnvCode [ EnvCodeIndex + 1 ] ) : ReadLine ( ) ) ;
260+ EnvCodeIndex ++ ;
261+ UpdateEnvironment ( ) ;
170262 }
171263 }
172264}
0 commit comments