Skip to content

Commit

Permalink
refactored utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHeitmann committed Oct 29, 2022
1 parent 2ac28af commit a7c1d97
Show file tree
Hide file tree
Showing 58 changed files with 187 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ app.*.map.json
/android/app/release

.vscode
extractedFonts
extractedFonts
assets/bins/*
2 changes: 1 addition & 1 deletion lib/background/IdLookup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../stateManagement/miscValues.dart';
import '../stateManagement/openFileTypes.dart';
import '../stateManagement/openFilesManager.dart';
import '../utils.dart';
import '../utils/utils.dart';
import 'IdsIndexer.dart';
import 'Initializable.dart';
import 'isolateCommunicator.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/background/IdsIndexer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:xml/xml.dart';
import '../../fileTypeUtils/dat/datExtractor.dart';
import '../../fileTypeUtils/utils/ByteDataWrapper.dart';
import '../../fileTypeUtils/yax/yaxToXml.dart';
import '../../utils.dart';
import '../utils/utils.dart';
import '../fileTypeUtils/pak/pakExtractor.dart';
import '../fileTypeUtils/yax/hashToStringMap.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/background/isolateCommunicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'dart:isolate';

import '../stateManagement/statusInfo.dart';
import '../utils.dart';
import '../utils/utils.dart';
import 'IdsIndexer.dart';
import 'Initializable.dart';
import 'IndexingGroup.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/background/searchService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:xml/xml.dart';
import '../fileTypeUtils/tmd/tmdReader.dart';
import '../fileTypeUtils/yax/hashToStringMap.dart';
import '../stateManagement/Property.dart';
import '../utils.dart';
import '../utils/utils.dart';
import 'IdLookup.dart';
import 'IdsIndexer.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/fileTypeUtils/dat/datHashGenerator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import '../../utils.dart';
import '../../utils/utils.dart';

// From https://github.com/xxk-i/DATrepacker
class HashInfo {
Expand Down
2 changes: 1 addition & 1 deletion lib/fileTypeUtils/dat/datRepacker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'dart:typed_data';
import 'package:path/path.dart' as path;

import '../../stateManagement/statusInfo.dart';
import '../../utils.dart';
import '../../utils/utils.dart';
import '../utils/ByteDataWrapper.dart';
import 'datHashGenerator.dart';

Expand Down
39 changes: 5 additions & 34 deletions lib/fileTypeUtils/ruby/pythonRuby.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,16 @@ import 'dart:io';
import 'package:path/path.dart';

import '../../stateManagement/statusInfo.dart';
import '../../utils.dart';

String? _assetsDir;
Completer<void> _assetDirSearchCompleter = Completer();
Future<void> _assetDirSearch = _assetDirSearchCompleter.future;

const _assetsDirName = "assets";
const _assetsDirSubDirs = { "fonts", "MrubyDecompiler" };
Future<bool> findAssetsDir() async {
var path = Directory.current.path;
// search cwd breadth first
List<String> searchPathsQueue = [path];
while (searchPathsQueue.isNotEmpty) {
path = searchPathsQueue.removeAt(0);
var subDirs = await Directory(path)
.list()
.where((f) => f is Directory)
.map((f) => f.path)
.toList();
var subDirNames = subDirs.map((p) => basename(p)).toSet();
if (basename(path) == _assetsDirName && _assetsDirSubDirs.every((subDir) => subDirNames.contains(subDir))) {
_assetsDir = path;
_assetDirSearchCompleter.complete();
print("Found assets dir at $path");
return true;
}
searchPathsQueue.addAll(subDirs);
}
print("Couldn't find assets dir");
return false;
}
import '../../utils/utils.dart';
import '../../utils/assetDirFinder.dart';

Future<bool> _processFile(String filePath) async {
await _assetDirSearch;
if (_assetsDir == null) {
await assetDirDone;
if (assetsDir == null) {
showToast("Assets directory not found");
return false;
}
var pyToolPath = join(_assetsDir!, "MrubyDecompiler", "__init__.py");
var pyToolPath = join(assetsDir!, "MrubyDecompiler", "__init__.py");
var result = await Process.run("python", [pyToolPath, filePath]);
return result.exitCode == 0;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/fileTypeUtils/yax/xmlToYax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:path/path.dart' as path;
import 'package:xml/xml.dart';

import '../../stateManagement/statusInfo.dart';
import '../../utils.dart';
import '../../utils/utils.dart';
import '../utils/ByteDataWrapper.dart';

int _getTagId(XmlElement tag) {
Expand Down
2 changes: 1 addition & 1 deletion lib/fileTypeUtils/yax/yaxToXml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:path/path.dart' as path;
import 'package:xml/xml.dart';

import '../../stateManagement/statusInfo.dart';
import '../../utils.dart';
import '../../utils/utils.dart';
import 'hashToStringMap.dart';
import 'japToEng.dart';
import '../utils/ByteDataWrapper.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/keyboardEvents/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';

import '../stateManagement/openFilesManager.dart';
import '../stateManagement/undoable.dart';
import '../utils.dart';
import '../utils/utils.dart';
import 'intents.dart';

class TabChangeAction extends Action<TabChangeIntent> {
Expand Down
2 changes: 1 addition & 1 deletion lib/keyboardEvents/globalShortcutsWrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../utils.dart';
import '../utils/utils.dart';
import 'actions.dart';
import 'intents.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/keyboardEvents/intents.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import 'package:flutter/material.dart';

import '../utils.dart';
import '../utils/utils.dart';

class TabChangeIntent extends Intent {
final HorizontalDirection direction;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:window_manager/window_manager.dart';

import 'background/IdLookup.dart';
import 'fileTypeUtils/ruby/pythonRuby.dart';
import 'stateManagement/ChangeNotifierWidget.dart';
import 'utils.dart';
import 'utils/utils.dart';
import 'utils/assetDirFinder.dart';
import 'widgets/theme/customTheme.dart';
import 'keyboardEvents/globalShortcutsWrapper.dart';
import 'stateManagement/preferencesData.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/FileHierarchy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../fileTypeUtils/dat/datExtractor.dart';
import '../fileTypeUtils/ruby/pythonRuby.dart';
import '../fileTypeUtils/yax/yaxToXml.dart';
import '../main.dart';
import '../utils.dart';
import '../utils/utils.dart';
import '../widgets/misc/confirmDialog.dart';
import '../widgets/propEditors/xmlActions/XmlActionPresets.dart';
import 'Property.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/Property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:collection';
import 'package:flutter/foundation.dart';

import '../fileTypeUtils/yax/hashToStringMap.dart';
import '../utils.dart';
import '../utils/utils.dart';
import 'miscValues.dart';
import 'undoable.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/changesExporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../fileTypeUtils/dat/datRepacker.dart';
import '../fileTypeUtils/pak/pakRepacker.dart';
import '../fileTypeUtils/ruby/pythonRuby.dart';
import '../fileTypeUtils/yax/xmlToYax.dart';
import '../utils.dart';
import '../utils/utils.dart';
import 'openFileTypes.dart';
import 'preferencesData.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/charNamesXmlWrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
import 'package:convert/convert.dart';

import '../fileTypeUtils/utils/ByteDataWrapper.dart';
import '../utils.dart';
import '../utils/utils.dart';
import '../widgets/propEditors/otherFileTypes/genericTable/tableEditor.dart';
import 'Property.dart';
import 'hasUuid.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/hasUuid.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import '../utils.dart';
import '../utils/utils.dart';

mixin HasUuid {
String _uuid = uuidGen.v1();
Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/openFileTypes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import '../fileTypeUtils/smd/smdReader.dart';
import '../fileTypeUtils/smd/smdWriter.dart';
import '../fileTypeUtils/tmd/tmdReader.dart';
import '../fileTypeUtils/tmd/tmdWriter.dart';
import '../utils.dart';
import '../utils/utils.dart';
import '../widgets/filesView/FileType.dart';
import 'FileHierarchy.dart';
import 'Property.dart';
Expand Down
89 changes: 82 additions & 7 deletions lib/stateManagement/otherFileTypes/McdData.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:math';
import 'package:path/path.dart';

import '../../fileTypeUtils/mcd/mcdReader.dart';
import '../../utils.dart';
import '../../utils/utils.dart';
import '../Property.dart';
import '../hasUuid.dart';
import '../nestedNotifier.dart';
Expand All @@ -22,6 +22,27 @@ class McdFontSymbol {
McdFontSymbol(this.code, this.char, this.x, this.y, this.width, this.height);
}

class UsedFontSymbol {
final int code;
final String char;
final int fontId;

UsedFontSymbol(this.code, this.char, [this.fontId = -1]);

UsedFontSymbol.withFont(UsedFontSymbol other, this.fontId) :
code = other.code,
char = other.char;

@override
bool operator ==(Object other) =>
other is UsedFontSymbol &&
code == other.code &&
fontId == other.fontId;

@override
int get hashCode => Object.hash(code, fontId);
}

class McdFont {
late final int fontId;
late final int fontWidth;
Expand Down Expand Up @@ -93,6 +114,15 @@ class McdLine with HasUuid {
void dispose() {
text.dispose();
}

Set<UsedFontSymbol> getUsedSymbolCodes() {
var charMatcher = RegExp(r"<[^>]+>|[^ ≡]");
var symbols = charMatcher.allMatches(text.value)
.map((m) => m.group(0)!)
.map((c) => UsedFontSymbol(c != "…" ? c.codeUnitAt(0) : 0x80, c))
.toSet();
return symbols;
}
}

class McdParagraph with HasUuid {
Expand Down Expand Up @@ -122,6 +152,17 @@ class McdParagraph with HasUuid {
line.dispose();
lines.dispose();
}

Set<UsedFontSymbol> getUsedSymbols() {
Set<UsedFontSymbol> symbols = {};
for (var line in lines) {
var lineSymbols = line
.getUsedSymbolCodes()
.map((c) => UsedFontSymbol.withFont(c, font.fontId));
symbols.addAll(lineSymbols);
}
return symbols;
}
}

class McdEvent with HasUuid {
Expand Down Expand Up @@ -163,6 +204,13 @@ class McdEvent with HasUuid {
paragraph.dispose();
paragraphs.dispose();
}

Set<UsedFontSymbol> getUsedSymbols() {
Set<UsedFontSymbol> symbols = {};
for (var paragraph in paragraphs)
symbols.addAll(paragraph.getUsedSymbols());
return symbols;
}
}

class McdData {
Expand Down Expand Up @@ -214,6 +262,15 @@ class McdData {
);
}

void dispose() {
textureWtaPath?.dispose();
textureWtpPath?.dispose();
usedFonts.dispose();
for (var event in events)
event.dispose();
events.dispose();
}

void addEvent() {
events.add(McdEvent(
HexProp(events.isNotEmpty ? events.last.eventId.value + 1 : randomId()),
Expand All @@ -234,12 +291,30 @@ class McdData {
.reduce(max);
}

void dispose() {
textureWtaPath?.dispose();
textureWtpPath?.dispose();
usedFonts.dispose();
Set<UsedFontSymbol> getUsedSymbols() {
Set<UsedFontSymbol> symbols = {};
for (var event in events)
event.dispose();
events.dispose();
symbols.addAll(event.getUsedSymbols());
return symbols;
}

Set<UsedFontSymbol> getLocalFontUnsupportedSymbols() {
var usedSymbols = getUsedSymbols();
return usedSymbols
.where((usedSym) => !usedFonts
.firstWhere((f) => f.fontId == usedSym.fontId)
.supportedSymbols
.any((supSym) => supSym.code == usedSym.code))
.toSet();
}

Set<UsedFontSymbol> getGlobalFontUnsupportedSymbols() {
var usedSymbols = getUsedSymbols();
return usedSymbols
.where((usedSym) => !availableFonts
.firstWhere((f) => f.fontId == usedSym.fontId)
.supportedSymbols
.any((supSym) => supSym.code == usedSym.code))
.toSet();
}
}
2 changes: 1 addition & 1 deletion lib/stateManagement/sync/syncObjects.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:convert';

import 'package:xml/xml.dart';

import '../../utils.dart';
import '../../utils/utils.dart';
import '../Property.dart';
import '../xmlProps/xmlProp.dart';
import 'syncServer.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/undoable.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import 'package:flutter/material.dart';

import '../utils.dart';
import '../utils/utils.dart';
import 'FileHierarchy.dart';
import 'miscValues.dart';
import 'openFilesManager.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/stateManagement/xmlProps/xmlProp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:xml/xml.dart';

import '../../fileTypeUtils/yax/hashToStringMap.dart';
import '../../fileTypeUtils/yax/japToEng.dart';
import '../../utils.dart';
import '../../utils/utils.dart';
import '../Property.dart';
import '../charNamesXmlWrapper.dart';
import '../nestedNotifier.dart';
Expand Down
Loading

0 comments on commit a7c1d97

Please sign in to comment.