summaryrefslogtreecommitdiff
path: root/Source/Android/jni/NetPlay/Netplay.cpp
blob: 61c2e77b9f570e19f0a0f3ae8557ab64694dc3ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2003 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <memory>
#include <string>
#include <vector>

#include <jni.h>

#include "Common/CommonTypes.h"
#include "Core/Boot/Boot.h"
#include "Core/Config/NetplaySettings.h"
#include "Core/NetPlayClient.h"
#include "UICommon/GameFile.h"
#include "UICommon/GameFileCache.h"

#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"
#include "jni/NetPlay/NetPlayUICallbacks.h"

static NetPlay::NetPlayUICallbacks* GetUICallbacksPointer(JNIEnv* env, jobject obj)
{
  return reinterpret_cast<NetPlay::NetPlayUICallbacks*>(
      env->GetLongField(obj, IDCache::GetNetPlayUICallbacksPointer()));
}

static NetPlay::NetPlayClient* GetClientPointer(JNIEnv* env, jobject obj)
{
  return reinterpret_cast<NetPlay::NetPlayClient*>(
      env->GetLongField(obj, IDCache::GetNetPlayClientPointer()));
}

extern "C" {

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeSendMessage(JNIEnv* env, jobject obj,
                                                                          jstring jmessage)
{
  if (auto* client = GetClientPointer(env, obj))
    client->SendChatMessage(GetJString(env, jmessage));
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeAdjustPadBufferSize(JNIEnv* env,
                                                                                   jobject obj,
                                                                                   jint buffer)
{
  if (auto* client = GetClientPointer(env, obj))
    client->AdjustPadBufferSize(static_cast<u32>(buffer));
}

JNIEXPORT jlong JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeCreateUICallbacks(JNIEnv* env,
                                                                                 jobject obj)
{
  jobject jgame_file_cache = env->GetStaticObjectField(
      IDCache::GetGameFileCacheManagerClass(), IDCache::GetGameFileCacheManagerInstance());
  auto* game_file_cache = reinterpret_cast<UICommon::GameFileCache*>(
      env->GetLongField(jgame_file_cache, IDCache::GetGameFileCachePointer()));

  std::vector<std::shared_ptr<const UICommon::GameFile>> games;
  game_file_cache->ForEach(
      [&games](const std::shared_ptr<const UICommon::GameFile>& game) { games.push_back(game); });

  return reinterpret_cast<jlong>(new NetPlay::NetPlayUICallbacks(obj, std::move(games)));
}

JNIEXPORT jlong JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeJoin(JNIEnv* env, jobject obj)
{
  auto* ui = GetUICallbacksPointer(env, obj);

  const std::string traversal_host = Config::Get(Config::NETPLAY_TRAVERSAL_SERVER);
  const u16 traversal_port = Config::Get(Config::NETPLAY_TRAVERSAL_PORT);
  const std::string nickname = Config::Get(Config::NETPLAY_NICKNAME);

  const std::string traversal_choice = Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE);
  const bool is_traversal = traversal_choice == "traversal";
  const std::string host_ip = is_traversal ? Config::Get(Config::NETPLAY_HOST_CODE) :
                                             Config::Get(Config::NETPLAY_ADDRESS);
  const u16 host_port = Config::Get(Config::NETPLAY_CONNECT_PORT);

  auto* client = new NetPlay::NetPlayClient(
      host_ip, host_port, ui, nickname,
      NetPlay::NetTraversalConfig{is_traversal, traversal_host, traversal_port});

  if (!client->IsConnected())
  {
    delete client;
    return 0;
  }

  return reinterpret_cast<jlong>(client);
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeReleaseUICallbacks(JNIEnv*,
                                                                                  jobject,
                                                                                  jlong pointer)
{
  delete reinterpret_cast<NetPlay::NetPlayUICallbacks*>(pointer);
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeReleaseBootSessionData(JNIEnv*,
                                                                                      jobject,
                                                                                      jlong pointer)
{
  delete reinterpret_cast<BootSessionData*>(pointer);
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_NetplaySession_nativeReleaseClient(JNIEnv*, jobject,
                                                                             jlong pointer)
{
  delete reinterpret_cast<NetPlay::NetPlayClient*>(pointer);
}

}  // extern "C"