using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using System.Reflection.Emit; using CLRScriptFramework; using NWScript; using NWScript.ManagedInterfaceLayer.NWScriptManagedInterface; using NWEffect = NWScript.NWScriptEngineStructure0; using NWEvent = NWScript.NWScriptEngineStructure1; using NWLocation = NWScript.NWScriptEngineStructure2; using NWTalent = NWScript.NWScriptEngineStructure3; using NWItemProperty = NWScript.NWScriptEngineStructure4; namespace CLRScript { public partial class nwn2ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation { /// /// This helper function saves the value of all 'globals' to an object /// array for use with storing a saved state (e.g. a script situation). /// /// A list of global variables, to be restored by a future /// call to LoadScriptGlobals. public object[] SaveScriptGlobals() { object[] Globals = new object[GlobalFields.Count]; int i = 0; foreach (FieldInfo Field in GlobalFields) { Globals[i] = Field.GetValue(this); i += 1; } return Globals; } /// /// This routine is the main entry point symbol for the script. It is /// invoked when the packaged script is executed. /// /// Supplies the "OBJECT_SELF" object id of /// the object that the script is being executed over. This may be the /// invalid object id (Script.OBJECT_INVALID) if no object is /// associated with the execute script request. /// Supplies the parameter values for /// the script. If the type deriving from IGeneratedScriptProgram /// declares a ScriptParameterTypes public field, then parameters may /// be passed in via this array (in which case the parameter types /// have already been converted and validated). Otherwise, no /// arguments are provided. /// Supplies the requested default /// return code to use if the script is a "main"-style script that /// would not conventionally return a value. /// public Int32 ExecuteScript([In] UInt32 ObjectSelf, [In] object[] ScriptParameters, [In] Int32 DefaultReturnCode) { UInt32 OldOBJECT_SELF = ObjectSelf; int ReturnCode; try { OBJECT_SELF = ObjectSelf; ReturnCode = ScriptMain(ScriptParameters, DefaultReturnCode); } finally { OBJECT_SELF = OldOBJECT_SELF; } return ReturnCode; } /// /// This routine is invoked when a script situation created by the /// script is resumed for execution (for example, a DelayCommand /// continuation). Its purpose is to perform the appropriate resume /// action for this continuation. /// /// Note that a script situation may be resumed after the original /// script object has been deleted, or even after the host process has /// been exited and restarted (in the save of a saved game that has /// been loaded). /// /// Supplies the ScriptSituationId that /// was provided to the initial StoreState request, which is intended /// to uniquely identify the site within which the script situation was /// created. /// Supplies an array of local variables that were /// provided to the initial StoreState request. The locals may only /// include standard NWScript types (Int32, UInt32, Single, and engine /// structures). /// Supplies the "OBJECT_SELF" object id of /// the object that the script is being executed over. This may be the /// invalid object id (Script.OBJECT_INVALID) if no object is /// associated with the execute script request. public void ExecuteScriptSituation([In] UInt32 ScriptSituationId, [In] object[] Locals, [In] UInt32 ObjectSelf) { // // Call the helper function. // DispatchExecuteScriptSituation(ScriptSituationId, Locals, ObjectSelf); } /// /// This routine is invoked when the script context is being cloned for /// the creation of a script situation. Its purpose is to create a new /// SampleManagedNWScript object and transfer any state desired to the /// new script object. /// /// The cloned script object is returned. public IGeneratedScriptProgram CloneScriptProgram() { return (IGeneratedScriptProgram)(new nwn2ai_onmoduleload(this)); } /// /// This routine is invoked when the script context was restored from /// the interopability script stack for a script situation. Its /// purpose is to restore any 'global' (i.e. member variable) state /// that was passed to the initial StoreState request. The globals may /// only include standard NWScript types (Int32, UInt32, Single, and /// engine structures). /// /// Supplies an array of global variables that /// were provided to the initial StoreState request. public void LoadScriptGlobals([In] object[] Globals) { // // Restore all [NWScriptGlobal] attributed globals from the global // list. // int i = 0; foreach (FieldInfo Field in GlobalFields) { Field.SetValue(this, Globals[i]); i += 1; } } } }