Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Created March 30, 2012 15:52
Show Gist options
  • Save ruby0x1/2252403 to your computer and use it in GitHub Desktop.
Save ruby0x1/2252403 to your computer and use it in GitHub Desktop.
An example of the new script binding in Laboratory2D (source)
/*
Copyright (C) 2011 by Underscore Discovery, Sven "FuzzYspo0N" Bergstrom ( http://underscorediscovery.com )
For more license information, please read the following
http://laboratory2D.com/#/license
The license is MIT based, the MIT license is appended to the file.
*/
//Required
#include "Scripted/Common/scriptCommon.h"
//Required
#include scriptIncludes;
//Local Header file
#include "Scripted/Core/scriptCore.h"
namespace Core {
namespace Scripted {
//! core.echo - Print information to the log, the debug console and stdout.
//Helper functions for core.echo and variants
void _doLog( const std::string &_str, Types::eCoreDebugLevelType _type = Types::eDEBUG_MESSAGE, bool _newline = true, bool _tofile = true ) {
CORE->debug->log( _str, _type, _newline, _tofile );
}
/*!
\bUsage : core.echo([(any)value]);
\param value (any) Optional, the value to log.
\return The string that was actually printed.
*/
scriptReturn echo( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//The value we will log
#ifdef LAB_USE_UNICODE
std::wstring value(L"");
#else
std::string value("");
#endif
// core.echo needs no parameters,
// but usually they do specify.
if( scriptArg(1) ) {
#ifdef LAB_USE_UNICODE
scriptArgStringW( 1, value );
#else
scriptArgString( 1, value );
#endif
}
//Log the requested value.
_doLog( value );
//return the string we logged.
return scriptReturnUndefined;
} //echo
//! - .
/*!
\bUsage : ;
\param .
\return .
*/
//execute a javascript file in the current context
scriptReturn exec( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//Fetch the values we require.
std::string filename;
//The source is actually optional.
//The filename just helps with logging where an error occurred.
std::string source;
//This flag allows us to execute code and get the results to script side.
bool executeSourceAndReturnResults = false;
//Fetch the file name to execute, or associate with the code.
scriptArgString( 1, filename );
if( scriptArg(2) ) {
//Fetch the source
scriptArgString( 2, source );
//If they specify source to execute, we return the results.
executeSourceAndReturnResults = true;
}
if( executeSourceAndReturnResults ) {
//We can return the result immediately
return scriptReturnValue( scriptExecuteStringWithResults( filename, source ) );
} else {
//Try to execute the script in the game context
Core::Types::eExecResult execResult = Core::Script::execScript( filename );
//Handle the resulting situation
switch( execResult ) {
//If the script has compile errors, return false at the end of the function.
case Types::eSCRIPT_ERROR_COMPILE_FAILED:
CORE->debug->log(std::string("\t :x: Compilation failed : ").append( filename ).append("\n\n"), Types::eDEBUG_ERROR);
break;
//If the script is not found, return false at the end of the function.
case Types::eSCRIPT_ERROR_NOT_FOUND:
CORE->debug->log(std::string("\t :x: File not found : ").append( filename ).append("\n\n"), Types::eDEBUG_WARNING);
break;
//Unknown result, return false.
case Types::eSCRIPT_ERROR_UNKNOWN:
return scriptReturnBool( false );
break;
//If no errors, return true.
case Types::eSCRIPT_ERROR_NONE:
return scriptReturnBool( true );
break;
//Default fall through to false.
default:
return scriptReturnBool( false );
break;
} //switch( execResult )
} //else !executeSourceAndReturnResults
//Default fall through to false.
return scriptReturnBool( false );
} //exec
//! - .
/*!
\bUsage : ;
\param .
\return .
*/
//exit all systems and shutdown
scriptReturn exit( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//Tell the engine we are leaving.
CORE->startShutdown();
//No return value
return scriptReturnUndefined;
}
//! - .
/*!
\bUsage : ;
\param .
\return .
*/
//get a unique id for stuff
scriptReturn getuuid( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//Return the string UUID
return scriptReturnString( CORE->getUUID() );
}
//! - .
/*!
\bUsage : ;
\param .
\return .
*/
//get the time from the main timer
scriptReturn gettime( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//Return the time in ms
return scriptReturnNumber( CORE->getTime() * 1000.0f );
}
//! - .
/*!
\bUsage : ;
\param .
\return .
*/
//reload app
scriptReturn reload( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//Properties for the handler
std::string projectpath;
std::string projectscript;
//If we have arguments
if( scriptArg(1) ) {
scriptArgString(1, projectpath);
//If specified, the starting script
if( scriptArg(2) ) {
scriptArgString(2, projectscript);
}
}
//Create a property container
//for sending the information
bea::PropertyContainer p;
p.set("projectpath", projectpath);
p.set("projectscript", projectscript);
//Set the flag so nothing updates
CORE->restarting = true;
//If there is a renderer,
//it shouldn't update
#ifndef LAB_HEADLESS
CORE->phoenix->ready = false;
#endif //#ifndef LAB_HEADLESS
//Set the restart to happen in half a second
CORE->time->schedule("core::restart", 500, p);
//Return undefined
return scriptReturnUndefined;
}
//! - .
/*!
\bUsage : ;
\param .
\return .
*/
//execute a system command
scriptReturn cmd( scriptCallbackArgs ) {
//Required
scriptCallbackBegin;
//cmd can append command line parameters
//to the called application in 'command'.
std::string command;
std::string commandline;
//store the command
scriptArgString(1, command);
//if any, store the commandline
if( scriptArg(2) ) {
scriptArgString(2, commandline );
}
std::string results = "";
#ifdef LAB_OS_WINDOWS
FILE* pipe = _popen(command.c_str() , "r");
#else
FILE* pipe = popen(command.c_str() , "r");
#endif
if (pipe) {
char buffer[128];
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
results += buffer;
}
#ifdef LAB_OS_WINDOWS
_pclose(pipe);
#else
pclose(pipe);
#endif
} //if pipe
//Return the result
return scriptReturnString( results );
}
} //Scripted
} //Core
/*
The MIT license :
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Copyright (C) 2011 by Underscore Discovery, Sven "FuzzYspo0N" Bergstrom ( http://underscorediscovery.com )
For more license information, please read the following
http://laboratory2D.com/#/license
The license is MIT based, the MIT license is appended to the file.
*/
#ifndef Laboratory2D_scriptCore_h
#define Laboratory2D_scriptCore_h
//Required
#include "Scripted/Common/scriptCommon.h"
//Required
#include scriptIncludes;
//Custom Includes
// none
namespace Core {
namespace Scripted {
//Print some text to the console/ui console/log
scriptReturn echo( scriptCallbackArgs );
//execute a javascript file
scriptReturn exec( scriptCallbackArgs );
//exit all systems and shutdown
scriptReturn exit( scriptCallbackArgs );
//get a unique id for stuff
scriptReturn getuuid( scriptCallbackArgs );
//get the time from the main timer
scriptReturn gettime( scriptCallbackArgs );
//reload app with a new or the same project
scriptReturn reload( scriptCallbackArgs );
//execute a system command, returning the results
scriptReturn cmd( scriptCallbackArgs );
} //namespace Scripted
} //namespace Core
#endif
/*
The MIT license :
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment