Automatic Script Reloading

Scripts and code that enhance the gameplay functionality of the engine.
3 posts Page 1 of 1
NeonTiger
Posts: 9
Joined: Sun Aug 02, 2015 4:50 am
by NeonTiger » Tue Jan 29, 2019 5:42 am
This is a small update of an old resource that was on garage games.
http://www.garagegames.com/community/blogs/view/21262

I added the ability to mark files that you are working in to keeps systems and things like the editor from freaking out and breaking.

example:
exec("scripts/server/stuff.cs");
to
rexec("scripts/server/stuff.cs"); // this file will now auto exec after saving

////////////////////////////////////////////////////////////////////////////
First open up volume.h

Find these lines
template <class T,class U>
inline bool AddChangeNotification( const Path &path, T obj, U func )

Then add code above them
template <class U>
inline bool AddChangeNotification(const Path &path, U func)
{
	FileSystemRef fs = GetFileSystem(path);
	if (!fs || !fs->getChangeNotifier())
		return false;

	FileSystemChangeNotifier::ChangeDelegate dlg(func);
	return fs->getChangeNotifier()->addNotification(path, dlg);
}

Now open up console.cpp
Above this function bool executeFile(const char* fileName, bool noCalls, bool journalScript)

Add
void reExec(const Torque::Path &path);
Now change bool executeFile(const char* fileName, bool noCalls, bool journalScript)

To
bool executeFile(const char* fileName, bool noCalls, bool journalScript, bool rexec)
In the same function change this line
if( (dsoPath && *dsoPath == 0) || (prefsPath && prefsPath[ 0 ] && dStrnicmp(scriptFileName, prefsPath, dStrlen(prefsPath)) == 0) )

To
if( (dsoPath && *dsoPath == 0) || dStrstr(scriptFileName, "prefs.cs")) //(prefsPath && prefsPath[ 0 ] && dStrnicmp(scriptFileName, prefsPath, dStrlen(prefsPath)) == 0) )
Still in the same function add this after the last #endif
if (compiled && rexec) 
 {
      Torque::Path path(scriptFileName);
      Torque::FS::AddChangeNotification(path, &reExec);
 }
After the bool executeFile function add
void reExec(const Torque::Path &path)
{
	//we know this is a file that exists, and a .cs file, so we can skip a lot of checks...  
	CodeBlock *newCodeBlock = new CodeBlock();
	StringTableEntry name = StringTable->insert(path.getFullPath().c_str());

	void *data;
	U32 dataSize = 0;
	Torque::FS::ReadFile(name, data, dataSize, true);

	newCodeBlock->compileExec(name, (char*)data, false, 0);

	delete[] data;
}
Now open up console.h and change
bool executeFile(const char* fileName, bool noCalls, bool journalScript);

To
bool executeFile(const char* fileName, bool noCalls, bool journalScript,bool rexec);
Lastly open up consoleFunctioncs.cpp and replace
DefineEngineFunction( exec, bool, ( const char* fileName, bool noCalls, bool journalScript ), ( false, false ),
   "Execute the given script file.\n"
   "@param fileName Path to the file to execute\n"
   "@param noCalls Deprecated\n"
   "@param journalScript Deprecated\n"
   "@return True if the script was successfully executed, false if not.\n\n"
   "@tsexample\n"
      "// Execute the init.cs script file found in the same directory as the current script file.\n"
      "exec( \"./init.cs\" );\n"
   "@endtsexample\n\n"
   "@see compile\n"
   "@see eval\n"
   "@ingroup Scripting" )
{
   return Con::executeFile(fileName, noCalls, journalScript);
}
With this
DefineEngineFunction( exec, bool, ( const char* fileName, bool noCalls, bool journalScript ), ( false, false ),
   "Execute the given script file.\n"
   "@param fileName Path to the file to execute\n"
   "@param noCalls Deprecated\n"
   "@param journalScript Deprecated\n"
   "@return True if the script was successfully executed, false if not.\n\n"
   "@tsexample\n"
      "// Execute the init.cs script file found in the same directory as the current script file.\n"
      "exec( \"./init.cs\" );\n"
   "@endtsexample\n\n"
   "@see compile\n"
   "@see eval\n"
   "@ingroup Scripting" )
{
   return Con::executeFile(fileName, noCalls, journalScript, false);
}
DefineEngineFunction(rexec, bool, (const char* fileName, bool noCalls, bool journalScript), (false, false),
	"Execute the given script file. As well as re-execute said file automatically after saving\n")
{
   return Con::executeFile(fileName, noCalls, journalScript, true);
}
Jason Campbell
Posts: 321
Joined: Fri Feb 13, 2015 2:51 am
 
by Jason Campbell » Tue Jan 29, 2019 7:00 am
I remember this one! Very useful. Thanks.
Duion
Posts: 1333
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Tue Jan 29, 2019 7:19 am
@
User avatar
NeonTiger

This is something I was looking for as well, I would suggest you to make a pull request into the main repo with this, so it gets added into the engine as a new feature, so not everyone has to maintain their own fork.
3 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest