Jump to content

Automatic Script Reloading


NeonTiger

Recommended Posts

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

inline bool AddChangeNotification( const Path &path, T obj, U func )


Then add code above them

template 
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);
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...