@@ -61,6 +61,8 @@ internal static void AddScript(BaseScript script)
6161 {
6262 if ( ! ms_definedScripts . Contains ( script ) )
6363 {
64+ script . InitializeOnAdd ( ) ;
65+
6466 ms_definedScripts . Add ( script ) ;
6567 }
6668 }
@@ -104,169 +106,7 @@ private static Assembly CreateAssemblyInternal(string assemblyFile, byte[] assem
104106
105107 Debug . WriteLine ( "Instantiated instance of script {0}." , type . FullName ) ;
106108
107- var allMethods = derivedScript . GetType ( ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) ;
108-
109- IEnumerable < MethodInfo > GetMethods ( Type t )
110- {
111- return allMethods . Where ( m => m . GetCustomAttributes ( t , false ) . Length > 0 ) ;
112- }
113-
114- // register all Tick decorators
115- try
116- {
117- foreach ( var method in GetMethods ( typeof ( TickAttribute ) ) )
118- {
119- Debug . WriteLine ( "Registering Tick for attributed method {0}" , method . Name ) ;
120-
121- if ( method . IsStatic )
122- derivedScript . RegisterTick ( ( Func < Task > ) Delegate . CreateDelegate ( typeof ( Func < Task > ) , method ) ) ;
123- else
124- derivedScript . RegisterTick ( ( Func < Task > ) Delegate . CreateDelegate ( typeof ( Func < Task > ) , derivedScript , method . Name ) ) ;
125- }
126- }
127- catch ( Exception e )
128- {
129- Debug . WriteLine ( "Registering Tick failed: {0}" , e . ToString ( ) ) ;
130- }
131-
132- // register all EventHandler decorators
133- try
134- {
135- foreach ( var method in GetMethods ( typeof ( EventHandlerAttribute ) ) )
136- {
137- var parameters = method . GetParameters ( ) . Select ( p => p . ParameterType ) . ToArray ( ) ;
138- var actionType = Expression . GetDelegateType ( parameters . Concat ( new [ ] { typeof ( void ) } ) . ToArray ( ) ) ;
139- var attribute = method . GetCustomAttribute < EventHandlerAttribute > ( ) ;
140-
141- Debug . WriteLine ( "Registering EventHandler {2} for attributed method {0}, with parameters {1}" , method . Name , String . Join ( ", " , parameters . Select ( p => p . GetType ( ) . ToString ( ) ) ) , attribute . Name ) ;
142-
143- if ( method . IsStatic )
144- derivedScript . RegisterEventHandler ( attribute . Name , Delegate . CreateDelegate ( actionType , method ) ) ;
145- else
146- derivedScript . RegisterEventHandler ( attribute . Name , Delegate . CreateDelegate ( actionType , derivedScript , method . Name ) ) ;
147- }
148- }
149- catch ( Exception e )
150- {
151- Debug . WriteLine ( "Registering EventHandler failed: {0}" , e . ToString ( ) ) ;
152- }
153-
154- // register all commands
155- try
156- {
157- foreach ( var method in GetMethods ( typeof ( CommandAttribute ) ) )
158- {
159- var attribute = method . GetCustomAttribute < CommandAttribute > ( ) ;
160- var parameters = method . GetParameters ( ) ;
161-
162- Debug . WriteLine ( "Registering command {0}" , attribute . Command ) ;
163-
164- // no params, trigger only
165- if ( parameters . Length == 0 )
166- {
167- if ( method . IsStatic )
168- {
169- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
170- {
171- method . Invoke ( null , null ) ;
172- } ) , attribute . Restricted ) ;
173- }
174- else
175- {
176- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
177- {
178- method . Invoke ( derivedScript , null ) ;
179- } ) , attribute . Restricted ) ;
180- }
181- }
182- // Player
183- else if ( parameters . Any ( p => p . ParameterType == typeof ( Player ) ) && parameters . Length == 1 )
184- {
185- #if IS_FXSERVER
186- if ( method . IsStatic )
187- {
188- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
189- {
190- method . Invoke ( null , new object [ ] { new Player ( source . ToString ( ) ) } ) ;
191- } ) , attribute . Restricted ) ;
192- }
193- else
194- {
195- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
196- {
197- method . Invoke ( derivedScript , new object [ ] { new Player ( source . ToString ( ) ) } ) ;
198- } ) , attribute . Restricted ) ;
199- }
200- #else
201- Debug . WriteLine ( "Client commands with parameter type Player not supported" ) ;
202- #endif
203- }
204- // string[]
205- else if ( parameters . Length == 1 )
206- {
207- if ( method . IsStatic )
208- {
209- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
210- {
211- method . Invoke ( null , new object [ ] { args . Select ( a => ( string ) a ) . ToArray ( ) } ) ;
212- } ) , attribute . Restricted ) ;
213- }
214- else
215- {
216- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
217- {
218- method . Invoke ( derivedScript , new object [ ] { args . Select ( a => ( string ) a ) . ToArray ( ) } ) ;
219- } ) , attribute . Restricted ) ;
220- }
221- }
222- // Player, string[]
223- else if ( parameters . Any ( p => p . ParameterType == typeof ( Player ) ) && parameters . Length == 2 )
224- {
225- #if IS_FXSERVER
226- if ( method . IsStatic )
227- {
228- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
229- {
230- method . Invoke ( null , new object [ ] { new Player ( source . ToString ( ) ) , args . Select ( a => ( string ) a ) . ToArray ( ) } ) ;
231- } ) , attribute . Restricted ) ;
232- }
233- else
234- {
235- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
236- {
237- method . Invoke ( derivedScript , new object [ ] { new Player ( source . ToString ( ) ) , args . Select ( a => ( string ) a ) . ToArray ( ) } ) ;
238- } ) , attribute . Restricted ) ;
239- }
240- #else
241- Debug . WriteLine ( "Client commands with parameter type Player not supported" ) ;
242- #endif
243- }
244- // legacy --> int, List<object>, string
245- else
246- {
247- if ( method . IsStatic )
248- {
249- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
250- {
251- method . Invoke ( null , new object [ ] { source , args , rawCommand } ) ;
252- } ) , attribute . Restricted ) ;
253- }
254- else
255- {
256- Native . API . RegisterCommand ( attribute . Command , new Action < int , List < object > , string > ( ( source , args , rawCommand ) =>
257- {
258- method . Invoke ( derivedScript , new object [ ] { source , args , rawCommand } ) ;
259- } ) , attribute . Restricted ) ;
260- }
261- }
262- }
263- }
264- catch ( Exception e )
265- {
266- Debug . WriteLine ( "Registering command failed: {0}" , e . ToString ( ) ) ;
267- }
268-
269- ms_definedScripts . Add ( derivedScript ) ;
109+ AddScript ( derivedScript ) ;
270110 }
271111 catch ( Exception e )
272112 {
0 commit comments