Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wispborne committed Feb 5, 2024
1 parent 0fab828 commit 4baec5f
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 43 deletions.
190 changes: 151 additions & 39 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import 'dart:async';
import 'dart:io';

import 'package:desktop_window/desktop_window.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:starsector_rules_helper/poller.dart';
import 'package:starsector_rules_helper/utils.dart';
import 'package:window_size/window_size.dart';

const version = "0.0.1";
const version = "1.0.0";
const appTitle = "Rules Reloader v$version";

void main() {
const appSubtitle = "by Wisp";

void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Size size = await DesktopWindow.getWindowSize();
const height = 350.0;
const width = (4 / 3) * height;
DesktopWindow.setWindowSize(const Size(width, height));
runApp(const MyApp());
setWindowTitle(appTitle);
}
Expand All @@ -25,15 +34,16 @@ class MyApp extends StatelessWidget {
seedColor: Colors.blue, brightness: Brightness.dark),
useMaterial3: true,
),
home: const MyHomePage(title: appTitle),
home: const MyHomePage(title: appTitle, subtitle: appSubtitle),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
const MyHomePage({super.key, required this.title, required this.subtitle});

final String title;
final String subtitle;

@override
State<MyHomePage> createState() => _MyHomePageState();
Expand All @@ -43,62 +53,164 @@ class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
int _modsBeingWatched = 0;

late Directory gamePath;
late Directory gameFiles;
late File vanillaRulesCsv;
late Directory modsFolder;
late List<File> modRulesCsvs;
late SharedPreferences _prefs;
Directory? gamePath = defaultGamePath();
Directory? gameFiles = null;
File? vanillaRulesCsv = null;
Directory? modsFolder = null;
List<File> modRulesCsvs = [];
final gamePathTextController = TextEditingController();
String? pathError;

@override
void initState() {
super.initState();

gamePath = defaultGamePath()!;
gameFiles = gameFilesPath(gamePath)!;
vanillaRulesCsv = getVanillaRulesCsvInGameFiles(gameFiles);
modsFolder = modFolderPath(gamePath)!;
modRulesCsvs = getAllRulesCsvsInModsFolder(modsFolder);

_modsBeingWatched = modRulesCsvs.length;

for (var element in modRulesCsvs) {
pollFileForModification(element, 1).listen((event) {
_saveVanillaRulesCsv();
SharedPreferences.getInstance().then((prefs) {
_prefs = prefs;
setState(() {
gamePath =
Directory(_prefs.getString('gamePath') ?? defaultGamePath()!.path);
if (!gamePath!.existsSync()) {
gamePath = Directory(defaultGamePath()!.path);
}
});
}
_updatePaths();
});
}

_do() async {
_prefs = await SharedPreferences.getInstance();
setState(() {
gamePath =
// Directory(_prefs.getString('gamePath') ?? defaultGamePath()!.path);
Directory(defaultGamePath()!.path);
});
_updatePaths();
}

_saveVanillaRulesCsv() {
vanillaRulesCsv.setLastModified(DateTime.now());
vanillaRulesCsv?.setLastModified(DateTime.now());

setState(() {
_counter++;
});
}

_updatePaths() {
setState(() {
gameFiles = gameFilesPath(gamePath!)!;
vanillaRulesCsv = getVanillaRulesCsvInGameFiles(gameFiles!);
modsFolder = modFolderPath(gamePath!)!;
modRulesCsvs = getAllRulesCsvsInModsFolder(modsFolder!);

gamePathTextController.text = gamePath!.path;
_modsBeingWatched = modRulesCsvs.length;
});

fileChanges.close();
fileChanges = StreamController();

fileChanges.stream.listen((event) {
_saveVanillaRulesCsv();
});

for (var element in modRulesCsvs) {
pollFileForModification(element, 1);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
title: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(widget.title, style: Theme.of(context).textTheme.titleLarge),
Text(widget.subtitle, style: Theme.of(context).textTheme.bodyMedium)
]),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Watching $_modsBeingWatched rules.csv files.',
),
const SizedBox(height: 20),
const Text(
'Vanilla rules.csv updated this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Spacer(),
RichText(
text: TextSpan(
text: 'Watching ',
children: [
TextSpan(
text: '$_modsBeingWatched',
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
const TextSpan(text: ' rules.csv files for changes.'),
],
),
),
const SizedBox(height: 20),
const Text(
'Vanilla rules.csv auto-reloaded',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
const Text(
'times',
),
const Spacer(),
Column(
children: [
Row(
children: [
Expanded(
child: TextField(
controller: gamePathTextController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Starsector Folder',
),
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: OutlinedButton(
onPressed: () {
if (!Directory(gamePathTextController.text)
.existsSync()) {
setState(() {
pathError = "Invalid path";
});
return;
}
setState(() {
pathError = null;
});
_prefs.setString(
'gamePath', gamePathTextController.text);
setState(() {
gamePath = Directory(gamePathTextController.text);
});
_updatePaths();
},
child: const Text('Set'),
),
),
],
),
if (pathError != null)
Text(
pathError!,
style: const TextStyle(color: Colors.red),
),
],
),
],
),
),
),
);
Expand Down
11 changes: 8 additions & 3 deletions lib/poller.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import 'dart:async';
import 'dart:io';

