Skip to content

Commit

Permalink
perfect: Built-in Perfect DB files for the first 8 moves
Browse files Browse the repository at this point in the history
When enable Perfect DB, copy these files to strong dir.
  • Loading branch information
calcitem committed May 2, 2024
1 parent b4cd2dc commit afa16d1
Show file tree
Hide file tree
Showing 20 changed files with 617 additions and 0 deletions.
501 changes: 501 additions & 0 deletions src/ui/flutter_app/assets/databases/std.secval

Large diffs are not rendered by default.

Binary file not shown.
Binary file added src/ui/flutter_app/assets/databases/std_0_1_9_8.sec2
Binary file not shown.
Binary file added src/ui/flutter_app/assets/databases/std_1_1_8_8.sec2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import '../../shared/config/constants.dart';
import '../../shared/database/database.dart';
import '../../shared/services/environment_config.dart';
import '../../shared/services/logger.dart';
import '../../shared/services/perfect_database_service.dart';
import '../../shared/services/url.dart';
import '../../shared/themes/app_theme.dart';
import '../../shared/widgets/settings/settings.dart';
Expand Down Expand Up @@ -129,6 +130,10 @@ class GeneralSettingsPage extends StatelessWidget {
DB().generalSettings = generalSettings.copyWith(usePerfectDatabase: value);

logger.v("$_logTag usePerfectDatabase: $value");

if (value == true) {
copyPerfectDatabaseFiles();
}
}

void _showUsePerfectDatabaseDialog(BuildContext context) => showDialog(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// This file is part of Sanmill.
// Copyright (C) 2019-2024 The Sanmill developers (see AUTHORS file)
//
// Sanmill is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Sanmill is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

import 'logger.dart';

Future<bool> copyPerfectDatabaseFiles() async {
Directory? dir;
try {
dir = (!kIsWeb && Platform.isAndroid)
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
logger.i('Directory obtained: ${dir?.path}');
} catch (e) {
logger.e('Failed to get directory: $e');
return false;
}

final String perfectDatabasePath = '${dir?.path}/strong';
final Directory directory = Directory(perfectDatabasePath);

try {
if (!await _exists(directory)) {
await directory.create(recursive: true);
logger.i('Created directory at $perfectDatabasePath');
}
} catch (e) {
logger.e('Error creating directory $perfectDatabasePath: $e');
return false;
}

// List of asset files to copy
final List<String> assetFiles = <String>[
'assets/databases/std.secval',
'assets/databases/std_0_0_9_9.sec2',
'assets/databases/std_0_1_9_8.sec2',
'assets/databases/std_1_1_8_8.sec2',
'assets/databases/std_1_2_8_7.sec2',
'assets/databases/std_1_3_7_6.sec2',
'assets/databases/std_2_2_7_7.sec2',
'assets/databases/std_2_3_6_6.sec2',
'assets/databases/std_2_3_7_6.sec2',
'assets/databases/std_2_4_6_5.sec2',
'assets/databases/std_3_3_5_5.sec2',
'assets/databases/std_3_3_6_5.sec2',
'assets/databases/std_3_3_6_6.sec2',
'assets/databases/std_3_4_5_5.sec2',
'assets/databases/std_3_4_6_5.sec2',
'assets/databases/std_4_3_5_5.sec2',
'assets/databases/std_4_4_5_5.sec2'
];

for (final String asset in assetFiles) {
try {
final File file = File('${directory.path}/${asset.split('/').last}');
if (!await _exists(file)) {
final ByteData byteData = await rootBundle.load(asset);
final ByteBuffer buffer = byteData.buffer;
await file.writeAsBytes(
buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
logger.i('Copied file to ${file.path}');
} else {
logger.i('File already exists and was not overwritten: ${file.path}');
}
} catch (e) {
logger.e('Failed to copy asset $asset: $e');
return false;
}
}

return true;
}

Future<bool> _exists(FileSystemEntity entity) async {
return entity.exists();
}
17 changes: 17 additions & 0 deletions src/ui/flutter_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ flutter:
- assets/images/background_image_3.jpg
- assets/images/background_image_4.jpg
- assets/images/background_image_5.jpg
- assets/databases/std.secval
- assets/databases/std_0_0_9_9.sec2
- assets/databases/std_0_1_9_8.sec2
- assets/databases/std_1_1_8_8.sec2
- assets/databases/std_1_2_8_7.sec2
- assets/databases/std_1_3_7_6.sec2
- assets/databases/std_2_2_7_7.sec2
- assets/databases/std_2_3_6_6.sec2
- assets/databases/std_2_3_7_6.sec2
- assets/databases/std_2_4_6_5.sec2
- assets/databases/std_3_3_5_5.sec2
- assets/databases/std_3_3_6_5.sec2
- assets/databases/std_3_3_6_6.sec2
- assets/databases/std_3_4_5_5.sec2
- assets/databases/std_3_4_6_5.sec2
- assets/databases/std_4_3_5_5.sec2
- assets/databases/std_4_4_5_5.sec2

flutter_intl:
enabled: true
Expand Down

0 comments on commit afa16d1

Please sign in to comment.