1 module as.mod; 2 import as.def; 3 import as.engine; 4 import as.func; 5 import std..string; 6 import std.exception; 7 8 enum ModuleCreateFlags : asEGMFlags { 9 /** 10 Only load the module if it exists 11 */ 12 OnlyIfExists = asEGMFlags.asGM_ONLY_IF_EXISTS, 13 14 /** 15 Try to create the module if it doesn't exist 16 */ 17 CreateIfNotExists = asEGMFlags.asGM_CREATE_IF_NOT_EXISTS, 18 19 /** 20 Always create the module, overwriting old modules with the same name 21 */ 22 AlwaysCreate = asEGMFlags.asGM_ALWAYS_CREATE 23 } 24 25 class Module { 26 private: 27 ScriptEngine engine; 28 29 ~this() { 30 this.engine = null; 31 } 32 33 package(as): 34 asIScriptModule* mod; 35 36 this(ScriptEngine engine, asIScriptModule* mod) { 37 this.engine = engine; 38 this.mod = mod; 39 } 40 41 public: 42 43 /** 44 Gets the engine this module belongs to 45 */ 46 ScriptEngine getEngine() { 47 return engine; 48 } 49 50 /** 51 Sets the name of the module 52 */ 53 void setName(string name) { 54 asModule_SetName(mod, name.toStringz); 55 } 56 57 /** 58 Gets the name of the module 59 */ 60 string getName() { 61 return cast(string)asModule_GetName(mod).fromStringz; 62 } 63 64 /** 65 Discards the module, removing it from the engine 66 */ 67 void discard() { 68 asModule_Discard(mod); 69 } 70 71 /** 72 Adds a script section to the module 73 */ 74 void addScriptSection(string name, string code, int lineOffset = 0) { 75 int err = asModule_AddScriptSection(mod, name.toStringz, code.toStringz, code.length, lineOffset); 76 } 77 78 /** 79 Builds scripts 80 */ 81 void build() { 82 int err = asModule_Build(mod); 83 enforce(err != asERetCodes.asINVALID_CONFIGURATION, "Invalid configuration"); 84 enforce(err != asERetCodes.asERROR, "Failed to compile script"); 85 enforce(err != asERetCodes.asBUILD_IN_PROGRESS, "Another thread is currently building"); 86 enforce(err != asERetCodes.asINIT_GLOBAL_VARS_FAILED, "Unable to initialize at least one global variable"); 87 enforce(err != asERetCodes.asNOT_SUPPORTED, "Compiler support disabled"); 88 enforce(err != asERetCodes.asMODULE_IS_IN_USE, "Code is in use and can't be removed"); 89 } 90 91 /** 92 Gets function by declaration 93 */ 94 Function getFunctionByDecl(string decl) { 95 return new Function(engine, asModule_GetFunctionByDecl(mod, decl.toStringz)); 96 } 97 98 /** 99 Gets function by name 100 */ 101 Function getFunctionByName(string name) { 102 return new Function(engine, asModule_GetFunctionByName(mod, name.toStringz)); 103 } 104 }