Skip to content

Commit 1893359

Browse files
committedMar 25, 2021
Major improvements and fixes.
1 parent 5c9a6b5 commit 1893359

10 files changed

+125
-145
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.1.1
22

33
- Effective Dart analysis improvements.
4+
- Major improvements and fixes.
45

56
## 0.1.0
67

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ some errors.
2222

2323
### 0.1.1
2424

25-
- Effective Dart analysis improvements.
25+
- Effective Dart analysis improvements.
26+
- Major improvements and fixes.

‎appdef.json

+10-33
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
{
2-
"replacements": [
3-
{
4-
"name": "new_line",
5-
"pattern": "00",
6-
"replace_with": "0A",
7-
"skip_comparison": true
8-
},
9-
{
10-
"name": "name_start",
11-
"pattern": "81 79",
12-
"replace_with": "5B 6E 61 6D 65 5D"
13-
},
14-
{
15-
"name": "text_start",
16-
"pattern": "81 7A",
17-
"replace_with": "5B 74 65 78 74 5D"
18-
},
19-
{
20-
"name": "line_break",
21-
"pattern": "25 4E",
22-
"replace_with": "5B 62 72 65 61 6B 5D",
23-
"skip_comparison": true
24-
},
25-
{
26-
"name": "line_end",
27-
"pattern": "25 4B 25 50",
28-
"replace_with": "5B 65 6E 64 5D"
29-
},
30-
{
31-
"name": "line_end_choice",
32-
"pattern": "25 4B 25 70",
33-
"replace_with": "5B 63 68 6F 69 63 65 5D"
34-
}
2+
"replacements": {
3+
"00": "0A",
4+
"81 79": "5B 6E 61 6D 65 5D",
5+
"81 7A": "5B 74 65 78 74 5D",
6+
"25 4E": "5B 62 72 65 61 6B 5D",
7+
"25 4B 25 50": "5B 65 6E 64 5D",
8+
"25 4B 25 70": "5B 63 68 6F 69 63 65 5D"
9+
},
10+
"skip_comparison": [
11+
"0A", "5B 62 72 65 61 6B 5D"
3512
]
3613
}

‎bin/corpse_tools.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import 'package:args/command_runner.dart';
44
import 'package:corpse_tools/commands.dart';
55

66
void main(List<String> args) {
7-
var desc = 'CorpseTools v0.1.1 made by Kyouin.\n'
7+
var desc = 'CorpseTools v0.1.1 made by Kyouin '
8+
'(https://github.com/KyouinDev/corpse_tools)\n'
89
'Supported game(s):\n'
910
' - Corpse Party: Book of Shadows (PC)';
10-
var runner = CommandRunner('corpse_tools', desc)
11+
var runner = CommandRunner('corpse_tools.exe', desc)
1112
..addCommand(ExtractTextCommand())
1213
..addCommand(ReplaceTextCommand());
1314
runner.run(args).catchError((error) {

‎lib/app_def.dart

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import 'dart:convert';
22
import 'dart:io';
33

4-
import 'entities/replacement.dart';
5-
64
class AppDef {
7-
final List<Replacement> replacements;
5+
final Map<String, String> replacements;
6+
final List<String> skipComparison;
87

98
static final AppDef _instance = _fromFile(File('appdef.json'));
109

11-
AppDef._(this.replacements);
10+
AppDef._(this.replacements, this.skipComparison);
1211

1312
static AppDef _fromFile(File file) {
1413
return _fromJson(json.decode(file.readAsStringSync()));
1514
}
1615

1716
static AppDef _fromJson(Map<String, dynamic> json) {
18-
return AppDef._(Replacement.listFromJson(json['replacements']));
17+
return AppDef._(
18+
Map.from(json['replacements']),
19+
List<String>.from(json['skip_comparison']),
20+
);
1921
}
2022

2123
factory AppDef() {

‎lib/commands/extract_text_command.dart

+15-18
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,20 @@ class ExtractTextCommand extends Command {
2323

2424
@override
2525
void run() {
26-
if (argResults?['input'] == null) {
27-
print('Missing parameters. '
28-
'Run "corpse_tools.exe help extract-text" for info.');
29-
return;
30-
}
31-
32-
var input = File(argResults?['input']);
33-
34-
if (!input.existsSync() || !input.path.endsWith('.BIN')) {
35-
print('Input file does not exist or not a .BIN type.');
36-
return;
37-
}
38-
39-
var lastLine = getReadableLines(readFileAsHexString(input));
40-
var formatLine = replaceSequences(lastLine);
41-
var linesCount = formatLine.count(AppDef().replacements[0].replaceWith) + 1;
42-
writeToFile(File(input.path.replaceAll('.BIN', '.txt')), formatLine);
43-
print('Done successfully. Total lines extracted: $linesCount.');
26+
var input = getFileFromArg(
27+
arg: argResults!['input'],
28+
command: 'extract-text',
29+
expected: '.BIN',
30+
);
31+
32+
if (input == null) return;
33+
34+
var time = DateTime.now();
35+
var readableContent = getReadableContent(readFileAsHexString(input));
36+
var replacedContent = replaceSequences(readableContent);
37+
var count = replacedContent.count(AppDef().replacements.values.first) + 1;
38+
writeToFile(File(input.path.replaceAll('.BIN', '.txt')), replacedContent);
39+
print('Done successfully. Total lines extracted: $count.');
40+
printElapsedTime(time);
4441
}
4542
}
+32-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:io';
2-
31
import 'package:args/command_runner.dart';
42

53
import '../utils.dart';
@@ -28,45 +26,45 @@ class ReplaceTextCommand extends Command {
2826

2927
@override
3028
void run() {
31-
if (argResults?['original'] == null || argResults?['modified'] == null) {
32-
print('Missing parameters. '
33-
'Run "corpse_tools.exe help replace-text" for info.');
34-
return;
35-
}
36-
37-
var original = File(argResults?['original']);
38-
var modified = File(argResults?['modified']);
29+
var original = getFileFromArg(
30+
arg: argResults!['original'],
31+
command: 'replace-text',
32+
expected: '.BIN',
33+
);
3934

40-
if (!original.existsSync() || !original.path.endsWith('.BIN')) {
41-
print('Original file does not exist or not a .BIN type.');
42-
return;
43-
}
35+
if (original == null) return;
4436

45-
if (!modified.existsSync() || !modified.path.endsWith('.txt')) {
46-
print('Modified file does not exist or not a .txt type.');
47-
return;
48-
}
37+
var modified = getFileFromArg(
38+
arg: argResults!['modified'],
39+
command: 'replace-text',
40+
expected: '.txt',
41+
);
4942

50-
var modifiedLine = readFileAsHexString(modified);
51-
var originalLine = getReadableLines(readFileAsHexString(original));
52-
var result = compareEdit(replaceSequences(originalLine), modifiedLine);
43+
if (modified == null) return;
5344

54-
if (result.success) {
55-
if (result.changedLines == 0) {
56-
print('No lines were changed.');
57-
return;
58-
}
45+
var time = DateTime.now();
46+
var modifContent = readFileAsHexString(modified);
47+
var origHexString = readFileAsHexString(original);
48+
var origContent = getReadableContent(origHexString);
49+
var result = compareEdit(replaceSequences(origContent), modifContent);
5950

60-
var lines = getUnreadableLines(readFileAsHexString(original));
61-
var replacedLines = replaceSequences(modifiedLine, extract: false);
62-
writeToFile(original, '$lines $replacedLines');
63-
print('Done successfully. Total lines replaced: ${result.changedLines}.');
64-
} else {
51+
if (!result.success) {
6552
print('Error while trying to replace modified text:');
53+
printError(String s) => print(' - $s'); //aux function
54+
result.messages.forEach(printError);
55+
return;
56+
}
6657

67-
for (var s in result.messages) {
68-
print(' - $s');
69-
}
58+
if (result.changedLines == 0) {
59+
print('No lines were changed.');
60+
printElapsedTime(time);
61+
return;
7062
}
63+
64+
var unreadable = getUnreadableContent(origHexString);
65+
var modifReplacedContent = replaceSequences(modifContent, extract: false);
66+
writeToFile(original, '$unreadable $modifReplacedContent');
67+
print('Done successfully. Total lines replaced: ${result.changedLines}.');
68+
printElapsedTime(time);
7169
}
7270
}

‎lib/entities/replacement.dart

-26
This file was deleted.

‎lib/utils/app_utils.dart

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
import '../app_def.dart';
22
import '../entities/compare_result.dart';
3-
import '../entities/replacement.dart';
43
import '../utils.dart';
54

6-
String getUnreadableLines(String hex) {
5+
String getUnreadableContent(String hex) {
76
return hex.substring(0, hex.lastIndexOf('02') + 2);
87
}
98

10-
String getReadableLines(String hex) {
9+
String getReadableContent(String hex) {
1110
return hex.substring(hex.lastIndexOf('02') + 3);
1211
}
1312

1413
String replaceSequences(String hex, {bool extract = true}) {
15-
for (var replacement in AppDef().replacements) {
14+
for (var replacement in AppDef().replacements.entries) {
1615
if (extract) {
17-
hex = hex.replaceAll(replacement.pattern, replacement.replaceWith);
16+
hex = hex.replaceAll(replacement.key, replacement.value);
1817
} else {
19-
hex = hex.replaceAll(replacement.replaceWith, replacement.pattern);
18+
hex = hex.replaceAll(replacement.value, replacement.key);
2019
}
2120
}
2221

2322
return hex;
2423
}
2524

25+
int compareLine(String origLine, String modifLine, String sequence) {
26+
return origLine.count(sequence) - modifLine.count(sequence);
27+
}
28+
2629
CompareResult compareEdit(String original, String modified) {
27-
var lineSeparator = AppDef().replacements[0].replaceWith;
28-
var originalLines = original.split(lineSeparator);
29-
var modifiedLines = modified.split(lineSeparator);
30+
var separator = AppDef().replacements.values.first;
31+
var origLines = original.split(separator);
32+
var modifLines = modified.split(separator);
3033

31-
if (originalLines.length != modifiedLines.length) {
34+
if (origLines.length != modifLines.length) {
3235
return CompareResult(
3336
success: false,
3437
messages: ['Number of lines does not match.'],
@@ -40,31 +43,31 @@ CompareResult compareEdit(String original, String modified) {
4043
var changedLines = 0;
4144
var success = true;
4245

43-
for (var i = 0; i < originalLines.length; i++) {
44-
var originalLine = originalLines[i];
45-
var modifiedLine = modifiedLines[i];
46+
for (var i = 0; i < origLines.length; i++) {
47+
var origLine = origLines[i];
48+
var modifLine = modifLines[i];
4649

47-
for (var replacement in AppDef().replacements) {
48-
var entity = replacement.replaceWith;
50+
for (var toSkip in AppDef().skipComparison) {
51+
origLine = origLine.replaceAll(toSkip, '');
52+
modifLine = modifLine.replaceAll(toSkip, '');
53+
}
4954

50-
if (replacement.skipComparison) {
51-
originalLine = originalLine.replaceAll(entity, '');
52-
modifiedLine = modifiedLine.replaceAll(entity, '');
53-
continue;
54-
}
55+
var replacements = AppDef().replacements.entries.where((entry) {
56+
return !AppDef().skipComparison.contains(entry.value);
57+
}).map((entry) => entry.value);
5558

56-
var name = String.fromCharCodes(hexStringToIntList(entity));
57-
var c1 = originalLine.count(entity);
58-
var c2 = modifiedLine.count(entity);
59+
for (var replacement in replacements) {
60+
var compare = compareLine(origLine, modifLine, replacement);
61+
var name = String.fromCharCodes(hexStringToIntList(replacement));
5962

60-
if (c1 != c2) {
61-
messages.add('Line ${i + 1}: found ${c1 > c2 ? 'less' : 'more'}'
63+
if (compare != 0) {
64+
messages.add('Line ${i + 1}: found ${compare > 0 ? 'less' : 'more'}'
6265
' $name than expected');
6366
success = false;
6467
}
6568
}
6669

67-
if (originalLine != modifiedLine) changedLines++;
70+
if (origLine != modifLine) changedLines++;
6871
}
6972

7073
return CompareResult(
@@ -74,6 +77,11 @@ CompareResult compareEdit(String original, String modified) {
7477
);
7578
}
7679

80+
void printElapsedTime(DateTime from) {
81+
var elapsed = DateTime.now().difference(from);
82+
print('Elapsed time: ${elapsed.inMilliseconds} ms.');
83+
}
84+
7785
extension SequenceCount on String {
7886
int count(Pattern pattern) => split(pattern).length - 1;
7987
}

‎lib/utils/file_utils.dart

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ import 'dart:io';
22

33
import '../utils.dart';
44

5+
File? getFileFromArg({
6+
required dynamic arg,
7+
required String command,
8+
required String expected,
9+
}) {
10+
if (arg == null) {
11+
print('Missing parameters. '
12+
'Run "corpse_tools.exe help $command" for info.');
13+
return null;
14+
}
15+
16+
var file = File(arg);
17+
18+
if (!file.existsSync() || !file.path.endsWith(expected)) {
19+
print('Input file does not exist or not a $expected type.');
20+
return null;
21+
}
22+
23+
return file;
24+
}
25+
526
String readFileAsHexString(File file) {
627
var bytes = file.readAsBytesSync();
728
return intListToHexString(bytes);

0 commit comments

Comments
 (0)
Please sign in to comment.