From 71e108f16a26183e348f5d42d8f92fa152d2f759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Brammer?= Date: Sun, 4 Mar 2012 21:23:11 +0100 Subject: [PATCH] Start of a C4Script API third party programs could use This is very experimental, subject to change, and the single function not at all useful yet. --- CMakeLists.txt | 2 + Makefile.am | 2 + include/c4script/c4script.h | 32 ++++++++ src/C4Include.h | 7 +- src/script/C4ScriptStandalone.cpp | 127 ++++++++++++++++++++++++++++++ src/script/shell.cpp | 109 ++----------------------- 6 files changed, 174 insertions(+), 105 deletions(-) create mode 100644 include/c4script/c4script.h create mode 100644 src/script/C4ScriptStandalone.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 033304df3..1117b291e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -926,6 +926,7 @@ add_executable(netpuncher EXCLUDE_FROM_ALL ) add_executable(c4script + include/c4script/c4script.h src/c4group/C4Group.cpp src/c4group/C4Group.h src/c4group/CStdFile.cpp @@ -956,6 +957,7 @@ add_executable(c4script src/script/C4StringTable.cpp src/script/C4PropList.cpp src/script/C4ScriptHost.cpp + src/script/C4ScriptStandalone.cpp src/script/C4ValueArray.cpp src/script/C4Value.cpp src/script/C4ValueMap.cpp diff --git a/Makefile.am b/Makefile.am index 68cfc4d60..a3ac3be79 100644 --- a/Makefile.am +++ b/Makefile.am @@ -690,6 +690,7 @@ endif ## c4script shell c4script_SOURCES = \ +include/c4script/c4script.h \ src/lib/C4SimpleLog.cpp \ src/lib/C4Real.cpp \ src/lib/C4Random.cpp \ @@ -701,6 +702,7 @@ src/script/C4AulParse.cpp \ src/script/C4StringTable.cpp \ src/script/C4PropList.cpp \ src/script/C4ScriptHost.cpp \ +src/script/C4ScriptStandalone.cpp \ src/script/C4ValueArray.cpp \ src/script/C4Value.cpp \ src/script/C4ValueMap.cpp \ diff --git a/include/c4script/c4script.h b/include/c4script/c4script.h new file mode 100644 index 000000000..5ad4f8a03 --- /dev/null +++ b/include/c4script/c4script.h @@ -0,0 +1,32 @@ +/* + * OpenClonk, http://www.openclonk.org + * + * Copyright (c) 2012 Günther Brammer + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef C4SCRIPTSTANDALONE_H +#define C4SCRIPTSTANDALONE_H + +#ifdef __cplusplus +extern "C" { +#endif + +int c4s_runscript(const char * filename); + +#ifdef __cplusplus +} +#endif + +#endif // C4SCRIPTSTANDALONE_H diff --git a/src/C4Include.h b/src/C4Include.h index eadc99376..b4b21bd92 100644 --- a/src/C4Include.h +++ b/src/C4Include.h @@ -20,7 +20,12 @@ * See clonk_trademark_license.txt for full license. */ -/* Main header to include all others */ +/* This header is included first from every source file. It serves three purposes: + - PlatformAbstraction.h + - Common utility functionality that's used everywhere + - Speeding up the compilation by precompiling this header +All of our headers are designed to be used with C4Include.h included before and +don't need to include this file or any of the files it includes. */ #ifndef INC_C4Include #define INC_C4Include diff --git a/src/script/C4ScriptStandalone.cpp b/src/script/C4ScriptStandalone.cpp new file mode 100644 index 000000000..a09482add --- /dev/null +++ b/src/script/C4ScriptStandalone.cpp @@ -0,0 +1,127 @@ +/* + * OpenClonk, http://www.openclonk.org + * + * Copyright (c) 2011-2012 Günther Brammer + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "../../include/c4script/c4script.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +C4Config Config; +C4Config::C4Config() {} +C4Config::~C4Config() {} +const char * C4Config::AtRelativePath(char const*s) {return s;} + +C4DefList Definitions; +C4DefList::C4DefList() {} +C4DefList::~C4DefList() {} +C4Def* C4DefList::ID2Def(C4ID id) {return NULL;} +C4Def * C4DefList::GetDef(int) {return 0;} +int C4DefList::GetDefCount() {return 0;} +void C4DefList::CallEveryDefinition() {} +void C4DefList::ResetIncludeDependencies() {} +bool C4DefList::DrawFontImage(const char* szImageTag, C4Facet& rTarget, C4DrawTransform* pTransform) { return false; } +float C4DefList::GetFontImageAspect(const char* szImageTag) { return -1.0f; } + +C4MaterialMap MaterialMap; +C4MaterialMap::C4MaterialMap() {} +C4MaterialMap::~C4MaterialMap() {} +void C4MaterialMap::UpdateScriptPointers() {} + +C4AulDebug *C4AulDebug::pDebug; +void C4AulDebug::DebugStepIn(C4AulBCC*) {} +void C4AulDebug::DebugStepOut(C4AulBCC*, C4AulScriptContext*, C4Value*) {} +void C4AulDebug::DebugStep(C4AulBCC*) {} + +C4GameObjects Objects; +C4GameObjects::C4GameObjects() {} +C4GameObjects::~C4GameObjects() {} +void C4GameObjects::UpdateScriptPointers() {} +void C4GameObjects::Clear(bool) {} +void C4GameObjects::Default() {} +bool C4GameObjects::Remove(C4Object*) {return 0;} +bool C4GameObjects::AssignInfo() {return 0;} +bool C4GameObjects::ValidateOwners() {return 0;} +C4Value C4GameObjects::GRBroadcast(char const*, C4AulParSet*, bool, bool) {return C4Value();} +C4Object * C4GameObjects::ObjectPointer(int) {return 0;} + +C4ObjectList::C4ObjectList() {} +C4ObjectList::~C4ObjectList() {} +void C4ObjectList::Default() {} +void C4ObjectList::Clear() {} +void C4ObjectList::InsertLinkBefore(C4ObjectLink*, C4ObjectLink*) {} +void C4ObjectList::InsertLink(C4ObjectLink*, C4ObjectLink*) {} +void C4ObjectList::RemoveLink(C4ObjectLink*) {} +bool C4ObjectList::Add(C4Object*, C4ObjectList::SortType, C4ObjectList*) {return 0;} +bool C4ObjectList::Remove(C4Object*) {return 0;} +bool C4ObjectList::AssignInfo() {return 0;} +bool C4ObjectList::ValidateOwners() {return 0;} + +void C4NotifyingObjectList::InsertLinkBefore(C4ObjectLink *pLink, C4ObjectLink *pBefore) {} +void C4NotifyingObjectList::InsertLink(C4ObjectLink*, C4ObjectLink*) {} +void C4NotifyingObjectList::RemoveLink(C4ObjectLink*) {} + +C4Reloc Reloc; +bool C4Reloc::Open(C4Group&, char const*) const {return false;} + +void C4LSector::Clear() {} +void C4Def::IncludeDefinition(C4Def*) {} +bool EraseItemSafe(const char *szFilename) {return false;} +void AddDbgRec(C4RecordChunkType, const void *, int) {} + +int c4s_runscript(const char * filename) +{ + InitCoreFunctionMap(&ScriptEngine); + + C4Group File; + if (!File.Open(GetWorkingDirectory())) + { + fprintf(stderr, "Open failed: %s\n", File.GetError()); + return 1; + } + + // get scripts + StdStrBuf fn; + File.ResetSearch(); + if (!File.FindNextEntry(filename, &fn)) + { + fprintf(stderr, "FindNextEntry failed: %s\n", File.GetError()); + return 1; + } + // host will be destroyed by script engine, so drop the references + GameScript.Reg2List(&ScriptEngine, &ScriptEngine); + GameScript.Load(File, fn.getData(), NULL, NULL, NULL); + + // Link script engine (resolve includes/appends, generate code) + ScriptEngine.Link(&::Definitions); + + // Set name list for globals + ScriptEngine.GlobalNamed.SetNameList(&ScriptEngine.GlobalNamedNames); + GameScript.Call("Main"); + GameScript.Clear(); + return 0; +} diff --git a/src/script/shell.cpp b/src/script/shell.cpp index 0326591e4..63138d2ec 100644 --- a/src/script/shell.cpp +++ b/src/script/shell.cpp @@ -1,7 +1,7 @@ /* * OpenClonk, http://www.openclonk.org * - * Copyright (c) 2011 Günther Brammer + * Copyright (c) 2012 Günther Brammer * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -16,110 +16,11 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -C4Config Config; -C4Config::C4Config() {} -C4Config::~C4Config() {} -const char * C4Config::AtRelativePath(char const*s) {return s;} - -C4DefList Definitions; -C4DefList::C4DefList() {} -C4DefList::~C4DefList() {} -C4Def* C4DefList::ID2Def(C4ID id) {return NULL;} -C4Def * C4DefList::GetDef(int) {return 0;} -int C4DefList::GetDefCount() {return 0;} -void C4DefList::CallEveryDefinition() {} -void C4DefList::ResetIncludeDependencies() {} -bool C4DefList::DrawFontImage(const char* szImageTag, C4Facet& rTarget, C4DrawTransform* pTransform) { return false; } -float C4DefList::GetFontImageAspect(const char* szImageTag) { return -1.0f; } - -C4MaterialMap MaterialMap; -C4MaterialMap::C4MaterialMap() {} -C4MaterialMap::~C4MaterialMap() {} -void C4MaterialMap::UpdateScriptPointers() {} - -C4AulDebug *C4AulDebug::pDebug; -void C4AulDebug::DebugStepIn(C4AulBCC*) {} -void C4AulDebug::DebugStepOut(C4AulBCC*, C4AulScriptContext*, C4Value*) {} -void C4AulDebug::DebugStep(C4AulBCC*) {} - -C4GameObjects Objects; -C4GameObjects::C4GameObjects() {} -C4GameObjects::~C4GameObjects() {} -void C4GameObjects::UpdateScriptPointers() {} -void C4GameObjects::Clear(bool) {} -void C4GameObjects::Default() {} -bool C4GameObjects::Remove(C4Object*) {return 0;} -bool C4GameObjects::AssignInfo() {return 0;} -bool C4GameObjects::ValidateOwners() {return 0;} -C4Value C4GameObjects::GRBroadcast(char const*, C4AulParSet*, bool, bool) {return C4Value();} -C4Object * C4GameObjects::ObjectPointer(int) {return 0;} - -C4ObjectList::C4ObjectList() {} -C4ObjectList::~C4ObjectList() {} -void C4ObjectList::Default() {} -void C4ObjectList::Clear() {} -void C4ObjectList::InsertLinkBefore(C4ObjectLink*, C4ObjectLink*) {} -void C4ObjectList::InsertLink(C4ObjectLink*, C4ObjectLink*) {} -void C4ObjectList::RemoveLink(C4ObjectLink*) {} -bool C4ObjectList::Add(C4Object*, C4ObjectList::SortType, C4ObjectList*) {return 0;} -bool C4ObjectList::Remove(C4Object*) {return 0;} -bool C4ObjectList::AssignInfo() {return 0;} -bool C4ObjectList::ValidateOwners() {return 0;} - -void C4NotifyingObjectList::InsertLinkBefore(C4ObjectLink *pLink, C4ObjectLink *pBefore) {} -void C4NotifyingObjectList::InsertLink(C4ObjectLink*, C4ObjectLink*) {} -void C4NotifyingObjectList::RemoveLink(C4ObjectLink*) {} - -C4Reloc Reloc; -bool C4Reloc::Open(C4Group&, char const*) const {return false;} - -void C4LSector::Clear() {} -void C4Def::IncludeDefinition(C4Def*) {} -bool EraseItemSafe(const char *szFilename) {return false;} -void AddDbgRec(C4RecordChunkType, const void *, int) {} +// Do not include C4Include.h - this file tests whether +// c4script.h is useable without that. +#include "../../include/c4script/c4script.h" int main(int argc, const char * argv[]) { - InitCoreFunctionMap(&ScriptEngine); - - C4Group File; - if (!File.Open(GetWorkingDirectory())) - { - fprintf(stderr, "Open failed: %s\n", File.GetError()); - return 1; - } - - // get scripts - StdStrBuf fn; - File.ResetSearch(); - if (!File.FindNextEntry(argv[1], &fn)) - { - fprintf(stderr, "FindNextEntry failed: %s\n", File.GetError()); - return 1; - } - // host will be destroyed by script engine, so drop the references - GameScript.Reg2List(&ScriptEngine, &ScriptEngine); - GameScript.Load(File, fn.getData(), NULL, NULL, NULL); - - // Link script engine (resolve includes/appends, generate code) - ScriptEngine.Link(&::Definitions); - - // Set name list for globals - ScriptEngine.GlobalNamed.SetNameList(&ScriptEngine.GlobalNamedNames); - GameScript.Call("Main"); - GameScript.Clear(); - return 0; + return c4s_runscript(argv[1]); }