2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.imgui.window.edit ;
2022-01-23 18:53:16 +01:00
import imgui.ImGui ;
import imgui.type.ImBoolean ;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv ;
import io.gitlab.jfronny.inceptum.common.Utils ;
import io.gitlab.jfronny.inceptum.imgui.GuiMain ;
import io.gitlab.jfronny.inceptum.imgui.control.Tab ;
import io.gitlab.jfronny.inceptum.imgui.window.AddModWindow ;
2022-10-27 20:54:55 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.instance.ModManager ;
import io.gitlab.jfronny.inceptum.launcher.system.instance.ModPath ;
import io.gitlab.jfronny.inceptum.launcher.system.instance.Mod ;
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource ;
2022-01-23 18:53:16 +01:00
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Path ;
2022-09-04 21:21:24 +02:00
import java.util.* ;
2022-01-23 18:53:16 +01:00
public class ModsTab extends Tab {
private final InstanceEditWindow window ;
private final ImBoolean filterUpdates = new ImBoolean ( ) ;
private Path selected = null ;
public ModsTab ( InstanceEditWindow window ) {
super ( " Mods " ) ;
this . window = window ;
}
@Override
protected void renderInner ( ) {
window . lastTabWasMods = true ;
2022-10-19 21:26:06 +02:00
if ( ! Files . exists ( window . instance . modsDir ( ) ) ) {
2022-01-23 18:53:16 +01:00
try {
2022-10-19 21:26:06 +02:00
Files . createDirectories ( window . instance . modsDir ( ) ) ;
2022-01-23 18:53:16 +01:00
} catch ( IOException e ) {
Utils . LOGGER . error ( " Could not create mods directory which was missing from this modded instance " , e ) ;
}
}
ImGui . beginChild ( " mods select " , 200 , 0 ) ;
if ( ImGui . button ( " Add " ) ) {
2022-10-19 21:26:06 +02:00
try {
GuiMain . WINDOWS . add ( new AddModWindow ( window . instance ) ) ;
} catch ( IOException e ) {
Utils . LOGGER . error ( " Could not open window " , e ) ;
}
2022-01-23 18:53:16 +01:00
}
ImGui . sameLine ( ) ;
2022-10-19 21:26:06 +02:00
if ( Files . exists ( window . instance . modsDir ( ) ) & & ImGui . button ( " Show " ) ) {
Utils . openFile ( window . instance . modsDir ( ) . toFile ( ) ) ;
2022-01-23 18:53:16 +01:00
}
ImGui . sameLine ( ) ;
2022-10-19 21:26:06 +02:00
if ( Files . exists ( window . instance . configDir ( ) ) & & ImGui . button ( " Configs " ) ) {
Utils . openFile ( window . instance . configDir ( ) . toFile ( ) ) ;
2022-01-23 18:53:16 +01:00
}
try {
2022-10-19 21:44:33 +02:00
Set < Mod > modSet = window . instance . getMods ( ) ;
2022-01-23 18:53:16 +01:00
boolean updatesFound = false ;
float scannedPercentage = 0 ;
boolean hasUnScanned = false ;
2022-10-19 21:26:06 +02:00
for ( Mod mod : modSet ) {
2022-10-27 20:54:55 +02:00
if ( window . instance . mds ( ) . hasScanned ( mod ) ) scannedPercentage + + ;
2022-01-23 18:53:16 +01:00
else hasUnScanned = true ;
2022-10-27 20:54:55 +02:00
for ( Optional < ModSource > value : mod . getMetadata ( ) . sources . values ( ) ) {
2022-01-23 18:53:16 +01:00
if ( value . isPresent ( ) ) {
updatesFound = true ;
break ;
}
if ( updatesFound )
break ;
}
}
scannedPercentage / = modSet . size ( ) ;
if ( hasUnScanned ) ImGui . progressBar ( scannedPercentage ) ;
if ( updatesFound )
ImGui . checkbox ( " Updatable " , filterUpdates ) ;
else
filterUpdates . set ( false ) ;
ImGui . separator ( ) ;
2022-10-19 21:26:06 +02:00
for ( Mod mod : modSet ) {
2022-01-23 18:53:16 +01:00
updatesFound = false ;
2022-10-27 20:54:55 +02:00
for ( Optional < ModSource > value : mod . getMetadata ( ) . sources . values ( ) ) {
updatesFound | = value . isPresent ( ) ;
2022-01-23 18:53:16 +01:00
}
if ( filterUpdates . get ( ) & & ! updatesFound ) continue ;
2022-10-27 20:54:55 +02:00
if ( ImGui . checkbox ( " ## " + mod . getName ( ) , mod . isEnabled ( ) ) ) {
Path newSel = ModPath . toggle ( mod . getMetadataPath ( ) ) ;
2022-01-23 18:53:16 +01:00
try {
2022-10-27 20:54:55 +02:00
Files . move ( mod . getMetadataPath ( ) , newSel ) ;
if ( mod . getMetadataPath ( ) . equals ( selected ) ) selected = newSel ;
2022-01-23 18:53:16 +01:00
} catch ( IOException e ) {
2022-09-04 21:21:24 +02:00
LauncherEnv . showError ( " Could not change disabled state " , e ) ;
2022-01-23 18:53:16 +01:00
}
}
ImGui . sameLine ( ) ;
2022-10-27 20:54:55 +02:00
if ( ImGui . button ( mod . getName ( ) ) ) selected = mod . getMetadataPath ( ) ;
2022-01-23 18:53:16 +01:00
}
} catch ( IOException e ) {
2022-01-27 17:57:13 +01:00
Utils . LOGGER . error ( " Could not show mod list " , e ) ;
2022-01-23 18:53:16 +01:00
}
ImGui . endChild ( ) ;
ImGui . sameLine ( ) ;
ImGui . beginGroup ( ) ;
if ( selected = = null ) {
ImGui . text ( " Select a mod to view settings " ) ;
2022-10-19 21:44:33 +02:00
} else if ( window . instance . mds ( ) . hasScanned ( selected ) ) {
Mod md = window . instance . mds ( ) . get ( selected ) ;
2022-01-23 18:53:16 +01:00
ImGui . text ( md . getName ( ) ) ;
ImGui . separator ( ) ;
for ( String s : md . getDescription ( ) ) {
ImGui . text ( s ) ;
}
ImGui . separator ( ) ;
2022-10-27 20:54:55 +02:00
Map < ModSource , Optional < ModSource > > sources = md . getMetadata ( ) . sources ;
ImGui . text ( " Sources: " ) ;
if ( sources . isEmpty ( ) )
ImGui . bulletText ( " Local Drive " ) ;
else {
for ( Map . Entry < ModSource , Optional < ModSource > > source : sources . entrySet ( ) ) {
ImGui . bulletText ( source . getKey ( ) . getName ( ) ) ;
source . getValue ( ) . ifPresent ( update - > {
ImGui . sameLine ( ) ;
if ( ImGui . button ( " Update to " + update . getVersion ( ) ) ) {
try {
selected = md . update ( update ) ;
} catch ( IOException e ) {
LauncherEnv . showError ( " Update failed " , e ) ;
2022-01-23 18:53:16 +01:00
}
}
2022-10-27 20:54:55 +02:00
} ) ;
2022-01-23 18:53:16 +01:00
}
}
if ( ImGui . button ( " Delete " ) ) {
2022-10-27 20:54:55 +02:00
if ( ! md . getMetadata ( ) . dependents . isEmpty ( ) )
LauncherEnv . showError ( " This mod still has the following dependent mods installed: " + String . join ( " , " , md . getMetadata ( ) . dependents ) , " Dependents present " ) ;
2022-01-23 18:53:16 +01:00
else {
try {
2022-10-27 20:54:55 +02:00
md . delete ( ) ;
2022-01-23 18:53:16 +01:00
} catch ( IOException e ) {
2022-09-04 21:21:24 +02:00
LauncherEnv . showError ( " Couldn't delete the file " , e ) ;
2022-01-23 18:53:16 +01:00
}
selected = null ;
}
}
2022-09-04 21:21:24 +02:00
} else {
2022-01-23 18:53:16 +01:00
ImGui . text ( " This mod has not yet been scanned, please be patient " ) ;
}
ImGui . endGroup ( ) ;
}
@Override
protected boolean canShow ( ) {
return window . instance . isFabric ( ) ;
}
}