|
17 | 17 | public record ScriptFile(String name, String text, Multimap<String, String> property, HashMap<String, Object> data) { |
18 | 18 |
|
19 | 19 | @Nullable |
20 | | - public static ScriptFile create(String name, String rawText){ |
| 20 | + public static ScriptFile create(String name, String rawText) { |
21 | 21 | try (BufferedReader reader = new BufferedReader(new StringReader(rawText))) { |
22 | | - List<String> lines = new ArrayList<>(); |
| 22 | + List<String> commentLines = new ArrayList<>(); |
| 23 | + StringBuilder nonCommentText = new StringBuilder(); |
23 | 24 | String line; |
| 25 | + |
24 | 26 | while ((line = reader.readLine()) != null) { |
25 | 27 | line = line.trim(); |
26 | 28 | if (StringUtils.startsWith(line, "#")) { |
27 | | - lines.add(line.substring(1)); |
| 29 | + commentLines.add(line.substring(1)); |
28 | 30 | } else { |
29 | | - Multimap<String, String> map = HashMultimap.create(); |
30 | | - for (String s : lines) { |
31 | | - int i = s.indexOf(' '); |
32 | | - if (i > 0) { |
33 | | - map.put(s.substring(0, i), s.substring(i + 1)); |
34 | | - } |
35 | | - } |
36 | | - if (!map.containsKey("compiler")) map.put("compiler", "javaShota"); |
37 | | - |
38 | | - byte[] b1 = IOUtils.toByteArray(reader, StandardCharsets.UTF_8); |
39 | | - byte[] b2 = new byte[line.length() + 1 + b1.length]; |
40 | | - System.arraycopy(line.getBytes(), 0, b2, 0, line.length()); |
41 | | - b2[line.length()] = '\n'; |
42 | | - System.arraycopy(b1, line.length(), b2, 0, b1.length); |
43 | | - |
44 | | - return new ScriptFile( |
45 | | - name, |
46 | | - new String(b2), |
47 | | - map, new HashMap<>() |
48 | | - ); |
| 31 | + nonCommentText.append(line).append("\n"); |
49 | 32 | } |
50 | 33 | } |
51 | | - return null; |
| 34 | + |
| 35 | + if(nonCommentText.length() > 0 && nonCommentText.charAt(nonCommentText.length()-1) == '\n'){ |
| 36 | + nonCommentText.deleteCharAt(nonCommentText.length()-1); |
| 37 | + } |
| 38 | + |
| 39 | + Multimap<String, String> map = HashMultimap.create(); |
| 40 | + for (String s : commentLines) { |
| 41 | + int i = s.indexOf(' '); |
| 42 | + if (i > 0) { |
| 43 | + map.put(s.substring(0, i), s.substring(i + 1)); |
| 44 | + } |
| 45 | + } |
| 46 | + if (!map.containsKey("compiler")) { |
| 47 | + map.put("compiler", "javaShota"); |
| 48 | + } |
| 49 | + |
| 50 | + return new ScriptFile(name, nonCommentText.toString(), map, new HashMap<>()); |
52 | 51 | } catch (Exception e) { |
53 | | - ShotaASM.LOGGER.error("error when locate script with", e); |
| 52 | + ShotaASM.LOGGER.error("Error creating ScriptFile for '{}': {}", name, e.getMessage(), e); |
54 | 53 | return null; |
55 | 54 | } |
56 | 55 | } |
|
0 commit comments