1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
.multivm
;
12 import cc
.squirreljme
.plugin
.SquirrelJMEPluginConfiguration
;
13 import cc
.squirreljme
.plugin
.multivm
.ident
.SourceTargetClassifier
;
14 import cc
.squirreljme
.plugin
.util
.UnassistedLaunchEntry
;
16 import java
.io
.IOException
;
17 import java
.io
.OutputStream
;
18 import java
.nio
.file
.Files
;
19 import java
.nio
.file
.Path
;
20 import java
.nio
.file
.StandardCopyOption
;
21 import java
.nio
.file
.StandardOpenOption
;
22 import java
.util
.ArrayList
;
23 import java
.util
.LinkedHashSet
;
25 import org
.gradle
.api
.Action
;
26 import org
.gradle
.api
.Task
;
27 import org
.gradle
.api
.tasks
.SourceSet
;
30 * This performs the actual work that is needed to build the ROM.
34 public class VMRomTaskAction
35 implements Action
<Task
>
37 /** The classifier used. */
38 protected final SourceTargetClassifier classifier
;
41 * Initializes the task.
43 * @param __classifier The classifier used.
44 * @throws NullPointerException On null arguments.
47 public VMRomTaskAction(SourceTargetClassifier __classifier
)
48 throws NullPointerException
50 if (__classifier
== null)
51 throw new NullPointerException("NARG");
53 this.classifier
= __classifier
;
61 public void execute(Task __task
)
62 throws NullPointerException
64 String sourceSet
= this.classifier
.getSourceSet();
65 VMSpecifier vmType
= this.classifier
.getVmType();
67 // Is this a single source set ROM?
68 boolean isSingleSourceSet
= vmType
.isSingleSourceSetRom(
69 this.classifier
.getBangletVariant());
70 boolean bootLoaderEnabled
= !isSingleSourceSet
||
72 SourceSet
.MAIN_SOURCE_SET_NAME
.equals(sourceSet
));
77 // We need somewhere safe to store the file
78 tempFile
= Files
.createTempFile(
79 vmType
.vmName(VMNameFormat
.LOWERCASE
), sourceSet
);
81 // ROM building parameters
82 RomBuildParameters build
= new RomBuildParameters();
84 // The boot libraries which will be placed first always
85 Set
<Path
> bootPaths
= new LinkedHashSet
<>();
87 // Get all of the libraries to translate
88 Set
<Path
> normalPaths
= new LinkedHashSet
<>();
89 for (VMLibraryTask task
: VMRomDependencies
.libraries(__task
,
92 // If we are single source set and this library is another
93 // source set, then do not include it here
94 String taskSourceSet
= task
.getSourceSet();
95 if (isSingleSourceSet
&&
96 !sourceSet
.equals(taskSourceSet
))
99 // Determine the path set
100 Set
<Path
> pathSet
= new LinkedHashSet
<>();
101 for (File f
: task
.getOutputs().getFiles().getFiles())
102 pathSet
.add(f
.toPath());
104 // Is this the boot loader library?
105 SquirrelJMEPluginConfiguration config
=
106 SquirrelJMEPluginConfiguration
.configurationOrNull(
108 boolean isMain
= SourceSet
.MAIN_SOURCE_SET_NAME
109 .equals(taskSourceSet
);
110 boolean isBootLoader
= (isMain
&& config
!= null &&
111 config
.isBootLoader
);
112 boolean isMainLauncher
= (isMain
&& config
!= null &&
113 config
.isMainLauncher
);
115 // If this is the boot loader, add our paths
116 if (bootLoaderEnabled
)
120 build
.bootLoaderMainClass
= config
.bootLoaderMainClass
;
121 build
.bootLoaderClassPath
= pathSet
.toArray(
122 new Path
[pathSet
.size()]);
125 // If this is the launcher, set the information needed to
126 // make sure it can actually be launched properly
129 UnassistedLaunchEntry entry
= config
.primaryEntry();
131 build
.launcherMainClass
= entry
.mainClass
;
132 build
.launcherArgs
= entry
.args();
133 build
.launcherClassPath
= VMHelpers
.runClassPath(task
,
138 // Add to the correct set of paths
139 if (bootLoaderEnabled
&& isBootLoader
)
140 bootPaths
.addAll(pathSet
);
142 normalPaths
.addAll(pathSet
);
145 // Make sure the boot libraries are always first
146 Set
<Path
> libPaths
= new LinkedHashSet
<>();
147 if (bootLoaderEnabled
)
148 libPaths
.addAll(bootPaths
);
149 libPaths
.addAll(normalPaths
);
151 // Setup output file for writing
152 try (OutputStream out
= Files
.newOutputStream(tempFile
,
153 StandardOpenOption
.WRITE
, StandardOpenOption
.TRUNCATE_EXISTING
,
154 StandardOpenOption
.CREATE
))
156 // Run the ROM processing
157 vmType
.processRom((VMBaseTask
)__task
,
158 this.classifier
.getBangletVariant(), out
, build
,
159 new ArrayList
<>(libPaths
));
163 VMHelpers
.recompressZip(tempFile
);
165 // Move the file over
167 __task
.getOutputs().getFiles().getSingleFile().toPath(),
168 StandardCopyOption
.REPLACE_EXISTING
);
170 catch (IOException e
)
172 throw new RuntimeException("I/O Error linking ROM for " +
173 __task
.getProject().getName(), e
);
176 // Always try to cleanup the temporary file
179 if (tempFile
!= null)
182 Files
.deleteIfExists(tempFile
);
184 catch (IOException ignored
)