mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
Fixed the behavior of game_loaded script function
(always returned 1 when called from normal scripts)
- Revert the changes to ScriptExtender.cpp in commit 9149df1, and use
unordered_set instead of vector for caching script program pointers.
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
//#include <unordered_set>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <stack>
|
||||
|
||||
@@ -72,10 +72,9 @@ struct GlobalScript {
|
||||
int count;
|
||||
int repeat;
|
||||
int mode; // 0 - local map loop, 1 - input loop, 2 - world map loop, 3 - local and world map loops
|
||||
int isLoading; // used for the return value of the game_loaded function
|
||||
|
||||
//GlobalScript() {}
|
||||
GlobalScript(ScriptProgram script) : prog(script), startProc(-1), count(0), repeat(0), mode(0), isLoading(1) {}
|
||||
GlobalScript(ScriptProgram script) : prog(script), startProc(-1), count(0), repeat(0), mode(0) {}
|
||||
};
|
||||
|
||||
struct ExportedVar {
|
||||
@@ -115,6 +114,7 @@ static std::list<TimedEvent> timerEventScripts;
|
||||
static std::vector<std::string> globalScriptFilesList;
|
||||
|
||||
static std::vector<GlobalScript> globalScripts;
|
||||
static std::unordered_set<fo::Program*> checkedScripts;
|
||||
|
||||
// a map of all sfall programs (global and hook scripts) by thier scriptPtr
|
||||
typedef std::unordered_map<fo::Program*, ScriptProgram> SfallProgsMap;
|
||||
@@ -675,6 +675,8 @@ static void PrepareGlobalScriptsList() {
|
||||
}
|
||||
fo::func::db_free_file_list(&filenames, 0);
|
||||
globalScripts.reserve(globalScriptFilesList.size());
|
||||
float bCount = globalScriptFilesList.size() / checkedScripts.max_load_factor();
|
||||
checkedScripts.rehash(static_cast<size_t>(bCount) + (bCount > static_cast<size_t>(bCount) ? 1 : 0)); // ceil
|
||||
}
|
||||
|
||||
// this runs before the game was loaded/started
|
||||
@@ -697,20 +699,10 @@ static struct {
|
||||
} lastProgram = {nullptr};
|
||||
|
||||
int __stdcall ScriptExtender::ScriptHasLoaded(fo::Program* script) {
|
||||
if (lastProgram.script == script) { // fast check
|
||||
return globalScripts[lastProgram.index].isLoading;
|
||||
if (checkedScripts.find(script) != checkedScripts.end()) {
|
||||
return 0; // has already been called from the script
|
||||
}
|
||||
for (size_t i = 0; i < globalScripts.size(); i++) {
|
||||
if (globalScripts[i].prog.ptr == script) {
|
||||
int loaded = globalScripts[i].isLoading;
|
||||
if (loaded) globalScripts[i].isLoading = 0;
|
||||
|
||||
lastProgram.script = script;
|
||||
lastProgram.index = i;
|
||||
return loaded;
|
||||
}
|
||||
}
|
||||
// error: global script not found
|
||||
checkedScripts.insert(script);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -718,9 +710,9 @@ int __stdcall ScriptExtender::ScriptHasLoaded(fo::Program* script) {
|
||||
static void ClearGlobalScripts() {
|
||||
devlog_f("\nReset global scripts.", DL_MAIN);
|
||||
isGameReset = true;
|
||||
lastProgram.script = nullptr;
|
||||
sfallProgsMap.clear();
|
||||
globalScripts.clear();
|
||||
checkedScripts.clear();
|
||||
selfOverrideMap.clear();
|
||||
globalExportedVars.clear();
|
||||
timerEventScripts.clear();
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
//#define WIN32_LEAN_AND_MEAN
|
||||
#define NOCRYPT
|
||||
|
||||
Reference in New Issue
Block a user