From b867365e85fd58e49f23976c874cf1363ea98108 Mon Sep 17 00:00:00 2001 From: Fabian Jungwirth Date: Tue, 19 Sep 2017 20:38:14 +0200 Subject: [PATCH] Refactor code base add namespace JVM add namespace Rage Split up old JVM Class in VM, Object and Exception --- src/RageJavaCore.cpp | 17 +-- src/RageJavaCore.hpp | 6 +- src/RagePlugin.cpp | 1 - src/RagePlugin.hpp | 5 +- src/event/PlayerEventHandler.cpp | 112 ++++++++-------- src/event/PlayerEventHandler.hpp | 4 +- src/jvm/{TypeConverter.cpp => Converter.cpp} | 28 ++-- src/jvm/{TypeConverter.hpp => Converter.hpp} | 33 +++-- src/jvm/Exception.cpp | 21 +++ src/jvm/Exception.hpp | 47 +++++++ src/jvm/JVM.hpp | 49 ------- src/jvm/JVMException.hpp | 34 ----- src/jvm/Object.cpp | 23 ++++ src/jvm/{JVMException.cpp => Object.hpp} | 15 ++- src/jvm/{JVM.cpp => VM.cpp} | 43 ++---- src/jvm/VM.hpp | 44 +++++++ src/{jni => native}/BlipNative.hpp | 0 src/{jni => native}/CheckpointNative.hpp | 0 src/{jni => native}/ColshapeNative.hpp | 0 src/{jni => native}/MarkerNative.hpp | 0 src/{jni => native}/ObjectNative.hpp | 0 src/{jni => native}/PickupNative.hpp | 0 src/{jni => native}/PlayerNative.cpp | 132 +++++++++---------- src/{jni => native}/PlayerNative.hpp | 0 src/{jni => native}/VehicleNative.hpp | 0 src/rage/Player.cpp | 14 +- src/rage/Player.hpp | 10 +- 27 files changed, 340 insertions(+), 298 deletions(-) rename src/jvm/{TypeConverter.cpp => Converter.cpp} (56%) rename src/jvm/{TypeConverter.hpp => Converter.hpp} (56%) create mode 100644 src/jvm/Exception.cpp create mode 100644 src/jvm/Exception.hpp delete mode 100644 src/jvm/JVM.hpp delete mode 100644 src/jvm/JVMException.hpp create mode 100644 src/jvm/Object.cpp rename src/jvm/{JVMException.cpp => Object.hpp} (57%) rename src/jvm/{JVM.cpp => VM.cpp} (70%) create mode 100644 src/jvm/VM.hpp rename src/{jni => native}/BlipNative.hpp (100%) rename src/{jni => native}/CheckpointNative.hpp (100%) rename src/{jni => native}/ColshapeNative.hpp (100%) rename src/{jni => native}/MarkerNative.hpp (100%) rename src/{jni => native}/ObjectNative.hpp (100%) rename src/{jni => native}/PickupNative.hpp (100%) rename src/{jni => native}/PlayerNative.cpp (66%) rename src/{jni => native}/PlayerNative.hpp (100%) rename src/{jni => native}/VehicleNative.hpp (100%) diff --git a/src/RageJavaCore.cpp b/src/RageJavaCore.cpp index 506c26b..6e89b8b 100644 --- a/src/RageJavaCore.cpp +++ b/src/RageJavaCore.cpp @@ -12,24 +12,9 @@ bool RageJavaCore::initialize(rage::IMultiplayer *mp) { this->mp = mp; - if(!this->initializeJVM()) { + if (!JVM::VM::create()) { return false; } - if(!this->initializeEventHandlers()) { - return false; - } - - return true; -} - -bool RageJavaCore::initializeJVM() { - if (!JVM::createJavaVirtualMachine()) { - return false; - } - return true; -} - -bool RageJavaCore::initializeEventHandlers() { try { mp->AddEventHandler(new PlayerEventHandler); mp->AddEventHandler(new EntityEventHandler); diff --git a/src/RageJavaCore.hpp b/src/RageJavaCore.hpp index e58a9e0..ecb5335 100644 --- a/src/RageJavaCore.hpp +++ b/src/RageJavaCore.hpp @@ -10,7 +10,8 @@ #pragma once -#include "jvm/JVM.hpp" +#include "jvm/VM.hpp" +#include "jvm/Exception.hpp" #include "event/PlayerEventHandler.hpp" #include "event/EntityEventHandler.hpp" #include "event/VehicleEventHandler.hpp" @@ -32,9 +33,6 @@ class RageJavaCore { bool initialize(rage::IMultiplayer *mp); private: - bool initializeJVM(); - bool initializeEventHandlers(); - rage::IMultiplayer *mp; }; diff --git a/src/RagePlugin.cpp b/src/RagePlugin.cpp index 48422d2..3847a37 100644 --- a/src/RagePlugin.cpp +++ b/src/RagePlugin.cpp @@ -9,7 +9,6 @@ */ #include "RagePlugin.hpp" -#include "RageJavaCore.hpp" RAGE_API rage::IPlugin *InitializePlugin(rage::IMultiplayer *mp) { std::cout << "Initialize Rage MultiPlayer Java Runtime ... " << std::endl; diff --git a/src/RagePlugin.hpp b/src/RagePlugin.hpp index be78ccb..4c226a0 100644 --- a/src/RagePlugin.hpp +++ b/src/RagePlugin.hpp @@ -13,11 +13,12 @@ #include #endif #include +#include "RageJavaCore.hpp" #include "sdk/rage.hpp" #include "event/PlayerEventHandler.hpp" #include "event/EntityEventHandler.hpp" #include "event/VehicleEventHandler.hpp" #include "event/ColshapeEventHandler.hpp" #include "event/CheckpointEventHandler.hpp" -#include "jvm/JVMException.hpp" -#include "jvm/JVM.hpp" \ No newline at end of file +#include "jvm/Exception.hpp" +#include "jvm/VM.hpp" \ No newline at end of file diff --git a/src/event/PlayerEventHandler.cpp b/src/event/PlayerEventHandler.cpp index 8c3afb5..d11c843 100644 --- a/src/event/PlayerEventHandler.cpp +++ b/src/event/PlayerEventHandler.cpp @@ -15,95 +15,95 @@ rage::IPlayerHandler *PlayerEventHandler::GetPlayerHandler() { } PlayerEventHandler::PlayerEventHandler() { - playerEventClass = JVM::getClass(playerEventClassName); - playerJoinMethod = JVM::getStaticMethod(playerEventClass, "onPlayerJoin", "(I)V"); - playerCommandMethod = JVM::getStaticMethod(playerEventClass, "onPlayerCommand", "(ILjava/lang/String;)V"); - playerQuitMethod = JVM::getStaticMethod(playerEventClass, "onPlayerQuit", "(IILjava/lang/String;)V"); - playerSpawnMethod = JVM::getStaticMethod(playerEventClass, "onPlayerSpawn", "(I)V"); - playerChatMethod = JVM::getStaticMethod(playerEventClass, "onPlayerChat", "(ILjava/lang/String;)V"); - playerEnterVehicleMethod = JVM::getStaticMethod(playerEventClass, "onPlayerEnterVehicle", "(III)V"); - playerEnteredVehicleMethod = JVM::getStaticMethod(playerEventClass, "onPlayerEnteredVehicle", "(III)V"); - playerExitVehicleMethod = JVM::getStaticMethod(playerEventClass, "onPlayerExitVehicle", "(II)V"); - playerLeftVehicleMethod = JVM::getStaticMethod(playerEventClass, "onPlayerExitVehicle", "(II)V"); - playerDeathMethod = JVM::getStaticMethod(playerEventClass, "onPlayerDeath", "(III)V"); + playerEventClass = JVM::VM::getClass(playerEventClassName); + playerJoinMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerJoin", "(I)V"); + playerCommandMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerCommand", "(ILjava/lang/String;)V"); + playerQuitMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerQuit", "(IILjava/lang/String;)V"); + playerSpawnMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerSpawn", "(I)V"); + playerChatMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerChat", "(ILjava/lang/String;)V"); + playerEnterVehicleMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerEnterVehicle", "(III)V"); + playerEnteredVehicleMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerEnteredVehicle", "(III)V"); + playerExitVehicleMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerExitVehicle", "(II)V"); + playerLeftVehicleMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerExitVehicle", "(II)V"); + playerDeathMethod = JVM::VM::getStaticMethod(playerEventClass, "onPlayerDeath", "(III)V"); } void PlayerEventHandler::OnPlayerJoin(rage::IPlayer *player) { - jint playerId = TypeConverter::toJInt(player->GetId()); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerJoinMethod, playerId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerJoinMethod, playerId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerCommand(rage::IPlayer *player, const std::u16string &command) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jstring jCommand = TypeConverter::toJString(command); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerCommandMethod, playerId, jCommand); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jstring jCommand = JVM::Converter::toJString(command); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerCommandMethod, playerId, jCommand); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerQuit(rage::IPlayer *player, rage::exit_t exitType, const char *reason) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jint jExitType = TypeConverter::toJInt((int)exitType); - jstring jReason = TypeConverter::toJString(reason); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerQuitMethod, playerId, jExitType, jReason); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jint jExitType = JVM::Converter::toJInt((int)exitType); + jstring jReason = JVM::Converter::toJString(reason); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerQuitMethod, playerId, jExitType, jReason); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerSpawn(rage::IPlayer *player) { - jint playerId = TypeConverter::toJInt(player->GetId()); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerSpawnMethod, playerId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerSpawnMethod, playerId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerChat(rage::IPlayer *player, const std::u16string &text) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jstring jText = TypeConverter::toJString(text); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerChatMethod, playerId, jText); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jstring jText = JVM::Converter::toJString(text); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerChatMethod, playerId, jText); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerEnterVehicle(rage::IPlayer *player, rage::IVehicle *vehicle, uint8_t seatId) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jint vehicleId = TypeConverter::toJInt(vehicle->GetId()); - jint jSeatId = TypeConverter::toJInt(seatId); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerEnterVehicleMethod, playerId, vehicleId, jSeatId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jint vehicleId = JVM::Converter::toJInt(vehicle->GetId()); + jint jSeatId = JVM::Converter::toJInt(seatId); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerEnterVehicleMethod, playerId, vehicleId, jSeatId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerEnteredVehicle(rage::IPlayer *player, rage::IVehicle *vehicle, uint8_t seatId) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jint vehicleId = TypeConverter::toJInt(vehicle->GetId()); - jint jSeatId = TypeConverter::toJInt(seatId); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerEnteredVehicleMethod, playerId, vehicleId, jSeatId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jint vehicleId = JVM::Converter::toJInt(vehicle->GetId()); + jint jSeatId = JVM::Converter::toJInt(seatId); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerEnteredVehicleMethod, playerId, vehicleId, jSeatId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerExitVehicle(rage::IPlayer *player, rage::IVehicle *vehicle) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jint vehicleId = TypeConverter::toJInt(vehicle->GetId()); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerExitVehicleMethod, playerId, vehicleId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jint vehicleId = JVM::Converter::toJInt(vehicle->GetId()); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerExitVehicleMethod, playerId, vehicleId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerLeftVehicle(rage::IPlayer *player, rage::IVehicle *vehicle) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jint vehicleId = TypeConverter::toJInt(vehicle->GetId()); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerLeftVehicleMethod, playerId, vehicleId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jint vehicleId = JVM::Converter::toJInt(vehicle->GetId()); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerLeftVehicleMethod, playerId, vehicleId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerDeath(rage::IPlayer *player, rage::hash_t reason, rage::IPlayer *killer) { - jint playerId = TypeConverter::toJInt(player->GetId()); - jint jReason = TypeConverter::toJInt(reason); - jint killerId = TypeConverter::toJInt(killer->GetId()); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerDeathMethod, playerId, jReason, killerId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + jint jReason = JVM::Converter::toJInt(reason); + jint killerId = JVM::Converter::toJInt(killer->GetId()); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerDeathMethod, playerId, jReason, killerId); + JVM::VM::checkForException(); } void PlayerEventHandler::OnPlayerRemoteEvent(rage::IPlayer *player, const std::string &eventName, const rage::args_t &args) { - jint playerId = TypeConverter::toJInt(player->GetId()); - JVM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerRemoteEventMethod, playerId); - JVM::checkForException(); + jint playerId = JVM::Converter::toJInt(player->GetId()); + JVM::VM::getJNIEnv()->CallStaticVoidMethod(playerEventClass, playerRemoteEventMethod, playerId); + JVM::VM::checkForException(); } diff --git a/src/event/PlayerEventHandler.hpp b/src/event/PlayerEventHandler.hpp index 483d86e..c6b9ec4 100644 --- a/src/event/PlayerEventHandler.hpp +++ b/src/event/PlayerEventHandler.hpp @@ -13,8 +13,8 @@ #include #include #include "../sdk/rage.hpp" -#include "../jvm/JVM.hpp" -#include "../jvm/TypeConverter.hpp" +#include "../jvm/VM.hpp" +#include "../jvm/Converter.hpp" const std::string playerEventClassName = JVM_LAUNCHER_MAIN_PACKAGE_NAME + "player/PlayerEvents"; diff --git a/src/jvm/TypeConverter.cpp b/src/jvm/Converter.cpp similarity index 56% rename from src/jvm/TypeConverter.cpp rename to src/jvm/Converter.cpp index e0c9db5..7c06ee9 100644 --- a/src/jvm/TypeConverter.cpp +++ b/src/jvm/Converter.cpp @@ -8,14 +8,14 @@ * See the file COPYING included with this distribution for more information. */ -#include "TypeConverter.hpp" +#include "Converter.hpp" -std::u16string TypeConverter::fromJStringU16(jstring input) { - const char *jStringMessage = JVM::getJNIEnv()->GetStringUTFChars(input, nullptr); +std::u16string JVM::Converter::toU16string(jstring input) { + const char *jStringMessage = VM::getJNIEnv()->GetStringUTFChars(input, nullptr); std::u16string wstr = u""; char16_t c16str[3] = u"\0"; mbstate_t mbs; - for (const auto& it: fromJString(input)){ + for (const auto& it: toString(input)){ memset(&mbs, 0, sizeof (mbs)); memmove(c16str, u"\0\0\0", 3); mbrtoc16(c16str, &it, 3, &mbs); @@ -24,32 +24,32 @@ std::u16string TypeConverter::fromJStringU16(jstring input) { return wstr; } -std::string TypeConverter::fromJString(jstring input) { - const char *jStringMessage = JVM::getJNIEnv()->GetStringUTFChars(input, nullptr); +std::string JVM::Converter::toString(jstring input) { + const char *jStringMessage = VM::getJNIEnv()->GetStringUTFChars(input, nullptr); std::string result(jStringMessage); return result; } -jstring TypeConverter::toJString(std::u16string input) { +jstring JVM::Converter::toJString(std::u16string input) { std::wstring_convert, int16_t> convert; auto p = reinterpret_cast(input.data()); std::string convertedString = convert.to_bytes(p, p + input.size()); - return JVM::getJNIEnv()->NewStringUTF(convertedString.c_str()); + return VM::getJNIEnv()->NewStringUTF(convertedString.c_str()); } -jstring TypeConverter::toJString(std::string string) { - return JVM::getJNIEnv()->NewStringUTF(string.c_str()); +jstring JVM::Converter::toJString(std::string string) { + return VM::getJNIEnv()->NewStringUTF(string.c_str()); } -jstring TypeConverter::toJString(const char *input) { - return JVM::getJNIEnv()->NewStringUTF(input); +jstring JVM::Converter::toJString(const char *input) { + return VM::getJNIEnv()->NewStringUTF(input); } -int TypeConverter::fromJInt(jint input) { +int JVM::Converter::toInt(jint input) { return (int) input; } -jint TypeConverter::toJInt(int input) { +jint JVM::Converter::toJInt(int input) { return (jint) input; } diff --git a/src/jvm/TypeConverter.hpp b/src/jvm/Converter.hpp similarity index 56% rename from src/jvm/TypeConverter.hpp rename to src/jvm/Converter.hpp index c5b895b..bbac53b 100644 --- a/src/jvm/TypeConverter.hpp +++ b/src/jvm/Converter.hpp @@ -19,16 +19,25 @@ #include #include -#include "JVM.hpp" - -class TypeConverter { -public: - static std::u16string fromJStringU16(jstring input); - static std::string fromJString(jstring input); - static jstring toJString(std::u16string input); - static jstring toJString(const char* input); - static jstring toJString(std::string string); - static jint toJInt(int input); - static int fromJInt(jint input); -}; +#include "VM.hpp" +namespace JVM { + + class Converter { + public: + static std::u16string toU16string(jstring input); + + static std::string toString(jstring input); + + static jstring toJString(std::u16string input); + + static jstring toJString(const char *input); + + static jstring toJString(std::string string); + + static jint toJInt(int input); + + static int toInt(jint input); + }; + +} diff --git a/src/jvm/Exception.cpp b/src/jvm/Exception.cpp new file mode 100644 index 0000000..d73b536 --- /dev/null +++ b/src/jvm/Exception.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2017 Noxaro aka Fabian Jungwirth + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * See the file COPYING included with this distribution for more information. + */ + +#include "Exception.hpp" + +void JVM::Exception::throwNotImplementedException(std::string reason) { + jclass clazz = VM::getJNIEnv()->FindClass("mp/rage/plugin/java/api/exception/NotImplementedException"); + VM::getJNIEnv()->ThrowNew(clazz, reason.c_str()); +} + +void JVM::Exception::throwPlayerNotFoundException(int playerId) { + jclass clazz = VM::getJNIEnv()->FindClass("mp/rage/plugin/java/api/exception/PlayerNotFoundException"); + VM::getJNIEnv()->ThrowNew(clazz, std::to_string(playerId).c_str()); +} \ No newline at end of file diff --git a/src/jvm/Exception.hpp b/src/jvm/Exception.hpp new file mode 100644 index 0000000..8abf3ca --- /dev/null +++ b/src/jvm/Exception.hpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2017 Noxaro aka Fabian Jungwirth + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * See the file COPYING included with this distribution for more information. + */ + +#pragma once + +#include +#include "VM.hpp" +#include +#include + +namespace JVM { + class Exception { + public: + static void throwNotImplementedException(std::string reason); + + static void throwPlayerNotFoundException(int playerId); + }; + + +} + +class JNIException { +public: + explicit JNIException(std::string reason) : pMessage(reason) {} + + std::string what() const { return pMessage; } + +private: + std::string pMessage; +}; + +class ClassNotFoundException : public JNIException { +public: + explicit ClassNotFoundException(const std::string &reason) : JNIException(reason) {} +}; + +class MethodNotFoundException : public JNIException { +public: + explicit MethodNotFoundException(const std::string &reason) : JNIException(reason) {} +}; \ No newline at end of file diff --git a/src/jvm/JVM.hpp b/src/jvm/JVM.hpp deleted file mode 100644 index bc614e7..0000000 --- a/src/jvm/JVM.hpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2017 Noxaro aka Fabian Jungwirth - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3.0 of the License, or (at your option) any later version. - * See the file COPYING included with this distribution for more information. - */ - -#pragma once - -#include -#include -#include "jni.h" -#include "JVMException.hpp" - - -const std::string JVM_LAUNCHER_MAIN_PACKAGE_NAME = "mp/rage/plugin/java/launcher/"; -const std::string JVM_LAUNCHER_CLASS_NAME = JVM_LAUNCHER_MAIN_PACKAGE_NAME + "Launcher"; -const std::string JVM_LAUNCHER_METHOD_NAME = "main"; - -class JVM { -public: - static bool createJavaVirtualMachine(); - - static JNIEnv *getJNIEnv(); - - static JavaVM *getJVM(); - - static bool checkForException(); - - static jclass getClass(std::string className); - - static jmethodID getStaticMethod(jclass jClass, std::string methodName, std::string methodSignature); - - static jobject createVector3(float x, float y, float z); - -// static jobject createPlayerProperty() @Todo - - static jobject createPlayerHeadBlend(int shape, int skin, float shapeMix, float skinMix, float thirdMix); - - static jint throwNotImplementedException(std::string reason); - -private: - static bool findAndExecuteMain(); - - static bool createJVM(); -}; \ No newline at end of file diff --git a/src/jvm/JVMException.hpp b/src/jvm/JVMException.hpp deleted file mode 100644 index 72c645a..0000000 --- a/src/jvm/JVMException.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2017 Noxaro aka Fabian Jungwirth - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3.0 of the License, or (at your option) any later version. - * See the file COPYING included with this distribution for more information. - */ - -#pragma once - -#include -#include - -class JVMException { -public: - explicit JVMException(std::string reason); - - std::string what() const { return pMessage; } - -private: - std::string pMessage; -}; - -class ClassNotFoundException : public JVMException { -public: - explicit ClassNotFoundException(const std::string &reason); -}; - -class MethodNotFoundException : public JVMException { -public: - explicit MethodNotFoundException(const std::string &reason); -}; \ No newline at end of file diff --git a/src/jvm/Object.cpp b/src/jvm/Object.cpp new file mode 100644 index 0000000..cae048e --- /dev/null +++ b/src/jvm/Object.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2017 Noxaro aka Fabian Jungwirth + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * See the file COPYING included with this distribution for more information. + */ + +#include "Object.hpp" + +jobject JVM::Object::vector3(float x, float y, float z) { + jclass clazz = VM::getJNIEnv()->FindClass("mp/rage/plugin/java/api/vector/Vector3"); + jmethodID methodId = VM::getJNIEnv()->GetMethodID(clazz, "", "(FFF)V"); + return VM::getJNIEnv()->NewObject(clazz, methodId, x, y, z); +} + +jobject JVM::Object::playerHeadBlend(int shape, int skin, float shapeMix, float skinMix, float thirdMix) { + jclass clazz = VM::getJNIEnv()->FindClass("mp/rage/plugin/java/api/player/PlayerHeadBlend"); + jmethodID methodId = VM::getJNIEnv()->GetMethodID(clazz, "", "(IIFFF)V"); + return VM::getJNIEnv()->NewObject(clazz, methodId, shape, skin, shapeMix, skinMix, thirdMix); +} diff --git a/src/jvm/JVMException.cpp b/src/jvm/Object.hpp similarity index 57% rename from src/jvm/JVMException.cpp rename to src/jvm/Object.hpp index 2b70a0a..67079e2 100644 --- a/src/jvm/JVMException.cpp +++ b/src/jvm/Object.hpp @@ -8,10 +8,15 @@ * See the file COPYING included with this distribution for more information. */ -#include "JVMException.hpp" +#pragma once -JVMException::JVMException(std::string reason): pMessage(reason) {} +#include +#include "VM.hpp" -ClassNotFoundException::ClassNotFoundException(const std::string &reason) : JVMException(reason) {} - -MethodNotFoundException::MethodNotFoundException(const std::string &reason) : JVMException(reason) {} +namespace JVM { + class Object { + public: + static jobject vector3(float x, float y, float z); + static jobject playerHeadBlend(int shape, int skin, float shapeMix, float skinMix, float thirdMix); + }; +} \ No newline at end of file diff --git a/src/jvm/JVM.cpp b/src/jvm/VM.cpp similarity index 70% rename from src/jvm/JVM.cpp rename to src/jvm/VM.cpp index 3871c0c..c49bebd 100644 --- a/src/jvm/JVM.cpp +++ b/src/jvm/VM.cpp @@ -8,34 +8,34 @@ * See the file COPYING included with this distribution for more information. */ -#include "JVM.hpp" +#include "VM.hpp" static JNIEnv *jniEnv = nullptr; static JavaVM *javaVM = nullptr; -JNIEnv *JVM::getJNIEnv() { +JNIEnv *JVM::VM::getJNIEnv() { return jniEnv; } -JavaVM *JVM::getJVM() { +JavaVM *JVM::VM::getJVM() { return javaVM; } -bool JVM::createJavaVirtualMachine() { +bool JVM::VM::create() { std::cout << "Creating Java Virtual Machine ... "; - if (!JVM::createJVM()) { + if (!VM::createJVM()) { std::cout << "Failed to initialize Java Virtual Machine" << std::endl; return false; } - if (!JVM::findAndExecuteMain()) { + if (!VM::findAndExecuteMain()) { std::cout << "Failed to execute Launcher" << std::endl; return false; } return true; } -bool JVM::checkForException() { +bool JVM::VM::checkForException() { if (jniEnv->ExceptionCheck()) { jthrowable throwable = jniEnv->ExceptionOccurred(); jclass throwableCls = jniEnv->GetObjectClass(throwable); @@ -48,15 +48,15 @@ bool JVM::checkForException() { return false; } -bool JVM::findAndExecuteMain() { +bool JVM::VM::findAndExecuteMain() { jclass jClass = jniEnv->FindClass(JVM_LAUNCHER_CLASS_NAME.c_str()); if (jClass == nullptr) { - std::cout << std::endl << "Couldn't find expected JVM main class" << std::endl; + std::cout << std::endl << "Couldn't find expected VM main class" << std::endl; return false; } jmethodID methodId = jniEnv->GetStaticMethodID(jClass, JVM_LAUNCHER_METHOD_NAME.c_str(), "()V"); if (methodId == nullptr) { - std::cout << std::endl << "Couldn't find expected JVM main method" << std::endl; + std::cout << std::endl << "Couldn't find expected VM main method" << std::endl; return false; } jniEnv->CallStaticVoidMethod(jClass, methodId); @@ -66,7 +66,7 @@ bool JVM::findAndExecuteMain() { return true; } -bool JVM::createJVM() { +bool JVM::VM::createJVM() { JavaVMInitArgs vm_args; JavaVMOption options[2]; @@ -110,7 +110,7 @@ bool JVM::createJVM() { } } -jclass JVM::getClass(std::string className) { +jclass JVM::VM::getClass(std::string className) { jclass jClass = jniEnv->FindClass(className.c_str()); if (jClass == nullptr) { throw ClassNotFoundException(className + " not found"); @@ -118,27 +118,10 @@ jclass JVM::getClass(std::string className) { return jClass; } -jmethodID JVM::getStaticMethod(jclass jClass, std::string methodName, std::string methodSignature) { +jmethodID JVM::VM::getStaticMethod(jclass jClass, std::string methodName, std::string methodSignature) { jmethodID methodId = jniEnv->GetStaticMethodID(jClass, methodName.c_str(), methodSignature.c_str()); if (methodId == nullptr) { throw MethodNotFoundException(methodName + " not found"); } return methodId; -} - -jobject JVM::createVector3(float x, float y, float z) { - jclass clazz = jniEnv->FindClass("mp/rage/plugin/java/api/vector/Vector3"); - jmethodID methodId = jniEnv->GetMethodID(clazz, "", "(FFF)V"); - return jniEnv->NewObject(clazz, methodId, x, y, z); -} - -jobject JVM::createPlayerHeadBlend(int shape, int skin, float shapeMix, float skinMix, float thirdMix) { - jclass clazz = jniEnv->FindClass("mp/rage/plugin/java/api/player/PlayerHeadBlend"); - jmethodID methodId = jniEnv->GetMethodID(clazz, "", "(IIFFF)V"); - return jniEnv->NewObject(clazz, methodId, shape, skin, shapeMix, skinMix, thirdMix); -} - -jint JVM::throwNotImplementedException(std::string reason) { - jclass clazz = jniEnv->FindClass("mp/rage/plugin/java/api/exception/NotImplementedException"); - return jniEnv->ThrowNew(clazz, reason.c_str()); } \ No newline at end of file diff --git a/src/jvm/VM.hpp b/src/jvm/VM.hpp new file mode 100644 index 0000000..fde2768 --- /dev/null +++ b/src/jvm/VM.hpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017 Noxaro aka Fabian Jungwirth + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * See the file COPYING included with this distribution for more information. + */ + +#pragma once + +#include +#include +#include "jni.h" +#include "Exception.hpp" + + +const std::string JVM_LAUNCHER_MAIN_PACKAGE_NAME = "mp/rage/plugin/java/launcher/"; +const std::string JVM_LAUNCHER_CLASS_NAME = JVM_LAUNCHER_MAIN_PACKAGE_NAME + "Launcher"; +const std::string JVM_LAUNCHER_METHOD_NAME = "main"; + +namespace JVM { + + class VM { + public: + static bool create(); + + static JNIEnv *getJNIEnv(); + + static JavaVM *getJVM(); + + static bool checkForException(); + + static jclass getClass(std::string className); + + static jmethodID getStaticMethod(jclass jClass, std::string methodName, std::string methodSignature); + + private: + static bool findAndExecuteMain(); + + static bool createJVM(); + }; +} \ No newline at end of file diff --git a/src/jni/BlipNative.hpp b/src/native/BlipNative.hpp similarity index 100% rename from src/jni/BlipNative.hpp rename to src/native/BlipNative.hpp diff --git a/src/jni/CheckpointNative.hpp b/src/native/CheckpointNative.hpp similarity index 100% rename from src/jni/CheckpointNative.hpp rename to src/native/CheckpointNative.hpp diff --git a/src/jni/ColshapeNative.hpp b/src/native/ColshapeNative.hpp similarity index 100% rename from src/jni/ColshapeNative.hpp rename to src/native/ColshapeNative.hpp diff --git a/src/jni/MarkerNative.hpp b/src/native/MarkerNative.hpp similarity index 100% rename from src/jni/MarkerNative.hpp rename to src/native/MarkerNative.hpp diff --git a/src/jni/ObjectNative.hpp b/src/native/ObjectNative.hpp similarity index 100% rename from src/jni/ObjectNative.hpp rename to src/native/ObjectNative.hpp diff --git a/src/jni/PickupNative.hpp b/src/native/PickupNative.hpp similarity index 100% rename from src/jni/PickupNative.hpp rename to src/native/PickupNative.hpp diff --git a/src/jni/PlayerNative.cpp b/src/native/PlayerNative.cpp similarity index 66% rename from src/jni/PlayerNative.cpp rename to src/native/PlayerNative.cpp index 9446157..87be757 100644 --- a/src/jni/PlayerNative.cpp +++ b/src/native/PlayerNative.cpp @@ -12,20 +12,21 @@ #include #include "../RagePlugin.hpp" #include "../rage/Player.hpp" +#include "../jvm/Object.hpp" jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getType(JNIEnv *, jclass, jint playerId) { return (jint)rage::entity_t::Player; } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_destroy(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->Destroy(); } } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getDimension(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetDimension(); } @@ -33,23 +34,23 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getDimension(JNIEnv * } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setDimension(JNIEnv *, jclass, jint playerId, jint dimension) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetDimension((uint32_t)dimension); } } jobject Java_mp_rage_plugin_java_launcher_player_PlayerNative_getRotation(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::vector3 position = player->GetRotation(); - return JVM::createVector3(position.x, position.y, position.z); + return JVM::Object::vector3(position.x, position.y, position.z); } return nullptr; } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setRotation(JNIEnv *, jclass, jint playerId, jfloat x, jfloat y, jfloat z) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::vector3 position = {x, y, z}; player->SetRotation(position); @@ -57,7 +58,7 @@ void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setRotation(JNIEnv *, } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getModel(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetModel(); } @@ -65,23 +66,23 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getModel(JNIEnv *, jc } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setModel(JNIEnv *, jclass, jint playerId, jint model) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetModel((uint32_t)model); } } jobject Java_mp_rage_plugin_java_launcher_player_PlayerNative_getVelocity(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::vector3 velocity = player->GetVelocity(); - return JVM::createVector3(velocity.x, velocity.y, velocity.x); + return JVM::Object::vector3(velocity.x, velocity.y, velocity.x); } return nullptr; } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getAlpha(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetAlpha(); } @@ -89,42 +90,42 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getAlpha(JNIEnv *, jc } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setAlpha(JNIEnv *, jclass, jint playerId, jint alpha) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetAlpha((uint8_t)alpha); } } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_kick(JNIEnv *, jclass, jint playerId, jstring reason) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { - player->Kick(TypeConverter::fromJString(reason).c_str()); + player->Kick(JVM::Converter::toString(reason).c_str()); } } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_ban(JNIEnv *, jclass, jint playerId, jstring reason) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { - player->Ban(TypeConverter::fromJString(reason).c_str()); + player->Ban(JVM::Converter::toString(reason).c_str()); } } -void Java_mp_rage_plugin_java_launcher_player_PlayerNative_outputChatBox(JNIEnv *, jclass, jint playerId, jstring message) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); +void Java_mp_rage_plugin_java_launcher_player_PlayerNative_outputChatBox(JNIEnv * env, jclass, jint playerId, jstring message) { + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { - player->OutputChatBox(TypeConverter::fromJStringU16(message)); + player->OutputChatBox(JVM::Converter::toU16string(message)); } } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_notify(JNIEnv *, jclass, jint playerId, jstring message) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { - player->Notify(TypeConverter::fromJStringU16(message)); + player->Notify(JVM::Converter::toU16string(message)); } } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_spawn(JNIEnv * env, jclass, jint playerId, jfloat x, jfloat y, jfloat z, jfloat heading) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::vector3 position = {x, y, z}; player->Spawn(position, heading); @@ -132,53 +133,53 @@ void Java_mp_rage_plugin_java_launcher_player_PlayerNative_spawn(JNIEnv * env, j } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_playAnimation(JNIEnv *, jclass, jint playerId, jstring dict, jstring name, jfloat speed, jint flags) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { - std::string convertedDict = TypeConverter::fromJString(dict); - std::string convertedName = TypeConverter::fromJString(name); + std::string convertedDict = JVM::Converter::toString(dict); + std::string convertedName = JVM::Converter::toString(name); player->PlayAnimation(convertedDict, convertedName, speed, flags); } } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_playScenario(JNIEnv *, jclass, jint playerId, jstring name) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { - std::string convertedName = TypeConverter::fromJString(name); + std::string convertedName = JVM::Converter::toString(name); player->PlayScenario(convertedName); } } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_stopAnimation(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->StopAnimation(); } } jobject Java_mp_rage_plugin_java_launcher_player_PlayerNative_getClothes(JNIEnv *, jclass, jint playerId, jint componentId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::clothData_t clothes = player->GetClothes((uint8_t)componentId); } - JVM::throwNotImplementedException("Player.getClothes is currently not implemented"); + JVM::Exception::throwNotImplementedException("Player.getClothes is currently not implemented"); return nullptr; } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setClothes(JNIEnv *, jclass, jint, jint, jobject) { - JVM::throwNotImplementedException("Player.setClothes is currently not implemented"); + JVM::Exception::throwNotImplementedException("Player.setClothes is currently not implemented"); } jobject Java_mp_rage_plugin_java_launcher_player_PlayerNative_getProp(JNIEnv *, jclass, jint playerId, jint propId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::propData_t props = player->GetProp((uint8_t)propId); } - JVM::throwNotImplementedException("Player.getProp is currently not implemented"); + JVM::Exception::throwNotImplementedException("Player.getProp is currently not implemented"); return nullptr; } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setProp(JNIEnv *, jclass, jint playerId, jint propId, jobject) { - JVM::throwNotImplementedException("Player.getProp is currently not implemented"); + JVM::Exception::throwNotImplementedException("Player.getProp is currently not implemented"); } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_eval(JNIEnv *, jclass, jint, jstring) { @@ -206,7 +207,7 @@ void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setHeading(JNIEnv *, } jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getMoveSpeed(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return player->GetMoveSpeed(); } @@ -214,7 +215,7 @@ jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getMoveSpeed(JNIEnv } jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHealth(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return player->GetHealth(); } @@ -222,14 +223,14 @@ jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHealth(JNIEnv *, } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setHealth(JNIEnv *, jclass, jint playerId, jfloat health) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetHealth(health); } } jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getArmor(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return player->GetArmour(); } @@ -237,23 +238,23 @@ jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getArmor(JNIEnv *, } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setArmor(JNIEnv *, jclass, jint playerId, jfloat armor) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetArmour(armor); } } jobject Java_mp_rage_plugin_java_launcher_player_PlayerNative_getAimingAt(JNIEnv* env, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::vector3 pos = player->GetAimingAt(); - return JVM::createVector3(pos.x, pos.y, pos.z); + return JVM::Object::vector3(pos.x, pos.y, pos.z); } return nullptr; } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getPing(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return player->GetPing(); } @@ -261,25 +262,25 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getPing(JNIEnv *, jcl } jstring Java_mp_rage_plugin_java_launcher_player_PlayerNative_getKickReason(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { std::string name = player->GetName(); - return TypeConverter::toJString(name); + return JVM::Converter::toJString(name); } return nullptr; } jstring Java_mp_rage_plugin_java_launcher_player_PlayerNative_getIp(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { std::string ip = player->GetIp(); - return TypeConverter::toJString(ip); + return JVM::Converter::toJString(ip); } return nullptr; } jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isJumping(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jboolean)player->IsJumping(); } @@ -287,7 +288,7 @@ jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isJumping(JNIEnv } jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isInCover(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jboolean)player->IsInCover(); } @@ -295,7 +296,7 @@ jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isInCover(JNIEnv } jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isEnteringVehicle(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jboolean)player->IsEnteringVehicle(); } @@ -303,7 +304,7 @@ jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isEnteringVehicle } jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isLeavingVehicle(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jboolean)player->IsLeavingVehicle(); } @@ -311,7 +312,7 @@ jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isLeavingVehicle( } jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isClimbing(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jboolean)player->IsClimbing(); } @@ -319,23 +320,23 @@ jboolean Java_mp_rage_plugin_java_launcher_player_PlayerNative_isClimbing(JNIEnv } jstring Java_mp_rage_plugin_java_launcher_player_PlayerNative_getActionString(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { std::string action = player->GetActionString(); - return TypeConverter::toJString(action); + return JVM::Converter::toJString(action); } return nullptr; } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_removeFromVehicle(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->RemoveFromVehicle(); } } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getSeat(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetSeat(); } @@ -343,7 +344,7 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getSeat(JNIEnv *, jcl } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getEyeColor(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetEyeColour(); } @@ -351,14 +352,14 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getEyeColor(JNIEnv *, } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setEyeColor(JNIEnv *, jclass, jint playerId, jint eyeColor) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetEyeColour((uint8_t)eyeColor); } } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHairColor(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetHairColour(); } @@ -366,7 +367,7 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHairColor(JNIEnv * } jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHairHighlightColor(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jint)player->GetHairHighlightColour(); } @@ -374,14 +375,14 @@ jint Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHairHighlightColor } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setHairColor(JNIEnv *, jclass, jint playerId, jint hairColor, jint hairHighlightColor) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetHairColour((uint8_t)hairColor, (uint8_t)hairHighlightColor); } } jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getFaceFeature(JNIEnv *, jclass, jint playerId, jint facePartId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { return (jfloat)player->GetFaceFeature((uint8_t)facePartId); } @@ -389,18 +390,18 @@ jfloat Java_mp_rage_plugin_java_launcher_player_PlayerNative_getFaceFeature(JNIE } void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setFaceFeature(JNIEnv *, jclass, jint playerId, jint faceId, jfloat scale) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { player->SetFaceFeature((uint8_t)faceId, (uint8_t)scale); } } jobject Java_mp_rage_plugin_java_launcher_player_PlayerNative_getHeadBlend(JNIEnv *, jclass, jint playerId) { - rage::IPlayer* player = Player::getPlayerById(TypeConverter::fromJInt(playerId)); + rage::IPlayer* player = Rage::Player::getPlayerById(JVM::Converter::toInt(playerId)); if(player) { rage::headBlend_t headBlend = player->GetHeadBlend(); // @TODO -// JVM::createPlayerHeadBlend(headBlend.m_shape, headBlend.m_skin, headBlend.m_shapeMix, headBlend.m_skinMix, headBlend.m_thirdMix); +// VM::playerHeadBlend(headBlend.m_shape, headBlend.m_skin, headBlend.m_shapeMix, headBlend.m_skinMix, headBlend.m_thirdMix); } return nullptr; } @@ -409,8 +410,7 @@ void Java_mp_rage_plugin_java_launcher_player_PlayerNative_setHeadBlend(JNIEnv * } -void -Java_mp_rage_plugin_java_launcher_player_PlayerNative_updateHeadBlend(JNIEnv *, jclass, jint, jfloat, jfloat, jfloat) { +void Java_mp_rage_plugin_java_launcher_player_PlayerNative_updateHeadBlend(JNIEnv *, jclass, jint, jfloat, jfloat, jfloat) { } diff --git a/src/jni/PlayerNative.hpp b/src/native/PlayerNative.hpp similarity index 100% rename from src/jni/PlayerNative.hpp rename to src/native/PlayerNative.hpp diff --git a/src/jni/VehicleNative.hpp b/src/native/VehicleNative.hpp similarity index 100% rename from src/jni/VehicleNative.hpp rename to src/native/VehicleNative.hpp diff --git a/src/rage/Player.cpp b/src/rage/Player.cpp index 6ab8a5f..6745839 100644 --- a/src/rage/Player.cpp +++ b/src/rage/Player.cpp @@ -10,11 +10,19 @@ #include "Player.hpp" -rage::IPlayer* Player::getPlayerById(int playerId) { +/** + * Get's a player from the player pool. + * If the pool returns null for the player a JVM exception will be thrown + */ +rage::IPlayer* Rage::Player::getPlayerById(int playerId) { rage::IMultiplayer *mp = RageJavaCore::getInstance().getMultiPlayer(); if(mp == nullptr) { return nullptr; } - rage::IPlayer *player = mp->GetPlayerPool().GetAt((rage::entityId_t)TypeConverter::fromJInt(playerId)); - return player; + rage::IPlayer *player = mp->GetPlayerPool().GetAt((rage::entityId_t) JVM::Converter::toInt(playerId)); + if(player) { + return player; + } + JVM::Exception::throwPlayerNotFoundException(playerId); + return nullptr; } diff --git a/src/rage/Player.hpp b/src/rage/Player.hpp index 0c11893..1536184 100644 --- a/src/rage/Player.hpp +++ b/src/rage/Player.hpp @@ -14,7 +14,9 @@ #include "../sdk/Entities.hpp" #include "../RageJavaCore.hpp" -class Player { -public: - static rage::IPlayer* getPlayerById(int playerId); -}; +namespace Rage { + class Player { + public: + static rage::IPlayer *getPlayerById(int playerId); + }; +}