-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.java
245 lines (221 loc) · 10.8 KB
/
Converter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import java.util.Scanner;
import javax.swing.JTextArea;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
// not completely finished, but hopefully it's close.
public class Converter {
private String pathIn;
private String pathOut;
private String levelData;
private String levelDataBackup;
private int laserCount;
private int replacedLaserCount;
private final String rLaserType = "\"_type\":13";
private final String lLaserType = "\"_type\":12";
public Converter(String path) {
this.pathIn = path;
this.pathOut = this.pathIn;
this.levelData = loadLevelData();
}
public Converter(String pathIn, String pathOut) {
this.pathIn = pathIn;
this.pathOut = pathOut;
this.levelData = loadLevelData();
}
public int getLaserCount() {
return laserCount;
}
public int getReplacedLaserCount() {
return replacedLaserCount;
}
private String loadLevelData() {
try {
FileReader reader = new FileReader(pathIn);
Scanner scan = new Scanner(reader);
String localLevelData = scan.nextLine();
scan.close();
levelDataBackup = localLevelData;
return localLevelData;
} catch (Exception e) {
return "Error";
}
}
public void backupLevelData(JTextArea log) {
String backupLocation = pathIn.substring(0, pathIn.indexOf(".dat")) + "_backup.dat";
try {
File file = new File(backupLocation);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(levelDataBackup);
fileWriter.flush();
fileWriter.close();
} catch (Exception e) {
log.append("ERROR: idk wtf when worng lol");
}
}
public void outputLevelData(JTextArea log) {
try {
File file = new File(pathOut);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(levelData);
fileWriter.flush();
fileWriter.close();
} catch (Exception e) {
log.append("ERROR: idk wtf when worng lol");
}
}
public void checkEvents(JTextArea log) {
laserCount = 0;
log.append("Looking for Laser events... \n");
for (int i = 0; i < (levelData.length() - rLaserType.length() - 5); i++) {
if (levelData.substring(i, (i + rLaserType.length())).equals(rLaserType)) {
log.append("Looking for " + lLaserType + " and " + rLaserType + ". Found: " + laserCount + " PROGRESS: " + i + "/" + levelData.length() + "\n");
laserCount++;
} else if (levelData.substring(i, (i + lLaserType.length())).equals(lLaserType)) {
log.append("Looking for " + lLaserType + " and " + rLaserType + ". Found: " + laserCount + " PROGRESS: " + i + "/" + levelData.length() + "\n");
laserCount++;
}
}
log.append("Found " + laserCount + " matches for laser speed events. \n");
}
public void replaceEvents(JTextArea log) {
replacedLaserCount = 0;
// Start replacing based on type 13's value
for (int i = 0; i < (levelData.length() - rLaserType.length() - 5); i++) {
if (levelData.substring(i, (i + rLaserType.length())).equals(rLaserType)) {
String event = levelData.substring(i, levelData.indexOf("}", i));
int eventValue = Integer.parseInt(event.substring(event.lastIndexOf(":") + 1));
int eventType = Integer.parseInt(event.substring(event.indexOf(":") + 1, event.indexOf(",")));
switch (eventValue) {
// Switch event into 360 event
case 0: case 1: case 2: case 3:
case 4: case 5: case 6: case 7:
eventType = 15;
break;
// keep event as laser
default:
eventValue = Math.abs(eventValue - 8);
break;
}
event = "\"_type\":" + eventType + ",\"_value\":" + eventValue;
levelData = levelData.substring(0, i) + event + levelData.substring(levelData.indexOf("}", i));
replacedLaserCount++;
log.append("Converting laser events... Currently on " + replacedLaserCount + "/" + laserCount + "\n");
// Start replacing based on type 12's value
} else if (levelData.substring(i, (i + lLaserType.length())).equals(lLaserType)) {
String event = levelData.substring(i, levelData.indexOf("}", i));
int eventValue = Integer.parseInt(event.substring(event.lastIndexOf(":") + 1));
int eventType = Integer.parseInt(event.substring(event.indexOf(":") + 1, event.indexOf(",")));
switch (eventValue) {
// Switch event into 360 event
case 0: case 1: case 2: case 3:
case 4: case 5: case 6: case 7:
eventType = 14;
break;
// keep event as laser
default:
eventValue = Math.abs(eventValue - 8);
break;
}
event = "\"_type\":" + eventType + ",\"_value\":" + eventValue;
levelData = levelData.substring(0, i) + event + levelData.substring(levelData.indexOf("}", i));
replacedLaserCount++;
log.append("Converting laser events... Currently on " + replacedLaserCount + "/" + laserCount + "\n");
}
}
log.append("Converting laser events... Done. " + replacedLaserCount + "/" + laserCount + "\n");
}
/* Used for holding old method, as I would still like to keep it in the branch for refrence for now.
private static void prototype() throws FileNotFoundException, IOException {
// Call for user file loc.
Scanner userPrompt = new Scanner(System.in);
System.out.println("Only run this once per file. Do not open files generated with this tool in MMA2.");
System.out.println("Where is the level.dat located?");
String oFile = userPrompt.nextLine().trim();
System.out.println("Where do you want your want your level.dat to be exported to?\nNOTE: Blank will overwrite the existing file.");
String nFileInput = userPrompt.nextLine();
String nFile;
if (nFileInput.isBlank()) {
nFile = oFile;
} else {
nFile = nFileInput.trim();
}
nFileInput = null;
// Read file
FileReader reader = new FileReader(oFile);
Scanner scan = new Scanner(reader);
String levelData = scan.nextLine();
scan.close();
//For error checking later
final String rLaserType = "\"_type\":13";
final String lLaserType = "\"_type\":12";
int LaserCount = 0;
System.out.print("Looking for Laser events... \r");
for (int i = 0; i < (levelData.length() - rLaserType.length() - 5); i++) {
if (levelData.substring(i,(i + rLaserType.length())).equals(rLaserType)) {
System.out.print("Looking for " + lLaserType + " and " + rLaserType +". Found: " + LaserCount + " PROGRESS: " + i + "/" + levelData.length() + "\r");
LaserCount++;
} else if (levelData.substring(i,(i + lLaserType.length())).equals(lLaserType)) {
System.out.print("Looking for " + lLaserType + " and " + rLaserType +". Found: " + LaserCount + " PROGRESS: " + i + "/" + levelData.length() + "\r");
LaserCount++;
}
}
System.out.println("Found " + LaserCount + " matches for laser speed events. ");
// confirm details with the user, if not exit.
System.out.println("Would you like to continue? Y/n (Default is Y)");
String confirm = userPrompt.nextLine();
if (confirm.equals("Y") || confirm.isBlank()) {
} else {
userPrompt.close();
System.exit(0);
}
int replaceCount = 0;
// Start replacing based on type 13's value
for (int i = 0; i < (levelData.length() - rLaserType.length() - 5); i++) {
if (levelData.substring(i,(i + rLaserType.length())).equals(rLaserType)) {
String event = levelData.substring(i,levelData.indexOf("}", i));
int eventValue = Integer.parseInt(event.substring(event.lastIndexOf(":") + 1));
int eventType = Integer.parseInt(event.substring(event.indexOf(":") + 1, event.indexOf(",")));
switch (eventValue) {
// Switch event into 360 event
case 0: case 1: case 2: case 3:
case 4: case 5: case 6: case 7:
eventType = 15; break;
// keep event as laser
default:
eventValue = Math.abs(eventValue - 8); break;
}
event = "\"_type\":" + eventType + ",\"_value\":" + eventValue;
levelData = levelData.substring(0,i) + event + levelData.substring(levelData.indexOf("}", i));
replaceCount++;
// Start replacing based on type 12's value
} else if (levelData.substring(i,(i + lLaserType.length())).equals(lLaserType)) {
String event = levelData.substring(i,levelData.indexOf("}", i));
int eventValue = Integer.parseInt(event.substring(event.lastIndexOf(":") + 1));
int eventType = Integer.parseInt(event.substring(event.indexOf(":") + 1, event.indexOf(",")));
switch (eventValue) {
// Switch event into 360 event
case 0: case 1: case 2: case 3:
case 4: case 5: case 6: case 7:
eventType = 14; break;
// keep event as laser
default:
eventValue = Math.abs(eventValue - 8); break;
}
event = "\"_type\":" + eventType + ",\"_value\":" + eventValue;
levelData = levelData.substring(0,i) + event + levelData.substring(levelData.indexOf("}", i));
replaceCount++;
}
System.out.print("Converting laser events... Currently on " + replaceCount + "/" + LaserCount + "\r");
}
System.out.println("Converting laser events... Done. " + replaceCount + "/" + LaserCount + " ");
System.out.println("Outputting file at " + nFile);
File file = new File(nFile);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(levelData);
fileWriter.flush();
fileWriter.close();
userPrompt.close();
}
*/
}