-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1] Working on multimod functionality.
- Loading branch information
Showing
9 changed files
with
305 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
src/main/java/com/dalolorn/sr2modmanager/adapter/sr2utils/DataReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package com.dalolorn.sr2modmanager.adapter.sr2utils; | ||
|
||
import org.eclipse.jgit.lib.ObjectLoader; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.*; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Collectors; | ||
|
||
public class DataReader { | ||
public final String filename; | ||
public final List<String> lines; | ||
|
||
public boolean allowLines; | ||
public boolean fullLine = false; | ||
public boolean allowMultiline = true; | ||
public boolean skipComments = true; | ||
public boolean skipEmpty = true; | ||
|
||
public boolean inMultiline = false; | ||
public boolean squash = false; | ||
public int indent; | ||
public int lineIndex; | ||
public String line; | ||
public String key; | ||
public String value; | ||
|
||
public DataReader(@NotNull File file) throws IOException { | ||
this(file, true); | ||
} | ||
|
||
public DataReader(@NotNull File file, boolean allowLines) throws IOException { | ||
this.allowLines = allowLines; | ||
filename = file.getName(); | ||
|
||
try ( | ||
var fileStream = new FileInputStream(file); | ||
var fileReader = new BufferedReader(new InputStreamReader(fileStream)) | ||
) { | ||
lines = fileReader.lines().collect(Collectors.toUnmodifiableList()); | ||
} | ||
} | ||
|
||
public DataReader(@NotNull ObjectLoader fileLoader, @NotNull String name) throws IOException { | ||
this(fileLoader, name, true); | ||
} | ||
|
||
public DataReader(@NotNull ObjectLoader fileLoader, @NotNull String name, boolean allowLines) throws IOException { | ||
this.allowLines = allowLines; | ||
filename = name; | ||
|
||
try ( | ||
var fileStream = fileLoader.openStream(); | ||
var fileReader = new BufferedReader(new InputStreamReader(fileStream)) | ||
) { | ||
lines = fileReader.lines().map(str -> str+"\n").collect(Collectors.toUnmodifiableList()); | ||
} | ||
} | ||
|
||
public String position() { | ||
return filename + " | Line " + lineIndex; | ||
} | ||
|
||
public boolean feed(String feedLine) { | ||
line = feedLine; | ||
return handle(); | ||
} | ||
|
||
public boolean handle() { | ||
// Handle multiline values | ||
if(inMultiline) { | ||
return handleMultilineValues(); | ||
} | ||
|
||
// Cut off comments | ||
cutOffComments(); | ||
|
||
// Get indent level | ||
if (getIndentLevel()) return false; | ||
|
||
// Detect comments | ||
if(skipEmpty && line.isEmpty()) | ||
return false; | ||
|
||
boolean isKeyValue = splitKeyValue(line); | ||
|
||
if(!isKeyValue) { | ||
if(allowLines) { | ||
fullLine = true; | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
else { | ||
key = key.trim(); | ||
value = value.trim(); | ||
fullLine = false; | ||
|
||
// Detect multiline blocks | ||
if(allowMultiline && (value.equals("<<") || value.equals("<<|"))) { | ||
squash = value.length() == 3; | ||
value = ""; | ||
inMultiline = true; | ||
return false; | ||
} | ||
|
||
return !key.isEmpty(); | ||
} | ||
} | ||
|
||
private boolean handleMultilineValues() { | ||
line = line.trim(); | ||
|
||
if(line.length() > 1) { | ||
if(line.charAt(line.length() - 1) == '\\') | ||
line = line.substring(0, line.length() - 1); | ||
else | ||
line = squash ? " " : "\n"; | ||
} | ||
else { | ||
// Preserve empty lines in squash mode | ||
line += squash ? "\n\n" : "\n"; | ||
} | ||
|
||
if(line.startsWith(">>")) { | ||
// Remove the last linebreak | ||
if(value.length() > 0 && value.charAt(value.length() - 1) == '\n') | ||
value = value.substring(0, value.length() - 1); | ||
inMultiline = false; | ||
return true; | ||
} | ||
else { | ||
value += line; | ||
return false; | ||
} | ||
} | ||
|
||
private void cutOffComments() { | ||
if(skipComments) { | ||
int commentIndex = line.indexOf("//"); | ||
if(commentIndex != -1) | ||
line = line.substring(0, commentIndex); | ||
} | ||
} | ||
|
||
private boolean getIndentLevel() { | ||
int pos = line.length() - line.stripLeading().length(); | ||
if(pos == 0) { | ||
if(skipEmpty) | ||
return true; | ||
indent = 0; | ||
} | ||
else { | ||
int rpos = line.substring(0, line.length() - line.stripTrailing().length()).length(); | ||
if(rpos != 0) | ||
line = line.substring(pos, rpos - pos + 1); | ||
indent = pos; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean next() { | ||
while(lineIndex < lines.size()) { | ||
line = lines.get(lineIndex).trim(); | ||
lineIndex++; | ||
if(handle()) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void reset() { | ||
lineIndex = 0; | ||
} | ||
|
||
public boolean splitKeyValue(String input) { | ||
return splitKeyValue(input, ':'); | ||
} | ||
|
||
public boolean splitKeyValue(String input, char separator) { | ||
int index = input.indexOf(separator); | ||
if(index == -1) | ||
return false; | ||
key = input.substring(0, index); | ||
value = input.substring(index+1); | ||
return true; | ||
} | ||
|
||
public List<String> compilePattern(String pattern) { | ||
pattern = pattern.toLowerCase(Locale.ROOT); | ||
if(pattern.isEmpty()) | ||
return Collections.emptyList(); | ||
|
||
List<String> compiled = Arrays.asList(pattern.split("\\*")); | ||
if(pattern.endsWith("*")) | ||
compiled.add(""); | ||
return compiled; | ||
} | ||
} |
68 changes: 67 additions & 1 deletion
68
src/main/java/com/dalolorn/sr2modmanager/model/Modinfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,77 @@ | ||
package com.dalolorn.sr2modmanager.model; | ||
|
||
import com.dalolorn.sr2modmanager.adapter.sr2utils.DataReader; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Modinfo { | ||
private static final int CUR_COMPATIBILITY = 200; | ||
|
||
public final boolean inRoot; | ||
public final String folderName; | ||
|
||
public Modinfo(boolean inRoot, String folderName) { | ||
public String name; | ||
public String description; | ||
public String parentName; | ||
public int version; | ||
public int compatibility; | ||
public boolean isBase; | ||
public boolean listed; | ||
public boolean forCurrentVersion; | ||
public List<String> overrides = new ArrayList<>(); | ||
public List<List<String>> overridePatterns = new ArrayList<>(); | ||
public Map<Integer, String> fallbacks; | ||
|
||
public Modinfo(boolean inRoot, String folderName, File file) throws IOException { | ||
this.inRoot = inRoot; | ||
this.folderName = folderName; | ||
parse(new DataReader(file)); | ||
} | ||
|
||
private void parse(DataReader file) { | ||
String key; | ||
String value; | ||
while(file.next()) { | ||
key = file.key; | ||
value = file.value; | ||
if(key.equalsIgnoreCase("Name")) { | ||
name = value; | ||
} | ||
else if(key.equalsIgnoreCase("Description")) { | ||
description = value; | ||
} | ||
else if(key.equalsIgnoreCase("Override")) { | ||
overrides.add(value); | ||
List<String> compiled = file.compilePattern(value); | ||
overridePatterns.add(compiled); | ||
} | ||
else if(key.equalsIgnoreCase("Derives From")) { | ||
if(value.equals("-")) | ||
parentName = ""; | ||
else | ||
parentName = value; | ||
} | ||
else if(key.equalsIgnoreCase("Base Mod")) { | ||
isBase = Boolean.parseBoolean(value); | ||
} | ||
else if(key.equalsIgnoreCase("Listed")) { | ||
listed = Boolean.parseBoolean(value); | ||
} | ||
else if(key.equalsIgnoreCase("Version")) { | ||
version = Integer.parseInt(value); | ||
} | ||
else if(key.equalsIgnoreCase("Compatibility")) { | ||
compatibility = Integer.parseInt(value); | ||
forCurrentVersion = compatibility >= CUR_COMPATIBILITY; | ||
} | ||
else if(key.equalsIgnoreCase("Fallback")) { | ||
file.splitKeyValue(value, '='); | ||
fallbacks.put(Integer.parseInt(file.value), file.key); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.