CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Dialog/PSPNpSigninDialog.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
#if defined(_WIN32)
20
#include "Common/CommonWindows.h"
21
#endif
22
#include "Common/TimeUtil.h"
23
#include "Common/Data/Text/I18n.h"
24
#include "Common/Serialize/Serializer.h"
25
#include "Common/Serialize/SerializeFuncs.h"
26
#include "Core/Config.h"
27
#include "Core/MemMapHelpers.h"
28
#include "Core/Util/PPGeDraw.h"
29
#include "Core/HLE/sceKernelMemory.h"
30
#include "Core/HLE/sceCtrl.h"
31
#include "Core/HLE/sceUtility.h"
32
#include "Core/HLE/sceNet.h"
33
#include "Core/HLE/sceNetAdhoc.h"
34
#include "Core/HLE/sceNp.h"
35
#include "Core/Dialog/PSPNpSigninDialog.h"
36
#include "Common/Data/Encoding/Utf8.h"
37
#include "Core/Reporting.h"
38
39
40
static const float FONT_SCALE = 0.65f;
41
42
// Needs testing.
43
const static int NP_INIT_DELAY_US = 200000;
44
const static int NP_SHUTDOWN_DELAY_US = 501000;
45
const static int NP_RUNNING_DELAY_US = 1000000; // faked delay to simulate signin process to give chance for players to read the text on the dialog
46
47
PSPNpSigninDialog::PSPNpSigninDialog(UtilityDialogType type) : PSPDialog(type) {
48
}
49
50
PSPNpSigninDialog::~PSPNpSigninDialog() {
51
}
52
53
int PSPNpSigninDialog::Init(u32 paramAddr) {
54
// Already running
55
if (ReadStatus() != SCE_UTILITY_STATUS_NONE)
56
return SCE_ERROR_UTILITY_INVALID_STATUS;
57
58
requestAddr = paramAddr;
59
int size = Memory::Read_U32(paramAddr);
60
memset(&request, 0, sizeof(request));
61
// Only copy the right size to support different request format
62
Memory::Memcpy(&request, paramAddr, size);
63
64
WARN_LOG_REPORT_ONCE(PSPNpSigninDialogInit, Log::sceNet, "NpSignin Init Params: %08x, %08x, %08x, %08x", request.npSigninStatus, request.unknown1, request.unknown2, request.unknown3);
65
66
ChangeStatusInit(NP_INIT_DELAY_US);
67
68
// Eat any keys pressed before the dialog inited.
69
UpdateButtons();
70
InitCommon();
71
72
//npSigninResult = -1;
73
startTime = (u64)(time_now_d() * 1000000.0);
74
step = 0;
75
76
StartFade(true);
77
return 0;
78
}
79
80
void PSPNpSigninDialog::DrawBanner() {
81
82
PPGeDrawRect(0, 0, 480, 22, CalcFadedColor(0x65636358));
83
84
PPGeStyle textStyle = FadedStyle(PPGeAlign::BOX_VCENTER, 0.6f);
85
textStyle.hasShadow = false;
86
87
// TODO: Draw a hexagon icon
88
PPGeDrawImage(10, 5, 11.0f, 10.0f, 1, 10, 1, 10, 10, 10, FadedImageStyle());
89
auto di = GetI18NCategory(I18NCat::DIALOG);
90
PPGeDrawText(di->T("Sign In"), 31, 10, textStyle);
91
}
92
93
void PSPNpSigninDialog::DrawIndicator() {
94
// TODO: Draw animated circle as processing indicator
95
PPGeDrawImage(456, 248, 20.0f, 20.0f, 1, 10, 1, 10, 10, 10, FadedImageStyle());
96
}
97
98
void PSPNpSigninDialog::DrawLogo() {
99
// TODO: Draw OpenDNAS logo
100
PPGeDrawImage(416, 22, 64.0f, 64.0f, 1, 10, 1, 10, 64, 64, FadedImageStyle());
101
}
102
103
void PSPNpSigninDialog::DisplayMessage(std::string_view text1, std::string_view text2a, std::string_view text2b, std::string_view text3a, std::string_view text3b, bool hasYesNo, bool hasOK) {
104
auto di = GetI18NCategory(I18NCat::DIALOG);
105
106
PPGeStyle buttonStyle = FadedStyle(PPGeAlign::BOX_CENTER, FONT_SCALE);
107
PPGeStyle messageStyle = FadedStyle(PPGeAlign::BOX_HCENTER, FONT_SCALE);
108
PPGeStyle messageStyleRight = FadedStyle(PPGeAlign::BOX_RIGHT, FONT_SCALE);
109
PPGeStyle messageStyleLeft = FadedStyle(PPGeAlign::BOX_LEFT, FONT_SCALE);
110
111
std::string text2 = std::string(text2a) + " " + std::string(text2b);
112
std::string text3 = std::string(text3a) + " " + std::string(text3b);
113
114
// Without the scrollbar, we have 350 total pixels.
115
float WRAP_WIDTH = 300.0f;
116
if (UTF8StringNonASCIICount(text1) >= (int)text1.size() / 4) {
117
WRAP_WIDTH = 336.0f;
118
if (text1.size() > 12) {
119
messageStyle.scale = 0.6f;
120
}
121
}
122
123
float totalHeight1 = 0.0f;
124
PPGeMeasureText(nullptr, &totalHeight1, text1, FONT_SCALE, PPGE_LINE_WRAP_WORD, WRAP_WIDTH);
125
float totalHeight2 = 0.0f;
126
if (text2 != " ")
127
PPGeMeasureText(nullptr, &totalHeight2, text2, FONT_SCALE, PPGE_LINE_USE_ELLIPSIS, WRAP_WIDTH);
128
float totalHeight3 = 0.0f;
129
if (text3 != " ")
130
PPGeMeasureText(nullptr, &totalHeight3, text3, FONT_SCALE, PPGE_LINE_USE_ELLIPSIS, WRAP_WIDTH);
131
float marginTop = 0.0f;
132
if (text2 != " " || text3 != " ")
133
marginTop = 11.0f;
134
float totalHeight = totalHeight1 + totalHeight2 + totalHeight3 + marginTop;
135
// The PSP normally only shows about 8 lines at a time.
136
// For improved UX, we intentionally show part of the next line.
137
float visibleHeight = std::min(totalHeight, 175.0f);
138
float h2 = visibleHeight / 2.0f;
139
140
float centerY = 135.0f;
141
float sy = centerY - h2 - 15.0f;
142
float ey = centerY + h2 + 20.0f;
143
float buttonY = centerY + h2 + 5.0f;
144
145
auto drawSelectionBoxAndAdjust = [&](float x) {
146
// Box has a fixed size.
147
float w = 15.0f;
148
float h = 8.0f;
149
PPGeDrawRect(x - w, buttonY - h, x + w, buttonY + h, CalcFadedColor(0x6DCFCFCF));
150
151
centerY -= h + 5.0f;
152
sy -= h + 5.0f;
153
ey = buttonY + h * 2.0f + 5.0f;
154
};
155
156
if (hasYesNo) {
157
if (yesnoChoice == 1) {
158
drawSelectionBoxAndAdjust(204.0f);
159
}
160
else {
161
drawSelectionBoxAndAdjust(273.0f);
162
}
163
164
PPGeDrawText(di->T("Yes"), 203.0f, buttonY - 1.0f, buttonStyle);
165
PPGeDrawText(di->T("No"), 272.0f, buttonY - 1.0f, buttonStyle);
166
if (IsButtonPressed(CTRL_LEFT) && yesnoChoice == 0) {
167
yesnoChoice = 1;
168
}
169
else if (IsButtonPressed(CTRL_RIGHT) && yesnoChoice == 1) {
170
yesnoChoice = 0;
171
}
172
buttonY += 8.0f + 5.0f;
173
}
174
175
if (hasOK) {
176
drawSelectionBoxAndAdjust(240.0f);
177
178
PPGeDrawText(di->T("OK"), 239.0f, buttonY - 1.0f, buttonStyle);
179
buttonY += 8.0f + 5.0f;
180
}
181
182
PPGeScissor(0, (int)(centerY - h2 - 2), 480, (int)(centerY + h2 + 2));
183
PPGeDrawTextWrapped(text1, 240.0f, centerY - h2 - scrollPos_, WRAP_WIDTH, 0, messageStyle);
184
if (!text2a.empty()) {
185
if (!text2b.empty())
186
PPGeDrawTextWrapped(text2a, 240.0f - 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + marginTop, WRAP_WIDTH, 0, messageStyleRight);
187
else
188
PPGeDrawTextWrapped(text2a, 240.0f, centerY - h2 - scrollPos_ + totalHeight1 + marginTop, WRAP_WIDTH, 0, messageStyle);
189
}
190
if (!text2b.empty())
191
PPGeDrawTextWrapped(text2b, 240.0f + 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + marginTop, WRAP_WIDTH, 0, messageStyleLeft);
192
if (!text3a.empty()) {
193
if (!text3b.empty())
194
PPGeDrawTextWrapped(text3a, 240.0f - 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + totalHeight2 + marginTop, WRAP_WIDTH, 0, messageStyleRight);
195
else
196
PPGeDrawTextWrapped(text3a, 240.0f, centerY - h2 - scrollPos_ + totalHeight1 + totalHeight2 + marginTop, WRAP_WIDTH, 0, messageStyle);
197
}
198
if (!text3b.empty())
199
PPGeDrawTextWrapped(text3b, 240.0f + 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + totalHeight2 + marginTop, WRAP_WIDTH, 0, messageStyleLeft);
200
PPGeScissorReset();
201
202
// Do we need a scrollbar?
203
if (visibleHeight < totalHeight) {
204
float scrollSpeed = 5.0f;
205
float scrollMax = totalHeight - visibleHeight;
206
207
float bobHeight = (visibleHeight / totalHeight) * visibleHeight;
208
float bobOffset = (scrollPos_ / scrollMax) * (visibleHeight - bobHeight);
209
float bobY1 = centerY - h2 + bobOffset;
210
PPGeDrawRect(415.0f, bobY1, 420.0f, bobY1 + bobHeight, CalcFadedColor(0xFFCCCCCC));
211
212
auto buttonDown = [this](int btn, int& held) {
213
if (IsButtonPressed(btn)) {
214
held = 0;
215
return true;
216
}
217
return IsButtonHeld(btn, held, 1, 1);
218
};
219
if (buttonDown(CTRL_DOWN, framesDownHeld_) && scrollPos_ < scrollMax) {
220
scrollPos_ = std::min(scrollMax, scrollPos_ + scrollSpeed);
221
}
222
if (buttonDown(CTRL_UP, framesUpHeld_) && scrollPos_ > 0.0f) {
223
scrollPos_ = std::max(0.0f, scrollPos_ - scrollSpeed);
224
}
225
}
226
227
PPGeDrawRect(60.0f, sy, 420.0f, sy + 1.0f, CalcFadedColor(0xFFFFFFFF));
228
PPGeDrawRect(60.0f, ey, 420.0f, ey + 1.0f, CalcFadedColor(0xFFFFFFFF));
229
}
230
231
int PSPNpSigninDialog::Update(int animSpeed) {
232
if (ReadStatus() != SCE_UTILITY_STATUS_RUNNING) {
233
return SCE_ERROR_UTILITY_INVALID_STATUS;
234
}
235
236
UpdateButtons();
237
UpdateCommon();
238
auto err = GetI18NCategory(I18NCat::ERRORS);
239
u64 now = (u64)(time_now_d() * 1000000.0);
240
241
if (request.npSigninStatus == NP_SIGNIN_STATUS_NONE) {
242
auto di = GetI18NCategory(I18NCat::DIALOG);
243
UpdateFade(animSpeed);
244
StartDraw();
245
246
PPGeDrawRect(0, 0, 480, 272, CalcFadedColor(0xC0C8B2AC));
247
DrawBanner();
248
DrawIndicator();
249
250
// TODO: Not sure what should happen here.. may be something like this https://pastebin.com/1eW48zBb ? but we can do test on Open DNAS Server later https://dnas.hashsploit.net/us-gw/
251
// DNAS dialog
252
if (step >= 2 && now - startTime > NP_RUNNING_DELAY_US) {
253
DrawLogo();
254
DisplayMessage(di->T("PleaseWait", "Please wait..."));
255
step++;
256
}
257
// Signin dialog
258
else {
259
// Skipping the Select Connection screen since we only have 1 fake profile
260
DisplayMessage(di->T("SigninPleaseWait", "Signing in...\nPlease wait."));
261
}
262
DisplayButtons(DS_BUTTON_CANCEL, di->T("Cancel"));
263
264
if (step >= 2 && now - startTime > NP_RUNNING_DELAY_US*2) {
265
if (pendingStatus != SCE_UTILITY_STATUS_FINISHED) {
266
StartFade(false);
267
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, NP_SHUTDOWN_DELAY_US);
268
step++;
269
}
270
}
271
272
else if (step == 1 && now - startTime > NP_RUNNING_DELAY_US) {
273
// Switch to the next message (with DNAS logo)
274
StartFade(true);
275
step++;
276
}
277
278
else if (step == 0) {
279
/*if (npAuthResult < 0 && request.NpSigninData.IsValid()) {
280
npAuthResult = sceNpAuthCreateStartRequest(request.NpSigninData->paramAddr);
281
}*/
282
step++;
283
}
284
285
if (/*npAuthResult >= 0 &&*/ IsButtonPressed(cancelButtonFlag)) {
286
StartFade(false);
287
//sceNpAuthAbortRequest(npAuthResult);
288
//sceNpAuthDestroyRequest(npAuthResult);
289
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, NP_SHUTDOWN_DELAY_US);
290
request.common.result = SCE_UTILITY_DIALOG_RESULT_ABORT;
291
request.npSigninStatus = NP_SIGNIN_STATUS_CANCELED;
292
//step = 0;
293
}
294
295
EndDraw();
296
}
297
298
if (ReadStatus() == SCE_UTILITY_STATUS_FINISHED || pendingStatus == SCE_UTILITY_STATUS_FINISHED) {
299
npSigninState = NP_SIGNIN_STATUS_SUCCESS;
300
__RtcTimeOfDay(&npSigninTimestamp);
301
request.npSigninStatus = npSigninState;
302
}
303
return 0;
304
}
305
306
int PSPNpSigninDialog::Shutdown(bool force) {
307
if (ReadStatus() != SCE_UTILITY_STATUS_FINISHED && !force)
308
return SCE_ERROR_UTILITY_INVALID_STATUS;
309
310
PSPDialog::Shutdown(force);
311
if (!force) {
312
ChangeStatusShutdown(NP_SHUTDOWN_DELAY_US);
313
}
314
315
// FIXME: This should probably be done within FinishShutdown to prevent some games (ie. UNO) from progressing further while the Dialog is still being faded-out, since we can't override non-virtual method... so here is the closes one to FinishShutdown.
316
if (Memory::IsValidAddress(requestAddr)) // Need to validate first to prevent Invalid address when the game is being Shutdown/Exited to menu
317
Memory::Memcpy(requestAddr, &request, request.common.size, "NpSigninDialogParam");
318
319
return 0;
320
}
321
322
void PSPNpSigninDialog::DoState(PointerWrap &p) {
323
PSPDialog::DoState(p);
324
325
auto s = p.Section("PSPNpSigninDialog", 1, 1);
326
if (!s)
327
return;
328
329
Do(p, request);
330
Do(p, step);
331
//Do(p, npSigninResult);
332
333
if (p.mode == p.MODE_READ) {
334
startTime = 0;
335
}
336
}
337
338
pspUtilityDialogCommon* PSPNpSigninDialog::GetCommonParam()
339
{
340
return &request.common;
341
}
342
343