diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a5c8232..0886ad07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -389,10 +389,13 @@ target_precompile_headers(libgui PRIVATE src/engine/pch.h) set(SCRIPT_HEADERS src/engine/script/enginetype.h src/engine/script/execution.h + src/engine/script/executioncontext.h + src/engine/script/executionstate.h src/engine/script/instrutil.h src/engine/script/ncsreader.h src/engine/script/program.h src/engine/script/routine.h + src/engine/script/routineprovider.h src/engine/script/services.h src/engine/script/scripts.h src/engine/script/types.h diff --git a/src/engine/game/action/docommand.h b/src/engine/game/action/docommand.h index ef8c51b5..e6b66634 100644 --- a/src/engine/game/action/docommand.h +++ b/src/engine/game/action/docommand.h @@ -17,12 +17,16 @@ #pragma once -#include "../../script/types.h" - #include "action.h" namespace reone { +namespace script { + +struct ExecutionContext; + +} + namespace game { class CommandAction : public Action { diff --git a/src/engine/game/actionexecutor.cpp b/src/engine/game/actionexecutor.cpp index 81b61e30..28e756e3 100644 --- a/src/engine/game/actionexecutor.cpp +++ b/src/engine/game/actionexecutor.cpp @@ -21,6 +21,7 @@ #include "../common/guardutil.h" #include "../scene/types.h" #include "../script/execution.h" +#include "../script/executioncontext.h" #include "action/usefeat.h" #include "action/useskill.h" diff --git a/src/engine/game/script/routines.cpp b/src/engine/game/script/routines.cpp index 49e7d7f1..6ac603d7 100644 --- a/src/engine/game/script/routines.cpp +++ b/src/engine/game/script/routines.cpp @@ -17,6 +17,8 @@ #include "routines.h" +#include "../../script/executioncontext.h" + #include "../enginetype/effect.h" #include "../enginetype/event.h" #include "../enginetype/location.h" diff --git a/src/engine/game/script/routines.h b/src/engine/game/script/routines.h index 62a7315a..f126db71 100644 --- a/src/engine/game/script/routines.h +++ b/src/engine/game/script/routines.h @@ -20,6 +20,7 @@ #include "../../common/collectionutil.h" #include "../../resource/types.h" #include "../../script/routine.h" +#include "../../script/routineprovider.h" #include "../../script/types.h" #include "../../script/variable.h" diff --git a/src/engine/game/script/routines_common.cpp b/src/engine/game/script/routines_common.cpp index b39987f1..d1787923 100644 --- a/src/engine/game/script/routines_common.cpp +++ b/src/engine/game/script/routines_common.cpp @@ -24,6 +24,7 @@ #include "../../common/log.h" #include "../../common/random.h" #include "../../resource/strings.h" +#include "../../script/executioncontext.h" #include "../game.h" diff --git a/src/engine/game/script/routines_events.cpp b/src/engine/game/script/routines_events.cpp index a25189e3..06b07f30 100644 --- a/src/engine/game/script/routines_events.cpp +++ b/src/engine/game/script/routines_events.cpp @@ -22,6 +22,7 @@ #include "routines.h" #include "../../common/log.h" +#include "../../script/executioncontext.h" #include "../enginetype/event.h" #include "../game.h" diff --git a/src/engine/game/script/runner.cpp b/src/engine/game/script/runner.cpp index 3e2cd951..6635ca44 100644 --- a/src/engine/game/script/runner.cpp +++ b/src/engine/game/script/runner.cpp @@ -18,6 +18,7 @@ #include "runner.h" #include "../../script/execution.h" +#include "../../script/executioncontext.h" #include "../game.h" diff --git a/src/engine/script/execution.cpp b/src/engine/script/execution.cpp index c3423e40..982e7d70 100644 --- a/src/engine/script/execution.cpp +++ b/src/engine/script/execution.cpp @@ -17,10 +17,15 @@ #include "execution.h" +#include "../common/guardutil.h" #include "../common/log.h" +#include "executioncontext.h" #include "instrutil.h" +#include "program.h" #include "routine.h" +#include "routineprovider.h" +#include "variable.h" using namespace std; using namespace std::placeholders; @@ -31,7 +36,9 @@ namespace script { static constexpr int kStartInstructionOffset = 13; -ScriptExecution::ScriptExecution(const shared_ptr &program, unique_ptr context) : _context(move(context)), _program(program) { +ScriptExecution::ScriptExecution(shared_ptr program, unique_ptr context) : _context(move(context)), _program(program) { + ensureNotNull(program, "program"); + static unordered_map> handlers { { ByteCode::CopyDownSP, &ScriptExecution::executeCopyDownSP }, { ByteCode::Reserve, &ScriptExecution::executeReserve }, diff --git a/src/engine/script/execution.h b/src/engine/script/execution.h index 2679442d..29bb37f1 100644 --- a/src/engine/script/execution.h +++ b/src/engine/script/execution.h @@ -17,17 +17,22 @@ #pragma once -#include "program.h" +#include "executionstate.h" #include "types.h" -#include "variable.h" namespace reone { namespace script { +struct ExecutionContext; +struct Instruction; +struct Variable; + +class ScriptProgram; + class ScriptExecution : boost::noncopyable { public: - ScriptExecution(const std::shared_ptr &program, std::unique_ptr context); + ScriptExecution(std::shared_ptr program, std::unique_ptr context); int run(); diff --git a/src/engine/script/executioncontext.h b/src/engine/script/executioncontext.h new file mode 100644 index 00000000..24f7c22a --- /dev/null +++ b/src/engine/script/executioncontext.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020-2021 The reone project contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * 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 + * along with this program. If not, see . + */ + +#pragma once + +#include "types.h" + +namespace reone { + +namespace script { + +struct ExecutionState; + +class IRoutineProvider; + +struct ExecutionContext { + IRoutineProvider *routines { nullptr }; + std::shared_ptr savedState; + uint32_t callerId { kObjectInvalid }; + uint32_t triggererId { kObjectInvalid }; + int userDefinedEventNumber { -1 }; + int scriptVar { -1 }; +}; + +} // namespace script + +} // namespae reone diff --git a/src/engine/script/executionstate.h b/src/engine/script/executionstate.h new file mode 100644 index 00000000..ad14d459 --- /dev/null +++ b/src/engine/script/executionstate.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020-2021 The reone project contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * 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 + * along with this program. If not, see . + */ + +#pragma once + +namespace reone { + +namespace script { + +struct Variable; + +class ScriptProgram; + +struct ExecutionState { + std::shared_ptr program; + std::vector globals; + std::vector locals; + uint32_t insOffset { 0 }; +}; + +} // namespace script + +} // namespae reone diff --git a/src/engine/script/ncsreader.cpp b/src/engine/script/ncsreader.cpp index 597c1963..eac0fe13 100644 --- a/src/engine/script/ncsreader.cpp +++ b/src/engine/script/ncsreader.cpp @@ -19,6 +19,8 @@ #include "../common/log.h" +#include "program.h" + using namespace std; namespace reone { diff --git a/src/engine/script/ncsreader.h b/src/engine/script/ncsreader.h index 2c7ec6bc..56ecce04 100644 --- a/src/engine/script/ncsreader.h +++ b/src/engine/script/ncsreader.h @@ -19,12 +19,12 @@ #include "../resource/format/binreader.h" -#include "program.h" - namespace reone { namespace script { +class ScriptProgram; + /** * Parses compiled script program. * diff --git a/src/engine/script/program.h b/src/engine/script/program.h index 9cf1a2dc..233f8032 100644 --- a/src/engine/script/program.h +++ b/src/engine/script/program.h @@ -17,85 +17,12 @@ #pragma once +#include "types.h" + namespace reone { namespace script { -enum class ByteCode { - CopyDownSP = 0x01, - Reserve = 0x02, - CopyTopSP = 0x03, - PushConstant = 0x04, - CallRoutine = 0x05, - LogicalAnd = 0x06, - LogicalOr = 0x07, - InclusiveBitwiseOr = 0x08, - ExclusiveBitwiseOr = 0x09, - BitwiseAnd = 0x0a, - Equal = 0x0b, - NotEqual = 0x0c, - GreaterThanOrEqual = 0x0d, - GreaterThan = 0x0e, - LessThan = 0x0f, - LessThanOrEqual = 0x10, - ShiftLeft = 0x11, - ShiftRight = 0x12, - UnsignedShiftRight = 0x13, - Add = 0x14, - Subtract = 0x15, - Multiply = 0x16, - Divide = 0x17, - Mod = 0x18, - Negate = 0x19, - OnesComplement = 0x1a, - AdjustSP = 0x1b, - Jump = 0x1d, - JumpToSubroutine = 0x1e, - JumpIfZero = 0x1f, - Return = 0x20, - Destruct = 0x21, - LogicalNot = 0x22, - DecRelToSP = 0x23, - IncRelToSP = 0x24, - JumpIfNonZero = 0x25, - CopyDownBP = 0x26, - CopyTopBP = 0x27, - DecRelToBP = 0x28, - IncRelToBP = 0x29, - SaveBP = 0x2a, - RestoreBP = 0x2b, - StoreState = 0x2c, - Noop = 0x2d, - Invalid = 0xff -}; - -enum class InstructionType { - None = 0, - One = 0x01, - Int = 0x03, - Float = 0x04, - String = 0x05, - Object = 0x06, - Effect = 0x10, - Event = 0x11, - Location = 0x12, - Talent = 0x13, - IntInt = 0x20, - FloatFloat = 0x21, - ObjectObject = 0x22, - StringString = 0x23, - StructStruct = 0x24, - IntFloat = 0x25, - FloatInt = 0x26, - EffectEffect = 0x30, - EventEvent = 0x31, - LocationLocation = 0x32, - TalentTalent = 0x33, - VectorVector = 0x3a, - VectorFloat = 0x3b, - FloatVector = 0x3c -}; - class NcsReader; struct Instruction { diff --git a/src/engine/script/routine.cpp b/src/engine/script/routine.cpp index 61b1090e..79d8dbb2 100644 --- a/src/engine/script/routine.cpp +++ b/src/engine/script/routine.cpp @@ -19,6 +19,8 @@ #include "../common/log.h" +#include "variable.h" + using namespace std; namespace reone { diff --git a/src/engine/script/routine.h b/src/engine/script/routine.h index c5ad8899..501379ab 100644 --- a/src/engine/script/routine.h +++ b/src/engine/script/routine.h @@ -17,12 +17,15 @@ #pragma once -#include "variable.h" +#include "types.h" namespace reone { namespace script { +struct ExecutionContext; +struct Variable; + class Routine { public: Routine() = default; diff --git a/src/engine/script/routineprovider.h b/src/engine/script/routineprovider.h new file mode 100644 index 00000000..f46bfbe9 --- /dev/null +++ b/src/engine/script/routineprovider.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020-2021 The reone project contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * 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 + * along with this program. If not, see . + */ + +#pragma once + +namespace reone { + +namespace script { + +class Routine; + +class IRoutineProvider { +public: + virtual ~IRoutineProvider() { + } + + virtual const Routine &get(int index) = 0; +}; + +} // namespace script + +} // namespae reone diff --git a/src/engine/script/types.h b/src/engine/script/types.h index 1fedb112..e73ed8b0 100644 --- a/src/engine/script/types.h +++ b/src/engine/script/types.h @@ -24,33 +24,95 @@ namespace script { constexpr uint32_t kObjectSelf = 0; constexpr uint32_t kObjectInvalid = 1; -struct Variable; - -class ScriptProgram; -class Routine; - -struct ExecutionState { - std::shared_ptr program; - std::vector globals; - std::vector locals; - uint32_t insOffset { 0 }; +enum class ByteCode { + CopyDownSP = 0x01, + Reserve = 0x02, + CopyTopSP = 0x03, + PushConstant = 0x04, + CallRoutine = 0x05, + LogicalAnd = 0x06, + LogicalOr = 0x07, + InclusiveBitwiseOr = 0x08, + ExclusiveBitwiseOr = 0x09, + BitwiseAnd = 0x0a, + Equal = 0x0b, + NotEqual = 0x0c, + GreaterThanOrEqual = 0x0d, + GreaterThan = 0x0e, + LessThan = 0x0f, + LessThanOrEqual = 0x10, + ShiftLeft = 0x11, + ShiftRight = 0x12, + UnsignedShiftRight = 0x13, + Add = 0x14, + Subtract = 0x15, + Multiply = 0x16, + Divide = 0x17, + Mod = 0x18, + Negate = 0x19, + OnesComplement = 0x1a, + AdjustSP = 0x1b, + Jump = 0x1d, + JumpToSubroutine = 0x1e, + JumpIfZero = 0x1f, + Return = 0x20, + Destruct = 0x21, + LogicalNot = 0x22, + DecRelToSP = 0x23, + IncRelToSP = 0x24, + JumpIfNonZero = 0x25, + CopyDownBP = 0x26, + CopyTopBP = 0x27, + DecRelToBP = 0x28, + IncRelToBP = 0x29, + SaveBP = 0x2a, + RestoreBP = 0x2b, + StoreState = 0x2c, + Noop = 0x2d, + Invalid = 0xff }; -class IRoutineProvider { -public: - virtual ~IRoutineProvider() { - } - - virtual const Routine &get(int index) = 0; +enum class InstructionType { + None = 0, + One = 0x01, + Int = 0x03, + Float = 0x04, + String = 0x05, + Object = 0x06, + Effect = 0x10, + Event = 0x11, + Location = 0x12, + Talent = 0x13, + IntInt = 0x20, + FloatFloat = 0x21, + ObjectObject = 0x22, + StringString = 0x23, + StructStruct = 0x24, + IntFloat = 0x25, + FloatInt = 0x26, + EffectEffect = 0x30, + EventEvent = 0x31, + LocationLocation = 0x32, + TalentTalent = 0x33, + VectorVector = 0x3a, + VectorFloat = 0x3b, + FloatVector = 0x3c }; -struct ExecutionContext { - IRoutineProvider *routines { nullptr }; - std::shared_ptr savedState; - uint32_t callerId { kObjectInvalid }; - uint32_t triggererId { kObjectInvalid }; - int userDefinedEventNumber { -1 }; - int scriptVar { -1 }; +enum class VariableType { + Void, + Int, + Float, + String, + Vector, + Object, + Effect, + Event, + Location, + Talent, + Action, + + NotImplemented // used to return default values from placeholder routines }; } // namespace script diff --git a/src/engine/script/variable.h b/src/engine/script/variable.h index e79a02a8..0b11c2f9 100644 --- a/src/engine/script/variable.h +++ b/src/engine/script/variable.h @@ -23,21 +23,7 @@ namespace reone { namespace script { -enum class VariableType { - Void, - Int, - Float, - String, - Vector, - Object, - Effect, - Event, - Location, - Talent, - Action, - - NotImplemented // used to return default values from placeholder routines -}; +struct ExecutionContext; class EngineType; class ScriptObject; diff --git a/src/tests/script/execution.cpp b/src/tests/script/execution.cpp index 0ae905b2..b0296669 100644 --- a/src/tests/script/execution.cpp +++ b/src/tests/script/execution.cpp @@ -22,6 +22,9 @@ #include #include "../../engine/script/execution.h" +#include "../../engine/script/executioncontext.h" +#include "../../engine/script/program.h" +#include "../../engine/script/variable.h" using namespace std;