| 115 | } |
| 116 | |
| 117 | CScriptMgr::SController *CScriptMgr::GetControllerScript(const string &script) |
| 118 | { |
| 119 | int r; |
| 120 | |
| 121 | // Find the cached controller |
| 122 | for( unsigned int n = 0; n < controllers.size(); n++ ) |
| 123 | { |
| 124 | if( controllers[n]->module == script ) |
| 125 | return controllers[n]; |
| 126 | } |
| 127 | |
| 128 | // No controller, check if the script has already been loaded |
| 129 | asIScriptModule *mod = engine->GetModule(script.c_str(), asGM_ONLY_IF_EXISTS); |
| 130 | if( mod ) |
| 131 | { |
| 132 | // We've already attempted loading the script before, but there is no controller |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | // Compile the script into the module |
| 137 | CScriptBuilder builder; |
| 138 | r = builder.StartNewModule(engine, script.c_str()); |
| 139 | if( r < 0 ) |
| 140 | return 0; |
| 141 | |
| 142 | // If the script file doesn't exist, then there is no script controller for this type |
| 143 | FILE *f; |
| 144 | if( (f = fopen((script + ".as").c_str(), "r")) == 0 ) |
| 145 | return 0; |
| 146 | fclose(f); |
| 147 | |
| 148 | // Let the builder load the script, and do the necessary pre-processing (include files, etc) |
| 149 | r = builder.AddSectionFromFile((script + ".as").c_str()); |
| 150 | if( r < 0 ) |
| 151 | return 0; |
| 152 | |
| 153 | r = builder.BuildModule(); |
| 154 | if( r < 0 ) |
| 155 | return 0; |
| 156 | |
| 157 | // Cache the functions and methods that will be used |
| 158 | SController *ctrl = new SController; |
| 159 | controllers.push_back(ctrl); |
| 160 | ctrl->module = script; |
| 161 | |
| 162 | // Find the class that implements the IController interface |
| 163 | mod = engine->GetModule(script.c_str(), asGM_ONLY_IF_EXISTS); |
| 164 | asITypeInfo *type = 0; |
| 165 | int tc = mod->GetObjectTypeCount(); |
| 166 | for( int n = 0; n < tc; n++ ) |
| 167 | { |
| 168 | bool found = false; |
| 169 | type = mod->GetObjectTypeByIndex(n); |
| 170 | int ic = type->GetInterfaceCount(); |
| 171 | for( int i = 0; i < ic; i++ ) |
| 172 | { |
| 173 | if( strcmp(type->GetInterface(i)->GetName(), "IController") == 0 ) |
| 174 | { |
nothing calls this directly
no test coverage detected