com.taptap.sdk.core 4.4.2 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Editor/TapSDKCoreCompile.cs +6 -0
- package/Mobile/Editor/NativeDependencies.xml +2 -3
- package/Mobile/Runtime/TapEventMobile.cs +0 -1
- package/{Standalone/Runtime/Internal/Openlog/Bean.meta → Plugins.meta} +1 -1
- package/Resources/TapMessage.prefab +1 -1
- package/Runtime/Internal/Log/TapLog.cs +1 -1
- package/Runtime/Public/TapTapSDK.cs +12 -3
- package/Runtime/Public/TapTapSdkOptions.cs +0 -4
- package/Standalone/Plugins/macOS/libtapsdkcorecpp.dylib +0 -0
- package/Standalone/Plugins/macOS/libtapsdkcorecpp.dylib.meta +80 -0
- package/Standalone/Plugins/x86/tapsdkcore.dll +0 -0
- package/Standalone/Plugins/x86/tapsdkcore.dll.meta +63 -0
- package/Standalone/{Runtime/Internal/Utils.meta → Plugins/x86.meta} +1 -1
- package/Standalone/Plugins/x86_64/tapsdkcore.dll +0 -0
- package/Standalone/Plugins/x86_64/tapsdkcore.dll.meta +63 -0
- package/{link.xml.meta → Standalone/Plugins/x86_64.meta} +3 -2
- package/Standalone/Runtime/Internal/Constants.cs +0 -17
- package/Standalone/Runtime/Internal/DeviceInfo.cs +93 -89
- package/Standalone/Runtime/Internal/EventSender.cs +2 -2
- package/Standalone/Runtime/Internal/Http/TapHttp.cs +12 -6
- package/Standalone/Runtime/Internal/Http/TapHttpUtils.cs +23 -7
- package/Standalone/Runtime/Internal/Openlog/TapAppDurationStandalone.cs +121 -0
- package/Standalone/Runtime/Internal/Openlog/{TapOpenlogQueueBase.cs.meta → TapAppDurationStandalone.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogParamConstants.cs +1 -3
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogStandalone.cs +96 -94
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogStartParamConstants.cs +23 -0
- package/Standalone/Runtime/Internal/Openlog/{Bean/TapOpenlogStoreBean.cs.meta → TapOpenlogStartParamConstants.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogWrapper.cs +178 -0
- package/Standalone/Runtime/Internal/Openlog/{Bean/TapOpenlogLogGroup.cs.meta → TapOpenlogWrapper.cs.meta} +1 -1
- package/Standalone/Runtime/Public/TapCoreStandalone.cs +20 -13
- package/Standalone/Runtime/TapSDK.Core.Standalone.Runtime.asmdef +0 -1
- package/package.json +1 -1
- package/Standalone/Runtime/Internal/Openlog/Bean/TapOpenlogLogGroup.cs +0 -39
- package/Standalone/Runtime/Internal/Openlog/Bean/TapOpenlogStoreBean.cs +0 -33
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogHttpClient.cs +0 -153
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogHttpClient.cs.meta +0 -11
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueBase.cs +0 -198
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueBusiness.cs +0 -17
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueBusiness.cs.meta +0 -11
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueTechnology.cs +0 -15
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueTechnology.cs.meta +0 -11
- package/Standalone/Runtime/Internal/Utils/TapProtoBufffer.cs +0 -28
- package/Standalone/Runtime/Internal/Utils/TapProtoBufffer.cs.meta +0 -11
- package/link.xml +0 -4
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Runtime.InteropServices;
|
|
3
|
+
|
|
4
|
+
namespace TapSDK.Core.Standalone.Internal.Openlog
|
|
5
|
+
{
|
|
6
|
+
internal class TapOpenlogWrapper
|
|
7
|
+
{
|
|
8
|
+
|
|
9
|
+
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
|
|
10
|
+
internal const string DllName = "tapsdkcore";
|
|
11
|
+
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
|
|
12
|
+
internal const string DllName = "libtapsdkcorecpp";
|
|
13
|
+
#endif
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 初始化接口,只需要调用一次。
|
|
17
|
+
*
|
|
18
|
+
* cfg 初始化配置,JSON 格式:
|
|
19
|
+
*
|
|
20
|
+
{
|
|
21
|
+
"region": 2,
|
|
22
|
+
"log_to_console": 1,
|
|
23
|
+
"log_level": 1,
|
|
24
|
+
"data_dir": "/tmp",
|
|
25
|
+
"env": "local",
|
|
26
|
+
"platform": "abc",
|
|
27
|
+
"ua": "TapSDK-Android/3.28.0",
|
|
28
|
+
"client_id": "uZ8Yy6cSXVOR6AMRPj",
|
|
29
|
+
"client_token": "AVhR1Bu9qfLR1cGbZMAdZ5rzJSxfoEiQaFf1T2P7",
|
|
30
|
+
"modules": [
|
|
31
|
+
"app_duration"
|
|
32
|
+
],
|
|
33
|
+
"common": {
|
|
34
|
+
"pn": "TapSDK",
|
|
35
|
+
"app_version_code": "123",
|
|
36
|
+
"app_version": "1.2.3",
|
|
37
|
+
"app_package_name": "",
|
|
38
|
+
"install_uuid": "",
|
|
39
|
+
"device_id": "123456",
|
|
40
|
+
"caid": "",
|
|
41
|
+
"dv": "",
|
|
42
|
+
"md": "",
|
|
43
|
+
"hardware": "",
|
|
44
|
+
"cpu": "",
|
|
45
|
+
"cpu_abis": "",
|
|
46
|
+
"os": "android",
|
|
47
|
+
"sv": "",
|
|
48
|
+
"width": "",
|
|
49
|
+
"height": "",
|
|
50
|
+
"total_rom": "",
|
|
51
|
+
"total_ram": "",
|
|
52
|
+
"open_id": "",
|
|
53
|
+
"tds_user_id": "",
|
|
54
|
+
"sdk_locale": ""
|
|
55
|
+
}
|
|
56
|
+
"app_duration": {
|
|
57
|
+
"tapsdk_version": ""
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
*
|
|
61
|
+
* - log_level 取值:1 Trace、2 Debug、3 Info、4 Warn、5 Error、6 完全不输出
|
|
62
|
+
* - region 取值:0 国内、1 海外、2 RND
|
|
63
|
+
* - modules 取值:app_duration(开启时长埋点)。如果 modules 不传,或者为空,
|
|
64
|
+
* 则仅开启 OpenLog 功能,不会上报游戏时长
|
|
65
|
+
*
|
|
66
|
+
* commonVariablesGetter 用于获取运行时会发生变化的公参
|
|
67
|
+
*
|
|
68
|
+
* 成功返回 0,失败返回 -1
|
|
69
|
+
*/
|
|
70
|
+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
|
71
|
+
internal static extern int TdkOnAppStarted(string cfg, CommonVariablesGetter commonVariablesGetter, FreeStringCallback freeString);
|
|
72
|
+
|
|
73
|
+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
74
|
+
internal delegate IntPtr CommonVariablesGetter();
|
|
75
|
+
|
|
76
|
+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
77
|
+
internal delegate void FreeStringCallback(IntPtr intPtr);
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* App 退出时调用,只需要调用一次。
|
|
81
|
+
*/
|
|
82
|
+
[DllImport(DllName)]
|
|
83
|
+
internal static extern void TdkOnAppStopped();
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 启用功能模块,如:app_duration
|
|
87
|
+
*
|
|
88
|
+
* modules JSON 数组,如:["app_duration"]
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
[DllImport(DllName)]
|
|
92
|
+
internal static extern void TdkEnableModules(string modules);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 禁用功能模块,如:app_duration
|
|
96
|
+
*
|
|
97
|
+
* modules JSON 数组,如:["app_duration"]
|
|
98
|
+
*
|
|
99
|
+
*/
|
|
100
|
+
[DllImport(DllName)]
|
|
101
|
+
internal static extern void TdkDisableModules(string modules);
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 需要发送埋点日志时调用。
|
|
105
|
+
* SDK 内部会整合初始化接口传入的公参、extraArgsFunc 返回的公参,以及 log 里的业务参数,
|
|
106
|
+
* 然后再发送到 OpenLog 服务端
|
|
107
|
+
*
|
|
108
|
+
* logstore 如:tapsdk、tapsdk-apm
|
|
109
|
+
* log 埋点日志,仅需传递业务参数,JSON 格式:{"action":"xxx", "open_id":"yyy","tds_user_id":"zzz"}
|
|
110
|
+
*/
|
|
111
|
+
[DllImport(DllName)]
|
|
112
|
+
internal static extern void TdkOpenLog(string logStore, string logContent);
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 用户登录成功时调用。
|
|
116
|
+
*
|
|
117
|
+
* userInfo 用户信息,JSON 格式:{"open_id":"","tds_user_id":""}
|
|
118
|
+
*/
|
|
119
|
+
[DllImport(DllName)]
|
|
120
|
+
internal static extern void TdkOnLogin(string userInfo);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 用户登出时调用。
|
|
124
|
+
*/
|
|
125
|
+
[DllImport(DllName)]
|
|
126
|
+
internal static extern void TdkOnLogout();
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 用户切到后台时调用。
|
|
130
|
+
*/
|
|
131
|
+
[DllImport(DllName)]
|
|
132
|
+
internal static extern void TdkOnForeground();
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 游戏切回前台时调用。
|
|
136
|
+
*/
|
|
137
|
+
[DllImport(DllName)]
|
|
138
|
+
internal static extern void TdkOnBackground();
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 设置额外的时长模块日志参数,JSON K/V 格式。每次调用这个接口时,会用最新得到的参数,替换原有参数。
|
|
142
|
+
*
|
|
143
|
+
* params 额外日志参数,JSON 格式:{"K1":"V1","K2":"V2","K3":"V3"}
|
|
144
|
+
*/
|
|
145
|
+
[DllImport(DllName)]
|
|
146
|
+
internal static extern void TdkSetExtraAppDurationParams(string paramsJson);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 设置日志等级
|
|
150
|
+
*/
|
|
151
|
+
[DllImport(DllName)]
|
|
152
|
+
internal static extern void TdkSetLogLevel(int logLevel, int logToConsole);
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 代码版本,如:1.2.5
|
|
156
|
+
*/
|
|
157
|
+
[DllImport(DllName)]
|
|
158
|
+
internal static extern IntPtr TdkVersion();
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* git commit 版本,如:98f5d81a0fdcab9a755878b3e825c2cb510e5196
|
|
162
|
+
*/
|
|
163
|
+
[DllImport(DllName)]
|
|
164
|
+
internal static extern IntPtr TdkGitCommit();
|
|
165
|
+
|
|
166
|
+
// 用于返回 TdkVersion 的封装方法
|
|
167
|
+
public static string GetTdkVersion()
|
|
168
|
+
{
|
|
169
|
+
return Marshal.PtrToStringAnsi(TdkVersion());
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 用于返回 TdkGitCommit 的封装方法
|
|
173
|
+
public static string GetTdkGitCommit()
|
|
174
|
+
{
|
|
175
|
+
return Marshal.PtrToStringAnsi(TdkGitCommit());
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -22,7 +22,6 @@ namespace TapSDK.Core.Standalone
|
|
|
22
22
|
internal static Tracker Tracker;
|
|
23
23
|
internal static User User;
|
|
24
24
|
internal static TapTapSdkOptions coreOptions;
|
|
25
|
-
public static bool isRnd = false;
|
|
26
25
|
internal static bool enableAutoEvent = true;
|
|
27
26
|
|
|
28
27
|
internal static TapGatekeeper gatekeeperData = new TapGatekeeper();
|
|
@@ -34,7 +33,6 @@ namespace TapSDK.Core.Standalone
|
|
|
34
33
|
/// </summary>
|
|
35
34
|
public TapCoreStandalone()
|
|
36
35
|
{
|
|
37
|
-
Debug.Log("TapCoStandalone constructor");
|
|
38
36
|
// Instantiate modules
|
|
39
37
|
Prefs = new Prefs();
|
|
40
38
|
Tracker = new Tracker();
|
|
@@ -42,12 +40,6 @@ namespace TapSDK.Core.Standalone
|
|
|
42
40
|
TapLoom.Initialize();
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
private static void SetRND(bool isRnd)
|
|
46
|
-
{
|
|
47
|
-
TapLog.Log("SetRND called = " + isRnd);
|
|
48
|
-
TapCoreStandalone.isRnd = isRnd;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
43
|
/// <summary>
|
|
52
44
|
/// Initializes the TapCore SDK with the specified options.
|
|
53
45
|
/// </summary>
|
|
@@ -64,18 +56,28 @@ namespace TapSDK.Core.Standalone
|
|
|
64
56
|
/// <param name="otherOptions">Additional TapCore SDK options.</param>
|
|
65
57
|
public void Init(TapTapSdkOptions coreOption, TapTapSdkBaseOptions[] otherOptions)
|
|
66
58
|
{
|
|
67
|
-
TapLog.Log("SDK
|
|
59
|
+
TapLog.Log("SDK Init Options : ", "coreOption : " + JsonConvert.SerializeObject(coreOption) + "\notherOptions : " + JsonConvert.SerializeObject(otherOptions));
|
|
68
60
|
coreOptions = coreOption;
|
|
69
61
|
|
|
62
|
+
TapOpenlogStandalone.Init();
|
|
63
|
+
|
|
70
64
|
var path = Path.Combine(Application.persistentDataPath, Constants.ClientSettingsFileName);
|
|
71
65
|
if (File.Exists(path))
|
|
72
66
|
{
|
|
73
67
|
var clientSettings = File.ReadAllText(path);
|
|
74
|
-
TapLog.Log("本地 clientSettings: " + clientSettings);
|
|
68
|
+
// TapLog.Log("本地 clientSettings: " + clientSettings);
|
|
75
69
|
try
|
|
76
70
|
{
|
|
77
71
|
TapGatekeeper tapGatekeeper = JsonConvert.DeserializeObject<TapGatekeeper>(clientSettings);
|
|
78
72
|
SetAutoEvent(tapGatekeeper);
|
|
73
|
+
if (tapGatekeeper.Switch?.Heartbeat == true)
|
|
74
|
+
{
|
|
75
|
+
TapAppDurationStandalone.Enable();
|
|
76
|
+
}
|
|
77
|
+
else
|
|
78
|
+
{
|
|
79
|
+
TapAppDurationStandalone.Disable();
|
|
80
|
+
}
|
|
79
81
|
gatekeeperData = tapGatekeeper;
|
|
80
82
|
}
|
|
81
83
|
catch (System.Exception e)
|
|
@@ -86,7 +88,6 @@ namespace TapSDK.Core.Standalone
|
|
|
86
88
|
|
|
87
89
|
Tracker.Init();
|
|
88
90
|
|
|
89
|
-
TapOpenlogStandalone.Init();
|
|
90
91
|
requestClientSetting();
|
|
91
92
|
}
|
|
92
93
|
|
|
@@ -138,6 +139,14 @@ namespace TapSDK.Core.Standalone
|
|
|
138
139
|
onSuccess: (data) =>
|
|
139
140
|
{
|
|
140
141
|
SetAutoEvent(data);
|
|
142
|
+
if (data.Switch?.Heartbeat == true)
|
|
143
|
+
{
|
|
144
|
+
TapAppDurationStandalone.Enable();
|
|
145
|
+
}
|
|
146
|
+
else
|
|
147
|
+
{
|
|
148
|
+
TapAppDurationStandalone.Disable();
|
|
149
|
+
}
|
|
141
150
|
gatekeeperData = data;
|
|
142
151
|
// 把 data 存储在本地
|
|
143
152
|
saveClientSettings(data);
|
|
@@ -161,7 +170,6 @@ namespace TapSDK.Core.Standalone
|
|
|
161
170
|
private void saveClientSettings(TapGatekeeper settings)
|
|
162
171
|
{
|
|
163
172
|
string json = JsonConvert.SerializeObject(settings);
|
|
164
|
-
Debug.Log("saveClientSettings: " + json);
|
|
165
173
|
File.WriteAllText(Path.Combine(Application.persistentDataPath, Constants.ClientSettingsFileName), json);
|
|
166
174
|
}
|
|
167
175
|
|
|
@@ -175,7 +183,6 @@ namespace TapSDK.Core.Standalone
|
|
|
175
183
|
enableAutoEvent = switchData.AutoEvent;
|
|
176
184
|
}
|
|
177
185
|
}
|
|
178
|
-
Debug.Log("SetAutoEvent enableAutoEvent is: " + enableAutoEvent);
|
|
179
186
|
}
|
|
180
187
|
}
|
|
181
188
|
|
package/package.json
CHANGED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
#if UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN
|
|
2
|
-
using Newtonsoft.Json;
|
|
3
|
-
using ProtoBuf;
|
|
4
|
-
using System.Collections.Generic;
|
|
5
|
-
|
|
6
|
-
namespace TapSDK.Core.Standalone.Internal
|
|
7
|
-
{
|
|
8
|
-
[ProtoContract]
|
|
9
|
-
public class LogContent
|
|
10
|
-
{
|
|
11
|
-
[JsonProperty("Key")]
|
|
12
|
-
[ProtoMember(1)]
|
|
13
|
-
public string Key { get; set; }
|
|
14
|
-
|
|
15
|
-
[JsonProperty("Value")]
|
|
16
|
-
[ProtoMember(2)]
|
|
17
|
-
public string Value { get; set; }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
[ProtoContract]
|
|
21
|
-
public class Log
|
|
22
|
-
{
|
|
23
|
-
[JsonProperty("Value")]
|
|
24
|
-
[ProtoMember(1)]
|
|
25
|
-
public uint Time { get; set; }
|
|
26
|
-
[JsonProperty("Contents")]
|
|
27
|
-
[ProtoMember(2)]
|
|
28
|
-
public List<LogContent> Contents { get; set; }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
[ProtoContract]
|
|
32
|
-
public class LogGroup
|
|
33
|
-
{
|
|
34
|
-
[JsonProperty("Logs")]
|
|
35
|
-
[ProtoMember(1)]
|
|
36
|
-
public List<Log> Logs { get; set; }
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
#endif
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
using System.Collections.Generic;
|
|
2
|
-
using Newtonsoft.Json;
|
|
3
|
-
|
|
4
|
-
namespace TapSDK.Core.Standalone.Internal.Openlog
|
|
5
|
-
{
|
|
6
|
-
public class TapOpenlogStoreBean
|
|
7
|
-
{
|
|
8
|
-
[JsonProperty("action")]
|
|
9
|
-
public string action;
|
|
10
|
-
|
|
11
|
-
[JsonProperty("timestamp")]
|
|
12
|
-
public long timestamp;
|
|
13
|
-
|
|
14
|
-
[JsonProperty("t_log_id")]
|
|
15
|
-
public string tLogId;
|
|
16
|
-
|
|
17
|
-
[JsonProperty("props")]
|
|
18
|
-
public readonly Dictionary<string, string> props;
|
|
19
|
-
|
|
20
|
-
public TapOpenlogStoreBean(string action, long timestamp, string tLogId, Dictionary<string, string> props)
|
|
21
|
-
{
|
|
22
|
-
this.action = action;
|
|
23
|
-
this.timestamp = timestamp;
|
|
24
|
-
this.tLogId = tLogId;
|
|
25
|
-
this.props = props;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public override string ToString()
|
|
29
|
-
{
|
|
30
|
-
return $"TapOpenlogStoreBean(action: {action} , tLogId: {tLogId} , timestamp: {timestamp})";
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
#if UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN
|
|
2
|
-
using System;
|
|
3
|
-
using System.Threading.Tasks;
|
|
4
|
-
using System.Net;
|
|
5
|
-
using System.Net.Http;
|
|
6
|
-
using System.Net.Http.Headers;
|
|
7
|
-
using System.Linq;
|
|
8
|
-
using System.Security.Cryptography;
|
|
9
|
-
using System.Text;
|
|
10
|
-
using TapSDK.Core.Internal.Log;
|
|
11
|
-
using K4os.Compression.LZ4;
|
|
12
|
-
|
|
13
|
-
namespace TapSDK.Core.Standalone.Internal
|
|
14
|
-
{
|
|
15
|
-
public class TapOpenlogHttpClient
|
|
16
|
-
{
|
|
17
|
-
|
|
18
|
-
private static readonly string HOST_CN = "openlog.tapapis.cn";
|
|
19
|
-
private static readonly string HOST_IO = "openlog.tapapis.com";
|
|
20
|
-
|
|
21
|
-
private static string GetHost()
|
|
22
|
-
{
|
|
23
|
-
if (TapCoreStandalone.coreOptions.region == TapTapRegionType.CN)
|
|
24
|
-
{
|
|
25
|
-
return HOST_CN;
|
|
26
|
-
}
|
|
27
|
-
else if (TapCoreStandalone.coreOptions.region == TapTapRegionType.Overseas)
|
|
28
|
-
{
|
|
29
|
-
return HOST_IO;
|
|
30
|
-
}
|
|
31
|
-
else
|
|
32
|
-
{
|
|
33
|
-
return HOST_CN;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private static TapLog log = new TapLog(module: "Openlog.HttpClient");
|
|
38
|
-
|
|
39
|
-
private static HttpClient client;
|
|
40
|
-
|
|
41
|
-
public TapOpenlogHttpClient()
|
|
42
|
-
{
|
|
43
|
-
client = new HttpClient();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public async Task<bool> Post(string path, byte[] content)
|
|
47
|
-
{
|
|
48
|
-
string url = BuildUrl(path);
|
|
49
|
-
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
|
50
|
-
|
|
51
|
-
request.Headers.Add("User-Agent", "TapSDK-Unity/" + TapTapSDK.Version);
|
|
52
|
-
request.Headers.Add("Accept", "*/*");
|
|
53
|
-
request.Headers.Add("x-log-apiversion", "0.6.0");
|
|
54
|
-
request.Headers.Add("x-log-compresstype", "lz4");
|
|
55
|
-
request.Headers.Add("x-log-signaturemethod", "hmac-sha1");
|
|
56
|
-
request.Headers.Add("x-log-timestamp", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
|
|
57
|
-
request.Headers.Add("x-log-bodyrawsize", content.Length.ToString());
|
|
58
|
-
|
|
59
|
-
byte[] compressContent = LZ4Compress(content);
|
|
60
|
-
|
|
61
|
-
var contentMD5 = EncryptString(compressContent);
|
|
62
|
-
request.Headers.Add("x-content-md5", contentMD5);
|
|
63
|
-
|
|
64
|
-
string methodPart = request.Method.Method;
|
|
65
|
-
string urlPath = request.RequestUri.LocalPath;
|
|
66
|
-
string headersPart = GetHeadersPart(request.Headers);
|
|
67
|
-
string signParts = methodPart + "\n" + contentMD5 + "\n" + "application/x-protobuf" + "\n" + headersPart + "\n" + urlPath;
|
|
68
|
-
string sign;
|
|
69
|
-
using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(TapCoreStandalone.coreOptions.clientToken)))
|
|
70
|
-
{
|
|
71
|
-
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(signParts));
|
|
72
|
-
sign = Convert.ToBase64String(hash);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
request.Headers.Add("Authorization", "LOG " + TapCoreStandalone.coreOptions.clientId + ":" + sign);
|
|
76
|
-
|
|
77
|
-
ByteArrayContent requestContent = new ByteArrayContent(compressContent);
|
|
78
|
-
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");
|
|
79
|
-
request.Content = requestContent;
|
|
80
|
-
|
|
81
|
-
return await SendRequest(request);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private static string EncryptString(byte[] str)
|
|
85
|
-
{
|
|
86
|
-
var md5 = MD5.Create();
|
|
87
|
-
byte[] byteOld = str;
|
|
88
|
-
byte[] byteNew = md5.ComputeHash(byteOld);
|
|
89
|
-
var sb = new StringBuilder();
|
|
90
|
-
foreach (byte b in byteNew)
|
|
91
|
-
{
|
|
92
|
-
sb.Append(b.ToString("X2"));
|
|
93
|
-
}
|
|
94
|
-
return sb.ToString();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private static byte[] LZ4Compress(byte[] data)
|
|
98
|
-
{
|
|
99
|
-
int maxCompressedLength = LZ4Codec.MaximumOutputSize(data.Length);
|
|
100
|
-
byte[] compressedData = new byte[maxCompressedLength];
|
|
101
|
-
int compressedLength = LZ4Codec.Encode(data, 0, data.Length, compressedData, 0, compressedData.Length);
|
|
102
|
-
|
|
103
|
-
byte[] result = new byte[compressedLength];
|
|
104
|
-
Array.Copy(compressedData, result, compressedLength);
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private static string GetHeadersPart(HttpRequestHeaders headers)
|
|
109
|
-
{
|
|
110
|
-
var headerKeys = headers
|
|
111
|
-
.Where(h => h.Key.StartsWith("x-log-", StringComparison.OrdinalIgnoreCase))
|
|
112
|
-
.OrderBy(h => h.Key.ToLowerInvariant())
|
|
113
|
-
.Select(h => $"{h.Key.ToLowerInvariant()}:{string.Join(",", h.Value)}")
|
|
114
|
-
.ToList();
|
|
115
|
-
|
|
116
|
-
return string.Join("\n", headerKeys);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private async Task<bool> SendRequest(HttpRequestMessage request)
|
|
120
|
-
{
|
|
121
|
-
HttpResponseMessage response;
|
|
122
|
-
try
|
|
123
|
-
{
|
|
124
|
-
response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
|
125
|
-
request.Dispose();
|
|
126
|
-
}
|
|
127
|
-
catch (HttpRequestException e)
|
|
128
|
-
{
|
|
129
|
-
log.Warning($"Request error", e.ToString());
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (response.IsSuccessStatusCode || (response.StatusCode >= HttpStatusCode.BadRequest && response.StatusCode < HttpStatusCode.InternalServerError))
|
|
134
|
-
{
|
|
135
|
-
response.Dispose();
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
else
|
|
139
|
-
{
|
|
140
|
-
log.Warning($"SendOpenlog failed", response.StatusCode.ToString());
|
|
141
|
-
response.Dispose();
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
private static string BuildUrl(string path)
|
|
147
|
-
{
|
|
148
|
-
string url = $"https://{GetHost()}/{path}?client_id={TapCoreStandalone.coreOptions.clientId}";
|
|
149
|
-
return url;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
#endif
|