Skip to content

Commit

Permalink
Refactor code base
Browse files Browse the repository at this point in the history
add namespace JVM
add namespace Rage
Split up old JVM Class in VM, Object and Exception
  • Loading branch information
Fabian Jungwirth committed Sep 19, 2017
1 parent 009cca5 commit b867365
Show file tree
Hide file tree
Showing 27 changed files with 340 additions and 298 deletions.
17 changes: 1 addition & 16 deletions src/RageJavaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/RageJavaCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,9 +33,6 @@ class RageJavaCore {
bool initialize(rage::IMultiplayer *mp);

private:
bool initializeJVM();
bool initializeEventHandlers();

rage::IMultiplayer *mp;
};

1 change: 0 additions & 1 deletion src/RagePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/RagePlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
#include <windows.h>
#endif
#include <iostream>
#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"
#include "jvm/Exception.hpp"
#include "jvm/VM.hpp"
112 changes: 56 additions & 56 deletions src/event/PlayerEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


4 changes: 2 additions & 2 deletions src/event/PlayerEventHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <iostream>
#include <jni.h>
#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";

Expand Down
28 changes: 14 additions & 14 deletions src/jvm/TypeConverter.cpp → src/jvm/Converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<std::codecvt_utf8_utf16<int16_t>, int16_t> convert;
auto p = reinterpret_cast<const int16_t *>(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;
}

Expand Down
33 changes: 21 additions & 12 deletions src/jvm/TypeConverter.hpp → src/jvm/Converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@
#include <cstring>

#include <jni.h>
#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);
};

}
21 changes: 21 additions & 0 deletions src/jvm/Exception.cpp
Original file line number Diff line number Diff line change
@@ -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());
}
Loading

0 comments on commit b867365

Please sign in to comment.