Skip to content

Commit 1d4abc7

Browse files
committed
Add ChatPlex web service
1 parent 1a0d58d commit 1d4abc7

File tree

7 files changed

+459
-4
lines changed

7 files changed

+459
-4
lines changed

shared/CP_SDK/CPConfig.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "Config/JsonConfig.hpp"
4+
#include <string>
45

56
namespace CP_SDK {
67

@@ -10,8 +11,9 @@ namespace CP_SDK {
1011
CP_SDK_CONFIG_JSONCONFIG_INSTANCE_DECL(CPConfig);
1112

1213
public:
13-
bool FirstRun = true;
14-
bool FirstChatServiceRun = true;
14+
bool FirstRun = true;
15+
bool FirstChatServiceRun = true;
16+
std::u16string ChatPlexServiceToken = u"";
1517

1618
protected:
1719
/// @brief Reset config to default

shared/CP_SDK/ChatPlexService.hpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#pragma once
2+
3+
#include "Network/JsonRPCResult.hpp"
4+
#include "Network/JsonRPCClient.hpp"
5+
#include "Utils/Event.hpp"
6+
#include "Utils/Il2cpp.hpp"
7+
#include "Utils/Delegate.hpp"
8+
9+
#include <beatsaber-hook/shared/utils/il2cpp-utils.hpp>
10+
#include <string>
11+
#include <string_view>
12+
13+
namespace CP_SDK {
14+
15+
namespace _u
16+
{
17+
using namespace il2cpp_utils;
18+
using namespace System;
19+
using namespace UnityEngine;
20+
}
21+
22+
namespace _v
23+
{
24+
using namespace CP_SDK::Network;
25+
using namespace CP_SDK::Utils;
26+
}
27+
28+
/// @brief ChatPlexService
29+
class CP_SDK_EXPORT ChatPlexService
30+
{
31+
CP_SDK_NO_DEF_CTORS(ChatPlexService);
32+
33+
public:
34+
enum class EState
35+
{
36+
Disconnected,
37+
LinkRequest,
38+
LinkWait,
39+
Connecting,
40+
41+
Connected,
42+
43+
Error
44+
};
45+
46+
private:
47+
static bool m_ThreadCondition;
48+
static _u::il2cpp_aware_thread* m_Thread;
49+
static EState m_State;
50+
static _v::WebClientCore::Ptr m_WebClientCore;
51+
static _v::JsonRPCClient::Ptr m_JsonRPCClient;
52+
static std::u16string m_LinkRequestID;
53+
static std::u16string m_LinkCode;
54+
static std::u16string m_LastError;
55+
static std::u16string m_ActiveSubscription;
56+
static std::vector<std::u16string> m_UnlockedFeatures;
57+
static std::queue<_v::Action<>> m_OnTokenReadyQueue;
58+
static std::mutex m_OnTokenReadyQueueMutex;
59+
static std::u16string m_DeviceName;
60+
61+
public:
62+
static const EState State();
63+
static const std::u16string_view Token();
64+
static const std::u16string_view LinkCode();
65+
static const std::u16string_view LastError();
66+
static const std::u16string_view ActiveSubscription();
67+
static const std::vector<const std::u16string>& UnlockedFeatures();
68+
69+
static _v::Event<EState, EState> StateChanged;
70+
71+
public:
72+
/// @brief Init the service
73+
static void Init();
74+
/// @brief Release the service
75+
static void Release();
76+
77+
public:
78+
/// @brief Add a callback to be called when the token is ready (call immediatly if ready)
79+
/// @param action Callback to be caled
80+
static void OnTokenReady(_v::CActionRef<> action);
81+
82+
public:
83+
/// @brief Start linking procedure
84+
static void StartLinking();
85+
/// @brief Stop linking procedure
86+
static void StopLinking();
87+
/// @brief Refresh the session
88+
static void Refresh();
89+
/// @brief Disconnect and erase the saved connected application token
90+
static void Disconnect();
91+
92+
private:
93+
/// @brief Change state and notify listenners
94+
static void ChangeState(const EState newState);
95+
/// @brief Fire on token ready actions
96+
static void FireOnTokenReady();
97+
98+
private:
99+
/// @brief Thread function
100+
static void ThreadRunner();
101+
102+
private:
103+
/// @brief Is RPC call result a success result?
104+
/// @param rpcResult Result of the RPC command
105+
/// @return True if success
106+
static bool IsRPCSuccess(_v::JsonRPCResult::Ptr& rpcResult);
107+
108+
private:
109+
/// @brief When we are Authed
110+
/// @param rpcResult Result of the RPC command
111+
static void OnAuthed(_v::JsonRPCResult::Ptr& rpcResult);
112+
/// @brief On error received
113+
/// @param rpcResult Result of the RPC command
114+
static void OnError(_v::JsonRPCResult::Ptr& rpcResult);
115+
116+
};
117+
118+
} ///< namespace CP_SDK

shared/CP_SDK/Utils/Json.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#pragma once
22

33
#include "Il2cpp.hpp"
4+
#include <beatsaber-hook/shared/config/config-utils.hpp>
5+
#include "beatsaber-hook/shared/rapidjson/include/rapidjson/document.h"
46

57
#include <fstream>
68
#include <map>
79
#include <memory>
810
#include <stdint.h>
911
#include <vector>
1012

11-
#include <beatsaber-hook/shared/config/config-utils.hpp>
1213
#include <UnityEngine/Color32.hpp>
1314
#include <UnityEngine/Color.hpp>
1415
#include <UnityEngine/Quaternion.hpp>

src/CP_SDK/CPConfig.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ namespace CP_SDK {
2323
{
2424
CP_SDK_JSON_SERIALIZE_BOOL(FirstRun);
2525
CP_SDK_JSON_SERIALIZE_BOOL(FirstChatServiceRun);
26+
CP_SDK_JSON_SERIALIZE_STRING(ChatPlexServiceToken);
2627
}
2728
/// @brief Read the document
2829
/// @param p_Document Source
2930
CP_SDK_JSON_UNSERIALIZE_IMPL(CPConfig)
3031
{
3132
CP_SDK_JSON_UNSERIALIZE_BOOL(FirstRun);
3233
CP_SDK_JSON_UNSERIALIZE_BOOL(FirstChatServiceRun);
34+
CP_SDK_JSON_UNSERIALIZE_STRING(ChatPlexServiceToken);
3335
}
3436

3537
////////////////////////////////////////////////////////////////////////////

src/CP_SDK/ChatPlexSDK.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "CP_SDK/Unity/MTMainThreadInvoker.hpp"
1010
#include "CP_SDK/Unity/MTThreadInvoker.hpp"
1111
#include "CP_SDK/ModuleBase.hpp"
12+
#include "CP_SDK/ChatPlexService.hpp"
1213

1314
#include <UnityEngine/Application.hpp>
1415

@@ -51,7 +52,6 @@ namespace CP_SDK {
5152
{
5253
InstallWEBPCodecs();
5354

54-
/// Init config
5555
Chat::Service::Init();
5656
}
5757
/// @brief On assembly exit
@@ -87,6 +87,8 @@ namespace CP_SDK {
8787

8888
/// Init UI
8989
UI::UISystem::Init();
90+
91+
ChatPlexService::Init();
9092
}
9193
catch (const std::exception& p_Exception)
9294
{
@@ -102,6 +104,8 @@ namespace CP_SDK {
102104
OnGenericSceneChange.Clear();
103105
OnGenericMenuSceneLoaded.Clear();
104106

107+
ChatPlexService::Release();
108+
105109
UI::UISystem::Destroy();
106110
UI::LoadingProgressBar::Destroy();
107111

0 commit comments

Comments
 (0)