Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
scriptstack [2025/12/15 02:17] jango [Routines] |
scriptstack [2025/12/15 02:50] (aktuell) jango [Routines] |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| =====Manager===== | =====Manager===== | ||
| - | The scripting system is initialised by creating one or more instances of the ScriptManager class. | + | Das Skriptsystem wird initialisiert, |
| - | Each instance represents a scripting environment where scripts can be loaded and executed. Each | + | |
| - | instance also provides a global variable scope that the scripts can use to share data. | + | |
| - | < | + | < |
| Manager manager = new Manager(); | Manager manager = new Manager(); | ||
| </ | </ | ||
| Zeile 11: | Zeile 9: | ||
| =====Script===== | =====Script===== | ||
| - | Once a script manager is available, scripts can be loaded by creating instances of the Script | + | Sobald ein Manager verfügbar ist, können Skripte durch Erstellen von Instanzen der Klasse "Script" geladen werden. Der Konstruktor des Skriptobjekts benötigt eine Referenz auf den Skriptmanager und einen Namen zur Identifizierung des Skripts. Standardmäßig entspricht dieser Name einem Dateinamen auf der Festplatte. |
| - | The script object' | + | |
| - | the script. By default, this name corresponds to a disk filename. | + | |
| - | < | + | < |
| Script script = new Script(manager, | Script script = new Script(manager, | ||
| </ | </ | ||
| - | The script object' | + | Der Konstruktor des Skriptobjekts kompiliert den Skriptquellcode automatisch in Scriptstack-Bytecode. Standardmäßig wird der generierte Bytecode mithilfe eines "Peephole" |
| - | By default, the generated byte code is optimised using a "peephole" | + | |
| - | instructions are added to facilitate mapping of the generated code with the original source. These | + | |
| - | settings may be controlled by setting the ScriptManager' | + | |
| - | OptimiseCode. | + | |
| =====Interpreter===== | =====Interpreter===== | ||
| - | A script object represents only the programming instructions contained within and not its | + | Ein Skriptobjekt repräsentiert lediglich Programmanweisungen (sog Token), nicht aber seinen Ausführungszustand. Um das Skript auszuführen, muss eine Instanz der Klasse " |
| - | execution state. To execute the script, an instance of the ScriptContext class must be created. The | + | |
| - | class' constructor requires a reference to the script to be executed or a reference to one of the | + | |
| - | script' | + | |
| - | context provides execution control, as well as access to the execution state in terms of the variables | + | |
| - | defined during execution, the next statement to be executed and so on. The ScriptContext class | + | |
| - | represents a running instance of a script. Hence, multiple instances of the same script object can be | + | |
| - | executed within the same script manager by creating multiple script contexts referencing the same | + | |
| - | script. | + | |
| - | < | + | < |
| // create a context for the script' | // create a context for the script' | ||
| Interpreter interpreter = new Interpreter(script); | Interpreter interpreter = new Interpreter(script); | ||
| Zeile 45: | Zeile 29: | ||
| // create a context for one of the script' | // create a context for one of the script' | ||
| - | Function scriptFunction = script.Functions[" | + | Function scriptFunction = script.Functions[" |
| Interpreter interpreter = new Interpreter(scriptFunction); | Interpreter interpreter = new Interpreter(scriptFunction); | ||
| </ | </ | ||
| - | The script context object allows execution of the referenced script via the three variants of its | + | Das Interpreter-Objekt ermöglicht die Ausführung des referenzierten Skripts über drei Varianten seiner " |
| - | Execute method. These are: execution of scripts for an indefinite amount of time; execution of | + | |
| - | scripts for a given time interval or execution of scripts for up to a maximum number of executed | + | |
| - | statements. | + | |
| - | The first method variant allows the referenced script function to execute indefinitely or until the | + | |
| - | end of the function is reached. If the script contains an infinite loop, this method will block | + | |
| - | indefinitely unless an interrupt is generated. The Execute method returns the total number of | + | |
| - | statements executed since its invocation. | + | |
| - | < | + | < |
| // execute indefinitely, | // execute indefinitely, | ||
| interpreter.Interpret(); | interpreter.Interpret(); | ||
| </ | </ | ||
| - | The second variant of the Execute method allows the script context to execute up to a given | + | Die zweite Variante der " |
| - | maximum number of statements. The script context may break out of execution before the | + | |
| - | maximum is reached if there are no more statements to process or if an interrupt is generated. | + | |
| - | < | + | < |
| // execute up to a maximumum of 10 statements | // execute up to a maximumum of 10 statements | ||
| interpreter.Interpret(10); | interpreter.Interpret(10); | ||
| </ | </ | ||
| - | The third variant of the Execute method accepts a TimeSpan | + | Die dritte Variante der " |
| - | allowed for script execution. The method may break out of execution earlier than the given interval | + | |
| - | if there are no more statements to process or an interrupt is generated. Given a script with a good | + | |
| - | balance of different statements, a possible use of this method is to determine the speed of the | + | |
| - | scripting system on the target environment | + | |
| - | < | + | < |
| // execute for up to 10 milliseconds | // execute for up to 10 milliseconds | ||
| TimeSpan tsInterval = new TimeSpan(0, 0, 0, 0, 10); | TimeSpan tsInterval = new TimeSpan(0, 0, 0, 0, 10); | ||
| Zeile 84: | Zeile 55: | ||
| </ | </ | ||
| - | The second and third variants of Execute may be used to implement a virtual multi-threaded | ||
| - | scripting environment. Global variables may be used as semaphores to synchronise concurrently | ||
| - | running scripts. | ||
| - | A script context will normally execute its referenced script function indefinitely, for a given time | + | Ein Interpreter führt seine referenzierte Skriptfunktion normalerweise unbegrenzt für ein bestimmtes Zeitintervall aus, bis eine maximale Anzahl von Anweisungen ausgeführt wurde oder keine weiteren Anweisungen mehr zu verarbeiten sind. In manchen Fällen ist es jedoch wünschenswert, die Ausführung vorzeitig abzubrechen, beispielsweise um die Kontrolle an den Host zurückzugeben, |
| - | interval, until a given maximum number of statements are executed or until there are no more | + | |
| - | statements to process. In some cases it is desirable to break execution prematurely, such as to | + | |
| - | return control to the host when specific statements are executed, or because a script is too | + | |
| - | computationally intensive to execute | + | |
| Conscript provides two ways for generating script interrupts: | Conscript provides two ways for generating script interrupts: | ||
| Zeile 100: | Zeile 65: | ||
| =====Scanner===== | =====Scanner===== | ||
| - | To allow for loading of scripts from other sources -- such as an archive file, network or database -- | + | Um das Laden von Skripten aus anderen Quellen wie Archivdateien, dem Netzwerk oder Datenbanken zu ermöglichen, |
| - | a custom script loader class can be developed and bound to the script manager to be used for | + | |
| - | loading the script. The script loader class may be any class that implements the ScriptLoader | + | |
| - | interface. The loader is used to retrieve the script specified in the Script class constructor and also | + | |
| - | any additional include scripts defined within the original script. | + | |
| - | < | + | < |
| // custom script loader class | // custom script loader class | ||
| public class MyScanner : Scanner | public class MyScanner : Scanner | ||
| Zeile 124: | Zeile 85: | ||
| =====Variablen===== | =====Variablen===== | ||
| - | One approach for allowing a script to communicate with or control the host application entails the | + | Eine Möglichkeit, |
| - | application polling the local variables of the associated script context and global variables of the | + | |
| - | associated script manager. It does this by querying the LocalDictionary property of the | + | |
| - | ScriptContext object, the ScriptDictionary property of the Script | + | |
| - | GlobalDicitonary property of the ScriptManager object. | + | |
| - | < | + | < |
| // get value of local variable | // get value of local variable | ||
| int i = (int)interpreter.LocalMemory[" | int i = (int)interpreter.LocalMemory[" | ||
| Zeile 155: | Zeile 112: | ||
| <code csharp> | <code csharp> | ||
| // define route | // define route | ||
| - | Routine routine = new Routine( | + | Routine routine = new Routine((Type)null, |
| - | (Type)null, " | + | |
| </ | </ | ||
| Zeile 163: | Zeile 119: | ||
| List< | List< | ||
| - | < | + | < |
| // prepare parameter type list | // prepare parameter type list | ||
| List< | List< | ||
| Zeile 172: | Zeile 128: | ||
| listParameterTypes.Add(typeof(ArrayList)); | listParameterTypes.Add(typeof(ArrayList)); | ||
| - | // define | + | // define |
| - | Routine routine = new Routine( | + | Routine routine = new Routine((Type)null, |
| - | (Type)null, " | + | |
| </ | </ | ||
| Zeile 183: | Zeile 138: | ||
| prototype without a handler: | prototype without a handler: | ||
| - | < | + | < |
| // register host function | // register host function | ||
| manager.Register(routine); | manager.Register(routine); | ||
| Zeile 195: | Zeile 150: | ||
| A.I. scripts in a game: | A.I. scripts in a game: | ||
| - | < | + | < |
| public class Program : Host | public class Program : Host | ||
| { | { | ||
| - | // class implementation | + | |
| - | | + | private Interpreter interpreter; |
| - | | + | |
| - | { | + | |
| - | | + | { |
| - | | + | interpreter = new Interpreter(script); |
| - | } | + | interpreter.Handler = this; |
| - | | + | } |
| - | { | + | |
| - | if (functionName == " | + | |
| - | { | + | { |
| - | | + | if (functionName == " |
| - | | + | { |
| - | | + | string str = (int)parameters[0]; |
| - | } | + | Console.WriteLine(str); |
| - | | + | return; |
| - | } | + | } |
| + | return null; | ||
| + | } | ||
| + | | ||
| + | } | ||
| </ | </ | ||
| Zeile 225: | Zeile 184: | ||
| Alternatively, | Alternatively, | ||
| - | < | + | < |
| // register global Sine function | // register global Sine function | ||
| - | HostFunctionPrototype hostFunctionPrototype | + | Routine routine |
| - | typeof(float), | + | manager.Register(routine, printHandler); |
| - | m_scriptManager.RegisterHostFunction(hostFunctionPrototype, trigHandler); | + | |
| // register global Cosine function | // register global Cosine function | ||
| - | HostFunctionPrototype hostFunctionPrototype | + | routine |
| - | | + | manager.Register(routine, readHandler); |
| - | m_scriptManager.RegisterHostFunction(hostFunctionPrototype, | + | |
| - | // register global Tangent function | + | |
| - | HostFunctionPrototype hostFunctionPrototype = new HostFunctionPrototype( | + | |
| - | typeof(float), | + | |
| - | m_scriptManager.RegisterHostFunction(hostFunctionPrototype, trigHandler); | + | |
| </ | </ | ||
| Zeile 254: | Zeile 208: | ||
| illustrates an alternative implementation of the earlier trigonometry functions example: | illustrates an alternative implementation of the earlier trigonometry functions example: | ||
| - | < | + | < |
| public class TrigonometryModule | public class TrigonometryModule | ||
| : HostModule | : HostModule | ||
| Zeile 313: | Zeile 267: | ||
| Host module registration is very similar to individual function registration: | Host module registration is very similar to individual function registration: | ||
| - | < | + | < |
| // create module instance | // create module instance | ||
| TrigonometryModule trigonometryModule = new TrigonometryModule(); | TrigonometryModule trigonometryModule = new TrigonometryModule(); | ||