Stream<int> pollFileForModification(File file, int interval) async* {
var fileChanges = StreamController();

/// Should probably be a way to stop this.
pollFileForModification(File file, int interval) async {
var lastModified = file.lastModifiedSync();
final fileChangesInstance = fileChanges;

while (true) {
while (!fileChangesInstance.isClosed) {
await Future.delayed(Duration(seconds: interval));
final newModified = file.lastModifiedSync();

if (newModified.isAfter(lastModified)) {
lastModified = newModified;
yield 1;
fileChanges.add(file);
}
}
}
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

#include "generated_plugin_registrant.h"

#include <desktop_window/desktop_window_plugin.h>
#include <window_size/window_size_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) desktop_window_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "DesktopWindowPlugin");
desktop_window_plugin_register_with_registrar(desktop_window_registrar);
g_autoptr(FlPluginRegistrar) window_size_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "WindowSizePlugin");
window_size_plugin_register_with_registrar(window_size_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
desktop_window
window_size
)

Expand Down
4 changes: 4 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import FlutterMacOS
import Foundation

import desktop_window
import shared_preferences_foundation
import window_size

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
DesktopWindowPlugin.register(with: registry.registrar(forPlugin: "DesktopWindowPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
WindowSizePlugin.register(with: registry.registrar(forPlugin: "WindowSizePlugin"))
}
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
dart_extensions_methods: ^2.0.0
# path: ^1.9.0
# path: ^1.9.0
file_picker: ^6.1.1
desktop_window: ^0.4.0
window_size:
git:
url: https://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
ref: 5c51870ced62a00e809ba4b81a846a052d241c9f
shared_preferences: ^2.2.2

dev_dependencies:
flutter_test:
Expand Down
6 changes: 6 additions & 0 deletions starsector_rules_helper.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/linux/flutter/ephemeral/.plugin_symlinks/window_size/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/linux/flutter/ephemeral/.plugin_symlinks/window_size/.pub" />
<excludeFolder url="file://$MODULE_DIR$/linux/flutter/ephemeral/.plugin_symlinks/window_size/build" />
<excludeFolder url="file://$MODULE_DIR$/windows/flutter/ephemeral/.plugin_symlinks/window_size/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/windows/flutter/ephemeral/.plugin_symlinks/window_size/.pub" />
<excludeFolder url="file://$MODULE_DIR$/windows/flutter/ephemeral/.plugin_symlinks/window_size/build" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

#include "generated_plugin_registrant.h"

#include <desktop_window/desktop_window_plugin.h>
#include <window_size/window_size_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktopWindowPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktopWindowPlugin"));
WindowSizePluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowSizePlugin"));
}
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
desktop_window
window_size
)

Expand Down

0 comments on commit 4baec5f

Please sign in to comment.