2025-11-12 10:21:27 -08:00
/* Copyright (C) 2026 Wildfire Games.
2023-12-02 16:30:12 -08:00
* This file is part of 0 A . D .
2017-09-11 20:11:33 -07:00
*
2023-12-02 16:30:12 -08:00
* 0 A . D . is free software : you can redistribute it and / or modify
2017-09-11 20:11:33 -07:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 2 of the License , or
* ( at your option ) any later version .
*
2023-12-02 16:30:12 -08:00
* 0 A . D . is distributed in the hope that it will be useful ,
2017-09-11 20:11:33 -07:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2023-12-02 16:30:12 -08:00
* along with 0 A . D . If not , see < http : //www.gnu.org/licenses/>.
2017-09-11 20:11:33 -07:00
*/
# include "precompiled.h"
# include "JSInterface_Main.h"
# include "graphics/FontMetrics.h"
# include "graphics/MapReader.h"
2025-06-29 06:35:45 -07:00
# include "lib/code_generation.h"
2025-08-02 07:05:58 -07:00
# include "lib/file/vfs/vfs_path.h"
# include "lib/frequency_filter.h"
2017-09-11 20:11:33 -07:00
# include "lib/sysdep/sysdep.h"
2025-08-02 07:05:58 -07:00
# include "lib/types.h"
2018-06-21 09:38:08 -07:00
# include "maths/MD5.h"
2025-08-02 07:05:58 -07:00
# include "maths/Size2D.h"
2025-05-27 20:44:49 -07:00
# include "ps/CLogger.h"
2025-08-02 07:05:58 -07:00
# include "ps/CStr.h"
2017-09-11 20:11:33 -07:00
# include "ps/CStrIntern.h"
2025-08-02 07:05:58 -07:00
# include "ps/Errors.h"
2017-09-11 20:11:33 -07:00
# include "ps/GUID.h"
# include "ps/GameSetup/Atlas.h"
# include "ps/Globals.h"
2018-08-08 05:59:05 -07:00
# include "ps/Util.h"
2021-03-02 12:01:14 -08:00
# include "scriptinterface/FunctionWrapper.h"
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
# include "scriptinterface/Request.h"
2017-09-11 20:11:33 -07:00
# include "tools/atlas/GameInterface/GameLoop.h"
2025-08-02 07:05:58 -07:00
# include <js/RootingAPI.h>
# include <js/TypeDecls.h>
# include <js/Value.h>
# include <string>
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
namespace Script { class Interface ; }
2025-08-02 07:05:58 -07:00
2025-11-12 10:21:27 -08:00
extern void QuitEngine ( int exitStatus ) ;
2021-03-02 12:01:14 -08:00
namespace JSI_Main
{
bool AtlasIsAvailable ( )
2017-09-11 20:11:33 -07:00
{
return ATLAS_IsAvailable ( ) ;
}
2021-03-02 12:01:14 -08:00
bool IsAtlasRunning ( )
2017-09-11 20:11:33 -07:00
{
return g_AtlasGameLoop & & g_AtlasGameLoop - > running ;
}
2021-03-02 12:01:14 -08:00
void OpenURL ( const std : : string & url )
2017-09-11 20:11:33 -07:00
{
sys_open_url ( url ) ;
}
2021-03-02 12:01:14 -08:00
std : : wstring GetSystemUsername ( )
2017-09-11 20:11:33 -07:00
{
return sys_get_user_name ( ) ;
}
2021-03-02 12:01:14 -08:00
std : : wstring GetMatchID ( )
2017-09-11 20:11:33 -07:00
{
return ps_generate_guid ( ) . FromUTF8 ( ) ;
}
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
JS : : Value LoadMapSettings ( const Script : : Interface & scriptInterface , const VfsPath & pathname )
2017-09-11 20:11:33 -07:00
{
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
Script : : Request rq ( scriptInterface ) ;
2017-09-11 20:11:33 -07:00
CMapSummaryReader reader ;
if ( reader . LoadMap ( pathname ) ! = PSRETURN_OK )
return JS : : UndefinedValue ( ) ;
2020-11-13 05:18:22 -08:00
JS : : RootedValue settings ( rq . cx ) ;
2021-03-02 12:01:14 -08:00
reader . GetMapSettings ( scriptInterface , & settings ) ;
2017-09-11 20:11:33 -07:00
return settings ;
}
// This value is recalculated once a frame. We take special care to
// filter it, so it is both accurate and free of jitter.
2021-03-02 12:01:14 -08:00
int GetFps ( )
2017-09-11 20:11:33 -07:00
{
if ( ! g_frequencyFilter )
return 0 ;
return g_frequencyFilter - > StableFrequency ( ) ;
}
2025-12-18 08:00:15 -08:00
CSize2D CalculateTextSize ( const std : : string & fontName , const std : : wstring & text )
2017-09-11 20:11:33 -07:00
{
2025-05-16 16:02:40 -07:00
float width = 0 ;
float height = 0 ;
2017-09-11 20:11:33 -07:00
CStrIntern _fontName ( fontName ) ;
CFontMetrics fontMetrics ( _fontName ) ;
fontMetrics . CalculateStringSize ( text . c_str ( ) , width , height ) ;
2022-03-02 08:27:11 -08:00
return CSize2D ( width , height ) ;
}
2025-12-18 08:00:15 -08:00
CSize2D GetTextSize ( const std : : string & fontName , const std : : wstring & text )
{
ONCE ( LOGWARNING ( " Engine.GetTextSize is deprecated and will be removed in a future version. Instead use guiObject.getPreferredTextSize and dropdown.getPreferredHeaderTextSize for more convenient text sizing or guiObject.getTextSize for sizing within an object. " ) ) ;
return CalculateTextSize ( fontName , text ) ;
}
2022-03-02 08:27:11 -08:00
int GetTextWidth ( const std : : string & fontName , const std : : wstring & text )
{
2025-12-18 08:00:15 -08:00
ONCE ( LOGWARNING ( " Engine.GetTextWidth is deprecated and will be removed in a future version. Instead use guiObject.getPreferredTextSize and dropdown.getPreferredHeaderTextSize for more convenient text sizing or guiObject.getTextSize for sizing within an object. " ) ) ;
return CalculateTextSize ( fontName , text ) . Width ;
2017-09-11 20:11:33 -07:00
}
2021-03-02 12:01:14 -08:00
std : : string CalculateMD5 ( const std : : string & input )
2018-06-21 09:38:08 -07:00
{
u8 digest [ MD5 : : DIGESTSIZE ] ;
MD5 m ;
2018-06-21 09:52:52 -07:00
m . Update ( ( const u8 * ) input . c_str ( ) , input . length ( ) ) ;
2018-06-21 09:38:08 -07:00
m . Final ( digest ) ;
2018-08-08 05:59:05 -07:00
return Hexify ( digest , MD5 : : DIGESTSIZE ) ;
2018-06-21 09:38:08 -07:00
}
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
void RegisterScriptFunctions ( const Script : : Request & rq )
2017-09-11 20:11:33 -07:00
{
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
Script : : Function : : Register < & QuitEngine > ( rq , " Exit " ) ;
Script : : Function : : Register < & AtlasIsAvailable > ( rq , " AtlasIsAvailable " ) ;
Script : : Function : : Register < & IsAtlasRunning > ( rq , " IsAtlasRunning " ) ;
Script : : Function : : Register < & OpenURL > ( rq , " OpenURL " ) ;
Script : : Function : : Register < & GetSystemUsername > ( rq , " GetSystemUsername " ) ;
Script : : Function : : Register < & GetMatchID > ( rq , " GetMatchID " ) ;
Script : : Function : : Register < & LoadMapSettings > ( rq , " LoadMapSettings " ) ;
Script : : Function : : Register < & GetFps > ( rq , " GetFPS " ) ;
Script : : Function : : Register < & GetTextSize > ( rq , " GetTextSize " ) ;
Script : : Function : : Register < & GetTextWidth > ( rq , " GetTextWidth " ) ;
Script : : Function : : Register < & CalculateMD5 > ( rq , " CalculateMD5 " ) ;
2021-03-02 12:01:14 -08:00
}
2017-09-11 20:11:33 -07:00
}