1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.network;
|
21
|
|
22
|
import android.app.Activity;
|
23
|
import android.content.pm.PackageInfo;
|
24
|
import android.content.pm.PackageManager;
|
25
|
|
26
|
import androidx.appcompat.app.AppCompatActivity;
|
27
|
import androidx.fragment.app.FragmentActivity;
|
28
|
|
29
|
import org.distorted.library.main.DistortedLibrary;
|
30
|
import org.distorted.objects.ObjectList;
|
31
|
|
32
|
import java.io.InputStream;
|
33
|
import java.net.HttpURLConnection;
|
34
|
import java.net.URL;
|
35
|
import java.net.UnknownHostException;
|
36
|
import java.security.MessageDigest;
|
37
|
import java.security.NoSuchAlgorithmException;
|
38
|
|
39
|
import static org.distorted.objects.ObjectList.MAX_LEVEL;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class RubikNetwork implements Runnable
|
44
|
{
|
45
|
public interface Receiver
|
46
|
{
|
47
|
void receive(String[][][] country, String[][][] name, float[][][] time);
|
48
|
void message(String mess);
|
49
|
void error(String error);
|
50
|
}
|
51
|
|
52
|
public static final int MAX_PLACES = 10;
|
53
|
|
54
|
private static final int DOWNLOAD = 0;
|
55
|
private static final int SUBMIT = 1;
|
56
|
private static final int DEBUG = 2;
|
57
|
private static final int IDLE = 3;
|
58
|
|
59
|
private final String[] hex = {
|
60
|
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
|
61
|
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
|
62
|
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
|
63
|
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
|
64
|
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
|
65
|
"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
|
66
|
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
|
67
|
"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
|
68
|
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
|
69
|
"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
|
70
|
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
|
71
|
"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
|
72
|
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
|
73
|
"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
|
74
|
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
|
75
|
"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
|
76
|
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
|
77
|
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
|
78
|
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
|
79
|
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
|
80
|
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
|
81
|
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
|
82
|
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
|
83
|
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
|
84
|
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
|
85
|
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
|
86
|
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
|
87
|
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
|
88
|
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
|
89
|
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
|
90
|
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
|
91
|
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
|
92
|
};
|
93
|
|
94
|
private static final int mTotal = ObjectList.getTotal();
|
95
|
private static final String[][][] mCountry = new String[mTotal][MAX_LEVEL][MAX_PLACES];
|
96
|
private static final String[][][] mName = new String[mTotal][MAX_LEVEL][MAX_PLACES];
|
97
|
private static final float[][][] mTime = new float[mTotal][MAX_LEVEL][MAX_PLACES];
|
98
|
private static final int[][] mPlaces = new int[mTotal][MAX_LEVEL];
|
99
|
|
100
|
private static RubikNetwork mThis;
|
101
|
private static String mScores = "";
|
102
|
private static boolean mRunning = false;
|
103
|
private static int mMode = IDLE;
|
104
|
private static Receiver mReceiver;
|
105
|
private static String mVersion;
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
|
109
|
private static String computeHash(String stringToHash, byte[] salt)
|
110
|
{
|
111
|
String generatedPassword;
|
112
|
|
113
|
try
|
114
|
{
|
115
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
116
|
md.update(salt);
|
117
|
byte[] bytes = md.digest(stringToHash.getBytes());
|
118
|
StringBuilder sb = new StringBuilder();
|
119
|
|
120
|
for (byte aByte : bytes)
|
121
|
{
|
122
|
sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
|
123
|
}
|
124
|
|
125
|
generatedPassword = sb.toString();
|
126
|
}
|
127
|
catch (NoSuchAlgorithmException e)
|
128
|
{
|
129
|
return "NoSuchAlgorithm";
|
130
|
}
|
131
|
|
132
|
return generatedPassword;
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
private boolean fillValues()
|
138
|
{
|
139
|
int begin=-1 ,end, len = mScores.length();
|
140
|
String row;
|
141
|
|
142
|
if( len==0 )
|
143
|
{
|
144
|
mReceiver.error("1");
|
145
|
return false;
|
146
|
}
|
147
|
else if( len<=2 )
|
148
|
{
|
149
|
mReceiver.error(mScores);
|
150
|
return false;
|
151
|
}
|
152
|
|
153
|
for(int i=0; i<mTotal; i++)
|
154
|
for(int j=0; j<MAX_LEVEL; j++)
|
155
|
{
|
156
|
mPlaces[i][j] = 0;
|
157
|
}
|
158
|
|
159
|
while( begin<len )
|
160
|
{
|
161
|
end = mScores.indexOf('\n', begin+1);
|
162
|
if( end<0 ) end = len;
|
163
|
|
164
|
try
|
165
|
{
|
166
|
row = mScores.substring(begin+1,end);
|
167
|
fillRow(row);
|
168
|
}
|
169
|
catch(Exception ex)
|
170
|
{
|
171
|
// faulty row - ignore
|
172
|
}
|
173
|
|
174
|
begin = end;
|
175
|
}
|
176
|
|
177
|
return true;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
private void fillRow(String row)
|
183
|
{
|
184
|
int s1 = row.indexOf(' ');
|
185
|
int s2 = row.indexOf(' ',s1+1);
|
186
|
int s3 = row.indexOf(' ',s2+1);
|
187
|
int s4 = row.indexOf(' ',s3+1);
|
188
|
int s5 = row.length();
|
189
|
|
190
|
if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
|
191
|
{
|
192
|
int object = ObjectList.unpackObjectFromString( row.substring(0,s1) );
|
193
|
|
194
|
if( object>=0 && object<mTotal )
|
195
|
{
|
196
|
int level = Integer.parseInt( row.substring(s1+1,s2) );
|
197
|
String name = row.substring(s2+1, s3);
|
198
|
int time = Integer.parseInt( row.substring(s3+1,s4) );
|
199
|
String country = row.substring(s4+1, s5);
|
200
|
|
201
|
if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry()
|
202
|
|
203
|
if(level>=0 && level<MAX_LEVEL)
|
204
|
{
|
205
|
int p = mPlaces[object][level];
|
206
|
mPlaces[object][level]++;
|
207
|
|
208
|
mCountry[object][level][p] = country;
|
209
|
mName [object][level][p] = name;
|
210
|
mTime [object][level][p] = ((float)(time/10))/100.0f;
|
211
|
}
|
212
|
}
|
213
|
}
|
214
|
else
|
215
|
{
|
216
|
tryDoCommand(row);
|
217
|
}
|
218
|
}
|
219
|
|
220
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
221
|
|
222
|
private void tryDoCommand(String row)
|
223
|
{
|
224
|
if( row.startsWith("comm") )
|
225
|
{
|
226
|
int colon = row.indexOf(':');
|
227
|
|
228
|
if( colon>0 )
|
229
|
{
|
230
|
String commandNumber = row.substring(4,colon);
|
231
|
int number;
|
232
|
|
233
|
try
|
234
|
{
|
235
|
number = Integer.parseInt(commandNumber);
|
236
|
}
|
237
|
catch(NumberFormatException ex)
|
238
|
{
|
239
|
number=0;
|
240
|
}
|
241
|
|
242
|
if(number==1)
|
243
|
{
|
244
|
String country = row.substring(colon+1);
|
245
|
RubikScores scores = RubikScores.getInstance();
|
246
|
scores.setCountry(country);
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
}
|
251
|
|
252
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253
|
|
254
|
private String URLencode(String s)
|
255
|
{
|
256
|
StringBuilder sbuf = new StringBuilder();
|
257
|
int len = s.length();
|
258
|
|
259
|
for (int i = 0; i < len; i++)
|
260
|
{
|
261
|
int ch = s.charAt(i);
|
262
|
|
263
|
if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
|
264
|
else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
|
265
|
else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
|
266
|
else if (ch == ' ' ) sbuf.append('+');
|
267
|
else if (ch == '-' || ch == '_'
|
268
|
|| ch == '.' || ch == '!'
|
269
|
|| ch == '~' || ch == '*'
|
270
|
|| ch == '\'' || ch == '('
|
271
|
|| ch == ')' ) sbuf.append((char)ch);
|
272
|
else if (ch <= 0x007f) sbuf.append(hex[ch]);
|
273
|
else if (ch <= 0x07FF)
|
274
|
{
|
275
|
sbuf.append(hex[0xc0 | (ch >> 6)]);
|
276
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
277
|
}
|
278
|
else
|
279
|
{
|
280
|
sbuf.append(hex[0xe0 | (ch >> 12)]);
|
281
|
sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
|
282
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
283
|
}
|
284
|
}
|
285
|
|
286
|
return sbuf.toString();
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
|
291
|
private void sendDebug()
|
292
|
{
|
293
|
String url = constructDebugURL();
|
294
|
|
295
|
try
|
296
|
{
|
297
|
java.net.URL connectURL = new URL(url);
|
298
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
299
|
|
300
|
conn.setDoInput(true);
|
301
|
conn.setDoOutput(true);
|
302
|
conn.setUseCaches(false);
|
303
|
conn.setRequestMethod("GET");
|
304
|
conn.connect();
|
305
|
conn.getOutputStream().flush();
|
306
|
conn.getInputStream();
|
307
|
}
|
308
|
catch( final Exception e )
|
309
|
{
|
310
|
// ignore
|
311
|
}
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
private boolean network(String url)
|
317
|
{
|
318
|
try
|
319
|
{
|
320
|
java.net.URL connectURL = new URL(url);
|
321
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
322
|
|
323
|
conn.setDoInput(true);
|
324
|
conn.setDoOutput(true);
|
325
|
conn.setUseCaches(false);
|
326
|
conn.setRequestMethod("GET");
|
327
|
conn.connect();
|
328
|
conn.getOutputStream().flush();
|
329
|
|
330
|
try( InputStream is = conn.getInputStream() )
|
331
|
{
|
332
|
int ch;
|
333
|
StringBuilder sb = new StringBuilder();
|
334
|
while( ( ch = is.read() ) != -1 )
|
335
|
{
|
336
|
sb.append( (char)ch );
|
337
|
}
|
338
|
mScores = sb.toString();
|
339
|
}
|
340
|
catch( final Exception e)
|
341
|
{
|
342
|
mReceiver.message("Failed to get an answer from the High Scores server");
|
343
|
return false;
|
344
|
}
|
345
|
}
|
346
|
catch( final UnknownHostException e )
|
347
|
{
|
348
|
mReceiver.message("No access to Internet");
|
349
|
return false;
|
350
|
}
|
351
|
catch( final SecurityException e )
|
352
|
{
|
353
|
mReceiver.message("Application not authorized to connect to the Internet");
|
354
|
return false;
|
355
|
}
|
356
|
catch( final Exception e )
|
357
|
{
|
358
|
mReceiver.message(e.getMessage());
|
359
|
return false;
|
360
|
}
|
361
|
|
362
|
if( mScores.length()==0 )
|
363
|
{
|
364
|
mReceiver.message("Failed to download scores");
|
365
|
return false;
|
366
|
}
|
367
|
|
368
|
return true;
|
369
|
}
|
370
|
|
371
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
372
|
|
373
|
private String constructDebugURL()
|
374
|
{
|
375
|
RubikScores scores = RubikScores.getInstance();
|
376
|
String name = URLencode(scores.getName());
|
377
|
int numRuns = scores.getNumRuns();
|
378
|
int numPlay = scores.getNumPlays();
|
379
|
String country = scores.getCountry();
|
380
|
String renderer = DistortedLibrary.getDriverRenderer();
|
381
|
String version = DistortedLibrary.getDriverVersion();
|
382
|
|
383
|
renderer = URLencode(renderer);
|
384
|
version = URLencode(version);
|
385
|
|
386
|
String url="https://distorted.org/magic/cgi-bin/debugs.cgi";
|
387
|
url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d"+"&d="+renderer+"&v="+version;
|
388
|
|
389
|
return url;
|
390
|
}
|
391
|
|
392
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
393
|
|
394
|
private String constructDownloadURL()
|
395
|
{
|
396
|
RubikScores scores = RubikScores.getInstance();
|
397
|
String name = URLencode(scores.getName());
|
398
|
String veri = scores.isVerified() ? name : "";
|
399
|
int numRuns = scores.getNumRuns();
|
400
|
int numPlay = scores.getNumPlays();
|
401
|
String country = scores.getCountry();
|
402
|
|
403
|
String url="https://distorted.org/magic/cgi-bin/download.cgi";
|
404
|
url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
|
405
|
url += "&o="+ ObjectList.getObjectList()+"&min=0&max="+MAX_LEVEL+"&l="+MAX_PLACES;
|
406
|
|
407
|
return url;
|
408
|
}
|
409
|
|
410
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
411
|
|
412
|
private String constructSubmitURL()
|
413
|
{
|
414
|
RubikScores scores = RubikScores.getInstance();
|
415
|
String name = URLencode(scores.getName());
|
416
|
String veri = scores.isVerified() ? name : "";
|
417
|
int numRuns = scores.getNumRuns();
|
418
|
int numPlay = scores.getNumPlays();
|
419
|
int deviceID= scores.getDeviceID();
|
420
|
String reclist = scores.getRecordList("&o=","&l=","&t=");
|
421
|
String country = scores.getCountry();
|
422
|
long epoch = System.currentTimeMillis();
|
423
|
String salt = "cubold";
|
424
|
|
425
|
String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
|
426
|
String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
|
427
|
url2 += reclist+"&c="+country+"&f="+epoch+"&oo="+ ObjectList.getObjectList();
|
428
|
url2 += "&min=0&max="+MAX_LEVEL+"&lo="+MAX_PLACES;
|
429
|
String hash = computeHash( url2+"d", salt.getBytes() );
|
430
|
|
431
|
return url1 + "?" + url2 + "&h=" + hash;
|
432
|
}
|
433
|
|
434
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435
|
|
436
|
private boolean gottaDownload()
|
437
|
{
|
438
|
return ((mScores.length()==0) && !mRunning);
|
439
|
}
|
440
|
|
441
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
442
|
|
443
|
@Override
|
444
|
public void run()
|
445
|
{
|
446
|
boolean receiveValues=true;
|
447
|
|
448
|
try
|
449
|
{
|
450
|
if( mMode==DOWNLOAD && gottaDownload() )
|
451
|
{
|
452
|
mRunning = true;
|
453
|
receiveValues = network(constructDownloadURL());
|
454
|
}
|
455
|
if( mMode==SUBMIT )
|
456
|
{
|
457
|
mRunning = true;
|
458
|
|
459
|
if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
|
460
|
{
|
461
|
receiveValues = network(constructSubmitURL());
|
462
|
}
|
463
|
}
|
464
|
if( mMode==DEBUG )
|
465
|
{
|
466
|
sendDebug();
|
467
|
receiveValues = false;
|
468
|
mRunning = false;
|
469
|
}
|
470
|
}
|
471
|
catch( Exception e )
|
472
|
{
|
473
|
if( mReceiver!=null ) mReceiver.message("Exception downloading records: "+e.getMessage() );
|
474
|
}
|
475
|
|
476
|
if( mRunning )
|
477
|
{
|
478
|
receiveValues = fillValues();
|
479
|
mRunning = false;
|
480
|
}
|
481
|
|
482
|
if( receiveValues )
|
483
|
{
|
484
|
if( mReceiver!=null ) mReceiver.receive(mCountry, mName, mTime);
|
485
|
|
486
|
if( mMode==SUBMIT )
|
487
|
{
|
488
|
RubikScores.getInstance().successfulSubmit();
|
489
|
}
|
490
|
}
|
491
|
}
|
492
|
|
493
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
494
|
|
495
|
private RubikNetwork()
|
496
|
{
|
497
|
|
498
|
}
|
499
|
|
500
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
501
|
// PUBLIC API
|
502
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
503
|
|
504
|
public static void onPause()
|
505
|
{
|
506
|
mRunning = false;
|
507
|
}
|
508
|
|
509
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
510
|
|
511
|
public static RubikNetwork getInstance()
|
512
|
{
|
513
|
if( mThis==null )
|
514
|
{
|
515
|
mThis = new RubikNetwork();
|
516
|
}
|
517
|
|
518
|
return mThis;
|
519
|
}
|
520
|
|
521
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
522
|
|
523
|
private void start(Receiver receiver, Activity act, int mode)
|
524
|
{
|
525
|
mReceiver = receiver;
|
526
|
mMode = mode;
|
527
|
|
528
|
try
|
529
|
{
|
530
|
PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
|
531
|
mVersion = pInfo.versionName;
|
532
|
}
|
533
|
catch (PackageManager.NameNotFoundException e)
|
534
|
{
|
535
|
mVersion = "0.9.2";
|
536
|
}
|
537
|
|
538
|
Thread networkThrd = new Thread(this);
|
539
|
networkThrd.start();
|
540
|
}
|
541
|
|
542
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
543
|
|
544
|
public void download(Receiver receiver, FragmentActivity act)
|
545
|
{
|
546
|
start(receiver, act, DOWNLOAD);
|
547
|
}
|
548
|
|
549
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
550
|
|
551
|
public void submit(Receiver receiver, FragmentActivity act)
|
552
|
{
|
553
|
start(receiver, act, SUBMIT);
|
554
|
}
|
555
|
|
556
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
557
|
|
558
|
public void debug(AppCompatActivity act)
|
559
|
{
|
560
|
start(null, act, DEBUG);
|
561
|
}
|
562
|
}
|