MCSI-8 feat: installation presenter and refactor installation process to use a coordinating use case
This commit is contained in:
parent
8b6fcad851
commit
5396358687
@ -2,44 +2,31 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:minecraft_server_installer/main/adapter/presenter/installation_state.dart';
|
||||
import 'package:minecraft_server_installer/main/adapter/presenter/progress_view_model.dart';
|
||||
import 'package:minecraft_server_installer/main/adapter/presenter/range_view_model.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/download_file_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/grant_file_permission_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/write_file_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/constants.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/install_server_use_case.dart';
|
||||
import 'package:minecraft_server_installer/properties/adapter/presenter/server_properties_view_model.dart';
|
||||
import 'package:minecraft_server_installer/vanilla/adapter/presenter/game_version_view_model.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
class InstallationBloc extends Bloc<InstallationEvent, InstallationState> {
|
||||
InstallationBloc(
|
||||
DownloadFileUseCase downloadFileUseCase,
|
||||
WriteFileUseCase writeFileUseCase,
|
||||
GrantFilePermissionUseCase grantFilePermissionUseCase,
|
||||
InstallServerUseCase installServerUseCase,
|
||||
) : super(const InstallationState.empty()) {
|
||||
on<InstallationStartedEvent>((_, emit) async {
|
||||
on<InstallationStartedEvent>((event, emit) async {
|
||||
if (!state.canStartToInstall) {
|
||||
return;
|
||||
}
|
||||
|
||||
final gameVersion = state.gameVersion!;
|
||||
final savePath = state.savePath!;
|
||||
|
||||
emit(state.copyWith(isLocked: true, downloadProgress: const ProgressViewModel.start()));
|
||||
|
||||
await downloadFileUseCase(
|
||||
gameVersion.url,
|
||||
path.join(savePath, Constants.serverFileName),
|
||||
await installServerUseCase(
|
||||
gameVersion: state.gameVersion!.toEntity(),
|
||||
savePath: state.savePath!,
|
||||
maxRam: state.ramSize.max,
|
||||
minRam: state.ramSize.min,
|
||||
isGuiEnabled: state.isGuiEnabled,
|
||||
serverProperties: event.serverProperties.toEntity(),
|
||||
onProgressChanged: (progressValue) => add(_InstallationProgressValueChangedEvent(progressValue)),
|
||||
);
|
||||
|
||||
final startScriptFilePath = path.join(savePath, Constants.startScriptFileName);
|
||||
final serverFilePath = path.join('.', Constants.serverFileName);
|
||||
final startScriptContent =
|
||||
'java -Xmx${state.ramSize.max}M -Xms${state.ramSize.min}M -jar $serverFilePath ${state.isGuiEnabled ? '' : 'nogui'}';
|
||||
await writeFileUseCase(startScriptFilePath, startScriptContent);
|
||||
await grantFilePermissionUseCase(startScriptFilePath);
|
||||
|
||||
await writeFileUseCase(path.join(savePath, Constants.eulaFileName), Constants.eulaFileContent);
|
||||
|
||||
emit(state.copyWith(isLocked: false, downloadProgress: const ProgressViewModel.complete()));
|
||||
});
|
||||
|
||||
@ -77,7 +64,11 @@ class InstallationBloc extends Bloc<InstallationEvent, InstallationState> {
|
||||
|
||||
sealed class InstallationEvent {}
|
||||
|
||||
class InstallationStartedEvent extends InstallationEvent {}
|
||||
class InstallationStartedEvent extends InstallationEvent {
|
||||
final ServerPropertiesViewModel serverProperties;
|
||||
|
||||
InstallationStartedEvent(this.serverProperties);
|
||||
}
|
||||
|
||||
class _InstallationProgressValueChangedEvent extends InstallationEvent {
|
||||
final double progressValue;
|
||||
|
53
lib/main/application/use_case/install_server_use_case.dart
Normal file
53
lib/main/application/use_case/install_server_use_case.dart
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
import 'package:minecraft_server_installer/main/application/use_case/download_file_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/grant_file_permission_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/write_file_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/constants.dart';
|
||||
import 'package:minecraft_server_installer/properties/application/use_case/write_server_properties_use_case.dart';
|
||||
import 'package:minecraft_server_installer/properties/domain/entity/server_properties.dart';
|
||||
import 'package:minecraft_server_installer/vanilla/domain/entity/game_version.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
class InstallServerUseCase {
|
||||
final DownloadFileUseCase _downloadFileUseCase;
|
||||
final WriteFileUseCase _writeFileUseCase;
|
||||
final GrantFilePermissionUseCase _grantFilePermissionUseCase;
|
||||
final WriteServerPropertiesUseCase _writeServerPropertiesUseCase;
|
||||
|
||||
InstallServerUseCase(
|
||||
this._downloadFileUseCase,
|
||||
this._writeFileUseCase,
|
||||
this._grantFilePermissionUseCase,
|
||||
this._writeServerPropertiesUseCase,
|
||||
);
|
||||
|
||||
Future<void> call({
|
||||
required GameVersion gameVersion,
|
||||
required String savePath,
|
||||
required int maxRam,
|
||||
required int minRam,
|
||||
required bool isGuiEnabled,
|
||||
required ServerProperties serverProperties,
|
||||
required void Function(double) onProgressChanged,
|
||||
}) async {
|
||||
// 1. Download server file
|
||||
await _downloadFileUseCase(
|
||||
gameVersion.url,
|
||||
path.join(savePath, Constants.serverFileName),
|
||||
onProgressChanged: onProgressChanged,
|
||||
);
|
||||
|
||||
// 2. Write start script
|
||||
final startScriptFilePath = path.join(savePath, Constants.startScriptFileName);
|
||||
final serverFilePath = path.join('.', Constants.serverFileName);
|
||||
final startScriptContent = 'java -Xmx${maxRam}M -Xms${minRam}M -jar $serverFilePath ${isGuiEnabled ? '' : 'nogui'}';
|
||||
await _writeFileUseCase(startScriptFilePath, startScriptContent);
|
||||
await _grantFilePermissionUseCase(startScriptFilePath);
|
||||
|
||||
// 3. Write EULA file
|
||||
await _writeFileUseCase(path.join(savePath, Constants.eulaFileName), Constants.eulaFileContent);
|
||||
|
||||
// 4. Write server.properties file
|
||||
await _writeServerPropertiesUseCase(serverProperties, savePath);
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import 'package:minecraft_server_installer/main/adapter/presenter/installation_s
|
||||
import 'package:minecraft_server_installer/main/adapter/presenter/range_view_model.dart';
|
||||
import 'package:minecraft_server_installer/main/constants.dart';
|
||||
import 'package:minecraft_server_installer/main/framework/ui/strings.dart';
|
||||
import 'package:minecraft_server_installer/properties/adapter/presenter/server_properties_bloc.dart';
|
||||
import 'package:minecraft_server_installer/vanilla/framework/ui/game_version_dropdown.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
@ -209,6 +210,6 @@ class BasicConfigurationTab extends StatelessWidget {
|
||||
}
|
||||
|
||||
void _downloadServerFile(BuildContext context) {
|
||||
context.read<InstallationBloc>().add((InstallationStartedEvent()));
|
||||
context.read<InstallationBloc>().add((InstallationStartedEvent(context.read<ServerPropertiesBloc>().state)));
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import 'package:minecraft_server_installer/main/adapter/presenter/installation_b
|
||||
import 'package:minecraft_server_installer/main/adapter/presenter/navigation_bloc.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/download_file_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/grant_file_permission_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/install_server_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/application/use_case/write_file_use_case.dart';
|
||||
import 'package:minecraft_server_installer/main/constants.dart';
|
||||
import 'package:minecraft_server_installer/main/framework/api/installation_api_service_impl.dart';
|
||||
@ -12,6 +13,10 @@ import 'package:minecraft_server_installer/main/framework/storage/installation_f
|
||||
import 'package:minecraft_server_installer/main/framework/ui/about_tab.dart';
|
||||
import 'package:minecraft_server_installer/main/framework/ui/basic_configuration_tab.dart';
|
||||
import 'package:minecraft_server_installer/main/framework/ui/side_navigation_bar.dart';
|
||||
import 'package:minecraft_server_installer/properties/adapter/gateway/server_properties_repository_impl.dart';
|
||||
import 'package:minecraft_server_installer/properties/adapter/presenter/server_properties_bloc.dart';
|
||||
import 'package:minecraft_server_installer/properties/application/use_case/write_server_properties_use_case.dart';
|
||||
import 'package:minecraft_server_installer/properties/framework/storage/server_properties_file_storage_impl.dart';
|
||||
import 'package:minecraft_server_installer/vanilla/adapter/gateway/vanilla_repository_impl.dart';
|
||||
import 'package:minecraft_server_installer/vanilla/adapter/presenter/vanilla_bloc.dart';
|
||||
import 'package:minecraft_server_installer/vanilla/application/use_case/get_game_version_list_use_case.dart';
|
||||
@ -22,17 +27,27 @@ class MinecraftServerInstaller extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final gameVersionApiService = VanillaApiServiceImpl();
|
||||
final gameVersionRepository = VanillaRepositoryImpl(gameVersionApiService);
|
||||
final getGameVersionListUseCase = GetGameVersionListUseCase(gameVersionRepository);
|
||||
|
||||
final serverPropertiesFileStorage = ServerPropertiesFileStorageImpl();
|
||||
final serverPropertiesRepository = ServerPropertiesRepositoryImpl(serverPropertiesFileStorage);
|
||||
final writeServerPropertiesUseCase = WriteServerPropertiesUseCase(serverPropertiesRepository);
|
||||
|
||||
final installationApiService = InstallationApiServiceImpl();
|
||||
final installationFileStorage = InstallationFileStorageImpl();
|
||||
final installationRepository = InstallationRepositoryImpl(installationApiService, installationFileStorage);
|
||||
|
||||
final gameVersionApiService = VanillaApiServiceImpl();
|
||||
final gameVersionRepository = VanillaRepositoryImpl(gameVersionApiService);
|
||||
|
||||
final downloadFileUseCase = DownloadFileUseCase(installationRepository);
|
||||
final writeFileUseCase = WriteFileUseCase(installationRepository);
|
||||
final grantFilePermissionUseCase = GrantFilePermissionUseCase(installationRepository);
|
||||
final getGameVersionListUseCase = GetGameVersionListUseCase(gameVersionRepository);
|
||||
|
||||
final installServerUseCase = InstallServerUseCase(
|
||||
downloadFileUseCase,
|
||||
writeFileUseCase,
|
||||
grantFilePermissionUseCase,
|
||||
writeServerPropertiesUseCase,
|
||||
);
|
||||
|
||||
return MaterialApp(
|
||||
title: Constants.appName,
|
||||
@ -45,16 +60,13 @@ class MinecraftServerInstaller extends StatelessWidget {
|
||||
home: MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(create: (_) => NavigationBloc()),
|
||||
BlocProvider(
|
||||
create: (_) => InstallationBloc(
|
||||
downloadFileUseCase,
|
||||
writeFileUseCase,
|
||||
grantFilePermissionUseCase,
|
||||
),
|
||||
),
|
||||
BlocProvider(create: (_) => ServerPropertiesBloc()),
|
||||
BlocProvider<VanillaBloc>(
|
||||
create: (_) => VanillaBloc(getGameVersionListUseCase)..add(VanillaGameVersionListLoadedEvent()),
|
||||
),
|
||||
BlocProvider(
|
||||
create: (_) => InstallationBloc(installServerUseCase),
|
||||
),
|
||||
],
|
||||
child: Scaffold(
|
||||
body: Row(
|
||||
|
@ -28,16 +28,18 @@ class ServerPropertiesDto {
|
||||
});
|
||||
|
||||
ServerPropertiesDto.fromEntity(ServerProperties serverProperties)
|
||||
: serverPort = serverProperties.serverPort,
|
||||
maxPlayers = serverProperties.maxPlayers,
|
||||
spawnProtection = serverProperties.spawnProtection,
|
||||
viewDistance = serverProperties.viewDistance,
|
||||
pvp = serverProperties.pvp,
|
||||
gameMode = serverProperties.gameMode.value,
|
||||
difficulty = serverProperties.difficulty.value,
|
||||
enableCommandBlock = serverProperties.enableCommandBlock,
|
||||
onlineMode = serverProperties.onlineMode,
|
||||
motd = serverProperties.motd;
|
||||
: this(
|
||||
serverPort: serverProperties.serverPort,
|
||||
maxPlayers: serverProperties.maxPlayers,
|
||||
spawnProtection: serverProperties.spawnProtection,
|
||||
viewDistance: serverProperties.viewDistance,
|
||||
pvp: serverProperties.pvp,
|
||||
gameMode: serverProperties.gameMode.value,
|
||||
difficulty: serverProperties.difficulty.value,
|
||||
enableCommandBlock: serverProperties.enableCommandBlock,
|
||||
onlineMode: serverProperties.onlineMode,
|
||||
motd: serverProperties.motd,
|
||||
);
|
||||
|
||||
Map<String, String> toStringMap() => {
|
||||
'server-port': serverPort.toString(),
|
||||
|
@ -1,5 +1,5 @@
|
||||
import 'package:minecraft_server_installer/properties/adapter/gateway/server_properties_dto.dart';
|
||||
|
||||
abstract interface class ServerPropertiesFileStorage {
|
||||
Future<void> writeServerProperties(ServerPropertiesDto serverPropertiesDto, String directoryPath);
|
||||
Future<void> writeServerProperties(ServerPropertiesDto serverPropertiesDto, String savePath);
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ class ServerPropertiesRepositoryImpl implements ServerPropertiesRepository {
|
||||
ServerPropertiesRepositoryImpl(this._serverPropertiesFileStorage);
|
||||
|
||||
@override
|
||||
Future<void> writeServerProperties(ServerProperties serverProperties, String directoryPath) =>
|
||||
Future<void> writeServerProperties(ServerProperties serverProperties, String savePath) =>
|
||||
_serverPropertiesFileStorage.writeServerProperties(
|
||||
ServerPropertiesDto.fromEntity(serverProperties),
|
||||
directoryPath,
|
||||
savePath,
|
||||
);
|
||||
}
|
||||
|
51
lib/properties/adapter/presenter/server_properties_bloc.dart
Normal file
51
lib/properties/adapter/presenter/server_properties_bloc.dart
Normal file
@ -0,0 +1,51 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:minecraft_server_installer/properties/adapter/presenter/server_properties_view_model.dart';
|
||||
import 'package:minecraft_server_installer/properties/domain/enum/difficulty.dart';
|
||||
import 'package:minecraft_server_installer/properties/domain/enum/game_mode.dart';
|
||||
|
||||
class ServerPropertiesBloc extends Bloc<ServerPropertiesEvent, ServerPropertiesViewModel> {
|
||||
ServerPropertiesBloc() : super(const ServerPropertiesViewModel.defaultValue()) {
|
||||
on<ServerPropertiesUpdatedEvent>((event, emit) => emit(
|
||||
state.copyWith(
|
||||
serverPort: event.serverPort ?? state.serverPort,
|
||||
maxPlayers: event.maxPlayers ?? state.maxPlayers,
|
||||
spawnProtection: event.spawnProtection ?? state.spawnProtection,
|
||||
viewDistance: event.viewDistance ?? state.viewDistance,
|
||||
pvp: event.pvp ?? state.pvp,
|
||||
gameMode: event.gameMode ?? state.gameMode,
|
||||
difficulty: event.difficulty ?? state.difficulty,
|
||||
enableCommandBlock: event.enableCommandBlock ?? state.enableCommandBlock,
|
||||
onlineMode: event.onlineMode ?? state.onlineMode,
|
||||
motd: event.motd ?? state.motd,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
sealed class ServerPropertiesEvent {}
|
||||
|
||||
class ServerPropertiesUpdatedEvent extends ServerPropertiesEvent {
|
||||
final int? serverPort;
|
||||
final int? maxPlayers;
|
||||
final int? spawnProtection;
|
||||
final int? viewDistance;
|
||||
final bool? pvp;
|
||||
final GameMode? gameMode;
|
||||
final Difficulty? difficulty;
|
||||
final bool? enableCommandBlock;
|
||||
final bool? onlineMode;
|
||||
final String? motd;
|
||||
|
||||
ServerPropertiesUpdatedEvent({
|
||||
this.serverPort,
|
||||
this.maxPlayers,
|
||||
this.spawnProtection,
|
||||
this.viewDistance,
|
||||
this.pvp,
|
||||
this.gameMode,
|
||||
this.difficulty,
|
||||
this.enableCommandBlock,
|
||||
this.onlineMode,
|
||||
this.motd,
|
||||
});
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:minecraft_server_installer/properties/domain/entity/server_properties.dart';
|
||||
import 'package:minecraft_server_installer/properties/domain/enum/difficulty.dart';
|
||||
import 'package:minecraft_server_installer/properties/domain/enum/game_mode.dart';
|
||||
|
||||
class ServerPropertiesViewModel with EquatableMixin {
|
||||
final int serverPort;
|
||||
final int maxPlayers;
|
||||
final int spawnProtection;
|
||||
final int viewDistance;
|
||||
final bool pvp;
|
||||
final GameMode gameMode;
|
||||
final Difficulty difficulty;
|
||||
final bool enableCommandBlock;
|
||||
final bool onlineMode;
|
||||
final String motd;
|
||||
|
||||
const ServerPropertiesViewModel({
|
||||
required this.serverPort,
|
||||
required this.maxPlayers,
|
||||
required this.spawnProtection,
|
||||
required this.viewDistance,
|
||||
required this.pvp,
|
||||
required this.gameMode,
|
||||
required this.difficulty,
|
||||
required this.enableCommandBlock,
|
||||
required this.onlineMode,
|
||||
required this.motd,
|
||||
});
|
||||
|
||||
const ServerPropertiesViewModel.defaultValue()
|
||||
: this(
|
||||
serverPort: 25565,
|
||||
maxPlayers: 20,
|
||||
spawnProtection: 16,
|
||||
viewDistance: 10,
|
||||
pvp: true,
|
||||
gameMode: GameMode.survival,
|
||||
difficulty: Difficulty.normal,
|
||||
enableCommandBlock: false,
|
||||
onlineMode: true,
|
||||
motd: 'A Minecraft Server',
|
||||
);
|
||||
|
||||
ServerProperties toEntity() => ServerProperties(
|
||||
serverPort: serverPort,
|
||||
maxPlayers: maxPlayers,
|
||||
spawnProtection: spawnProtection,
|
||||
viewDistance: viewDistance,
|
||||
pvp: pvp,
|
||||
gameMode: gameMode,
|
||||
difficulty: difficulty,
|
||||
enableCommandBlock: enableCommandBlock,
|
||||
onlineMode: onlineMode,
|
||||
motd: motd,
|
||||
);
|
||||
|
||||
ServerPropertiesViewModel copyWith({
|
||||
int? serverPort,
|
||||
int? maxPlayers,
|
||||
int? spawnProtection,
|
||||
int? viewDistance,
|
||||
bool? pvp,
|
||||
GameMode? gameMode,
|
||||
Difficulty? difficulty,
|
||||
bool? enableCommandBlock,
|
||||
bool? onlineMode,
|
||||
String? motd,
|
||||
}) =>
|
||||
ServerPropertiesViewModel(
|
||||
serverPort: serverPort ?? this.serverPort,
|
||||
maxPlayers: maxPlayers ?? this.maxPlayers,
|
||||
spawnProtection: spawnProtection ?? this.spawnProtection,
|
||||
viewDistance: viewDistance ?? this.viewDistance,
|
||||
pvp: pvp ?? this.pvp,
|
||||
gameMode: gameMode ?? this.gameMode,
|
||||
difficulty: difficulty ?? this.difficulty,
|
||||
enableCommandBlock: enableCommandBlock ?? this.enableCommandBlock,
|
||||
onlineMode: onlineMode ?? this.onlineMode,
|
||||
motd: motd ?? this.motd,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
serverPort,
|
||||
maxPlayers,
|
||||
spawnProtection,
|
||||
viewDistance,
|
||||
pvp,
|
||||
gameMode,
|
||||
difficulty,
|
||||
enableCommandBlock,
|
||||
onlineMode,
|
||||
motd,
|
||||
];
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import 'package:minecraft_server_installer/properties/domain/entity/server_properties.dart';
|
||||
|
||||
abstract interface class ServerPropertiesRepository {
|
||||
Future<void> writeServerProperties(ServerProperties serverProperties, String directoryPath);
|
||||
Future<void> writeServerProperties(ServerProperties serverProperties, String savePath);
|
||||
}
|
||||
|
@ -6,6 +6,6 @@ class WriteServerPropertiesUseCase {
|
||||
|
||||
WriteServerPropertiesUseCase(this._serverPropertiesRepository);
|
||||
|
||||
Future<void> call(ServerProperties serverProperties, String directoryPath) =>
|
||||
_serverPropertiesRepository.writeServerProperties(serverProperties, directoryPath);
|
||||
Future<void> call(ServerProperties serverProperties, String savePath) =>
|
||||
_serverPropertiesRepository.writeServerProperties(serverProperties, savePath);
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ import 'package:minecraft_server_installer/properties/adapter/gateway/server_pro
|
||||
|
||||
class ServerPropertiesFileStorageImpl implements ServerPropertiesFileStorage {
|
||||
@override
|
||||
Future<void> writeServerProperties(ServerPropertiesDto serverPropertiesDto, String directoryPath) async {
|
||||
File file = File('$directoryPath/server.properties');
|
||||
Future<void> writeServerProperties(ServerPropertiesDto serverPropertiesDto, String savePath) async {
|
||||
File file = File('$savePath/server.properties');
|
||||
await file.create(recursive: true);
|
||||
|
||||
final propertiesMap = serverPropertiesDto.toStringMap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user