2121import javax .security .auth .login .CredentialException ;
2222import javax .security .sasl .AuthenticationException ;
2323
24+ import com .google .common .collect .ImmutableSet ;
2425import com .wildermods .thrixlvault .steam .CompletedDownload ;
2526import com .wildermods .thrixlvault .steam .FailedDownload ;
2627import com .wildermods .thrixlvault .steam .IManifest ;
3031
3132import static com .wildermods .thrixlvault .SteamDownloader .SteamState .*;
3233
33- public class SteamDownloader {
34+ public class SteamDownloader extends Downloader < ISteamDownloadable , ISteamDownload > {
3435
3536 private static final Object GLOBAL_RUN_LOCK = new Object (); // So we only have one instance of steamcmd operating at a time.
3637
3738 public static final Path DEFAULT_APP_INSTALL_DIR = Path .of (System .getProperty ("user.home" )).resolve ("thrixlvault" ).resolve ("current_download" );
3839 private final Path installDir ;
3940 private final String username ;
40- private final Collection <ISteamDownloadable > downloadables ;
4141 private final HashMap <IManifest , ISteamDownload > processedDownloads = new HashMap <IManifest , ISteamDownload >();
4242
4343 private volatile Process process ;
4444 private volatile SteamState prevState = SteamState .UNINITIALIZED ;
4545 private volatile SteamState steamState = SteamState .UNINITIALIZED ;
4646 private volatile boolean userInput = false ;
4747 private volatile Instant lastResponse = Instant .now ();
48+ private volatile Duration hangTimeout = Duration .ofSeconds (30 );
49+ private volatile Duration downloadTimeout = Duration .ofMinutes (7 );
50+ private volatile Consumer <ISteamDownload > onManifestDownload = (d ) -> {};
4851
4952 private final Object interruptLock = new Object ();
53+ private final Object stateLock = new Object ();
5054 private volatile boolean interrupt = false ;;
5155
5256 private volatile ISteamDownloadable currentDownload ;
5357
5458 private static final Pattern ERROR_REGEX = Pattern .compile ("ERROR \\ ((?<error>.*)\\ )\\ n" );
5559
5660 public SteamDownloader (String username , Collection <ISteamDownloadable > downloads ) {
61+ super (downloads );
5762 this .username = username ;
58- this .downloadables = downloads ;
5963 this .installDir = DEFAULT_APP_INSTALL_DIR ;
6064 }
6165
6266 public SteamDownloader (String username , Collection <ISteamDownloadable > downloads , Path installDir ) {
67+ super (downloads );
6368 this .username = username ;
64- this .downloadables = downloads ;
6569 this .installDir = installDir ;
6670 }
6771
68- public Set <ISteamDownload > run () throws Throwable {
69- return run ((c ) -> {});
72+ public SteamDownloader setHangTimeout (Duration hangTimeout ) {
73+ this .hangTimeout = hangTimeout ;
74+ return this ;
7075 }
7176
72- public Set <ISteamDownload > run (Consumer <ISteamDownload > onManifestDownload ) throws IOException , InterruptedException {
73- return run (onManifestDownload , Duration .ofSeconds (30 ), Duration .ofMinutes (7 ));
77+ public SteamDownloader setDownloadTimeout (Duration downloadTimeout ) {
78+ this .downloadTimeout = downloadTimeout ;
79+ return this ;
7480 }
75-
76- public Set <ISteamDownload > run (Consumer <ISteamDownload > onManifestDownload , Duration hangTimeout , Duration downloadTimeout ) throws IOException , InterruptedException {
81+
82+ public SteamDownloader setConsumer (Consumer <ISteamDownload > onManifestDownload ) {
83+ this .onManifestDownload = onManifestDownload ;
84+ return this ;
85+ }
86+
87+ @ Override
88+ public Set <ISteamDownload > runImpl () throws IOException {
7789 synchronized (GLOBAL_RUN_LOCK ) {
7890 System .out .println ("[DOWNLOADER] Acquired global run lock" );
7991 return this .runInternal (onManifestDownload , hangTimeout , downloadTimeout );
@@ -328,6 +340,11 @@ else if (state == DOWNLOADING) {
328340 return Set .copyOf (processedDownloads .values ());
329341 }
330342
343+ @ Override
344+ protected ImmutableSet <ISteamDownload > getDownloadsInProgress () throws UnsupportedOperationException {
345+ throw new UnsupportedOperationException ();
346+ }
347+
331348 private void putDownload (Consumer <ISteamDownload > onManifestDownload , IManifest manifest , ISteamDownload download ) {
332349 this .processedDownloads .put (manifest , download );
333350 onManifestDownload .accept (download );
@@ -382,14 +399,17 @@ public SteamState getState() {
382399 return steamState ;
383400 }
384401
385- private synchronized SteamState setState (SteamState newState ) {
386- prevState = getState ();
387- steamState = newState ;
388- System .out .println ("\n [DOWNLOADER/STATE]: Steam state changed from " + prevState + " to " + newState );
389- return prevState ;
402+ private SteamState setState (SteamState newState ) {
403+ synchronized (stateLock ) {
404+ prevState = getState ();
405+ steamState = newState ;
406+ System .out .println ("\n [DOWNLOADER/STATE]: Steam state changed from " + prevState + " to " + newState );
407+ return prevState ;
408+ }
390409 }
391410
392411 public enum SteamState {
393412 UNINITIALIZED , INITIALIZE , SETUP_DIR , LOGGING_IN , DOWNLOADING , FINISHED , FAILURE
394413 }
414+
395415}
0 commit comments