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.
rope
Günther Brammer 2012-03-04 21:23:11 +01:00
parent 9543c10cad
commit 71e108f16a
6 changed files with 174 additions and 105 deletions

View File

@ -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

View File

@ -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 \

View File

@ -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

View File

@ -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

View File

@ -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 <C4Include.h>
#include <C4AulDebug.h>
#include <C4Def.h>
#include <C4ScriptHost.h>
#include <C4DefList.h>
#include <C4Log.h>
#include <C4Config.h>
#include <C4GameObjects.h>
#include <C4Material.h>
#include <C4Reloc.h>
#include <C4Record.h>
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;
}

View File

@ -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 <C4Include.h>
#include <C4AulDebug.h>
#include <C4Def.h>
#include <C4ScriptHost.h>
#include <C4DefList.h>
#include <C4Log.h>
#include <C4Config.h>
#include <C4GameObjects.h>
#include <C4Material.h>
#include <C4Reloc.h>
#include <C4Record.h>
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]);
}