generated from cortinico/kotlin-gradle-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: launching minecraft and non-working asset downloader (will be r…
…eplaced by .minecraft's assets)
- Loading branch information
1 parent
17d6d78
commit 23a7987
Showing
12 changed files
with
294 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ | |
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
build | ||
example/run | ||
.idea |
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,18 +1,21 @@ | ||
import club.aetherium.gradle.extension.RunMode.Companion.tweaker | ||
|
||
plugins { | ||
java | ||
id("club.aetherium.gradle") | ||
} | ||
|
||
minecraft { | ||
minecraftVersion = "1.8.9" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://raw.githubusercontent.com/BleachDev/cursed-mappings/main/") | ||
maven("https://maven.legacyfabric.net") | ||
} | ||
|
||
dependencies { | ||
add("mappings", "net.legacyfabric:yarn:1.8.9+build.mcp") | ||
mappings("net.legacyfabric:yarn:1.8.9+build.mcp") | ||
} | ||
|
||
minecraft { | ||
minecraftVersion = "1.8.9" | ||
runMode = tweaker("club.aetherium.example.Tweaker") | ||
} |
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,48 @@ | ||
package club.aetherium.example; | ||
|
||
import net.minecraft.launchwrapper.ITweaker; | ||
import net.minecraft.launchwrapper.LaunchClassLoader; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Tweaker implements ITweaker { | ||
private final List<String> launchArguments = new ArrayList<>(); | ||
|
||
@Override | ||
public void acceptOptions(List<String> args, File gameDir, File assetsDir, String profile) { | ||
this.launchArguments.addAll(args); | ||
|
||
if (!args.contains("--version") && profile != null) { | ||
launchArguments.add("--version"); | ||
launchArguments.add(profile); | ||
} | ||
|
||
if (!args.contains("--assetsDir") && assetsDir != null) { | ||
launchArguments.add("--assetsDir"); | ||
launchArguments.add(assetsDir.getAbsolutePath()); | ||
} | ||
|
||
if (!args.contains("--gameDir") && gameDir != null) { | ||
launchArguments.add("--gameDir"); | ||
launchArguments.add(gameDir.getAbsolutePath()); | ||
} | ||
System.out.println("AcceptOptions"); | ||
} | ||
|
||
@Override | ||
public void injectIntoClassLoader(LaunchClassLoader classLoader) { | ||
System.out.println("InjectIntoClassLoader"); | ||
} | ||
|
||
@Override | ||
public String getLaunchTarget() { | ||
return "net.minecraft.client.main.Main"; | ||
} | ||
|
||
@Override | ||
public String[] getLaunchArguments() { | ||
return launchArguments.toArray(new String[0]); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
plugin-build/plugin/src/main/java/club/aetherium/gradle/extension/RunMode.kt
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,23 @@ | ||
package club.aetherium.gradle.extension | ||
|
||
import org.gradle.api.Project | ||
|
||
data class RunMode( | ||
val vmArgs: List<String>, | ||
val runArgs: List<String>, | ||
val additionalDependencies: List<String>, | ||
val additionalRepositories: List<String>, | ||
val mainClass: String | ||
) { | ||
companion object { | ||
val Vanilla = RunMode(emptyList(), emptyList(), emptyList(), emptyList(), | ||
"net.minecraft.client.main.Main") | ||
fun tweaker(vararg tweakClasses: String) = RunMode( | ||
emptyList(), | ||
tweakClasses.map { "--tweakClass=$it" }, | ||
listOf("com.github.heni123321:LegacyLauncher:ac106bbe00"), | ||
listOf("https://jitpack.io"), | ||
"net.minecraft.launchwrapper.Launch" | ||
) | ||
} | ||
} |
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
99 changes: 99 additions & 0 deletions
99
plugin-build/plugin/src/main/java/club/aetherium/gradle/tasks/run/DownloadAssetsTask.kt
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,99 @@ | ||
package club.aetherium.gradle.tasks.run | ||
|
||
import club.aetherium.gradle.extension.MinecraftExtension | ||
import club.aetherium.gradle.utils.Downloader | ||
import club.aetherium.gradle.utils.manifest.MinecraftManifest | ||
import club.aetherium.gradle.utils.manifest.MinecraftManifest.gson | ||
import club.aetherium.gradle.utils.manifest.data.VersionData | ||
import com.google.gson.JsonObject | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.sync.Semaphore | ||
import kotlinx.coroutines.sync.withPermit | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.File | ||
import java.net.URL | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import java.util.concurrent.CompletableFuture | ||
|
||
private const val RESOURCES_URL = "https://resources.download.minecraft.net/" | ||
|
||
abstract class DownloadAssetsTask : DefaultTask() { | ||
@OutputDirectory | ||
val assetsDirectory = project.projectDir.resolve("build") | ||
.resolve(".aether").resolve("assets") | ||
private val minecraftVersion: Property<String> | ||
get() = project.extensions.getByType(MinecraftExtension::class.java).minecraftVersion | ||
|
||
private val aetherCache = project.projectDir.resolve("build").resolve(".aether") | ||
|
||
@TaskAction | ||
fun downloadAssets() { | ||
minecraftVersion.orNull ?: throw GradleException("Version must be set") | ||
|
||
val mcManifest = MinecraftManifest.fromId(minecraftVersion.get()) | ||
?: throw RuntimeException("Unknown version specified (${minecraftVersion.get()})") | ||
|
||
val manifest = gson.fromJson( | ||
URL(mcManifest.url).openStream().reader().readText(), | ||
VersionData::class.java | ||
) ?: throw RuntimeException("Failed to fetch version manifest") | ||
|
||
val get = URL(manifest.assetIndex.url).readText() | ||
|
||
val assetIndex = aetherCache.resolve("indexes/${manifest.assetIndex.id}.json") | ||
|
||
if (!assetIndex.exists()) { | ||
assetIndex.parentFile.mkdirs() | ||
assetIndex.createNewFile() | ||
assetIndex.writeText(get) | ||
} | ||
|
||
val json = gson.fromJson(get, JsonObject::class.java) | ||
val objects = json["objects"].asJsonObject | ||
|
||
var downloaded = AtomicInteger(0) | ||
|
||
fun assetDownloaded() { | ||
val prog = downloaded.get().toFloat() / objects.size().toFloat() | ||
|
||
val totalBoxes = 30 | ||
val neededBoxes = (prog * totalBoxes).toInt() | ||
val characters = "=".repeat(neededBoxes) | ||
val whitespaces = " ".repeat(totalBoxes - neededBoxes) | ||
|
||
logger.lifecycle( | ||
"\r[AetherGradle] Assets: [$characters$whitespaces] (${prog * 100})\r" | ||
) | ||
} | ||
|
||
objects.asMap().forEach { (_, value) -> | ||
val hash = value.asJsonObject["hash"]!!.asString | ||
val folder = assetsDirectory.resolve("objects/" + hash.substring(0, 2)) | ||
val file = File(folder, hash) | ||
val size = value.asJsonObject["size"]!!.asLong | ||
|
||
logger.lifecycle( | ||
"${file.exists()} | ${file.length()} / $size" | ||
) | ||
|
||
logger.lifecycle( | ||
"${file.exists()} | ${Downloader.hash(file)} / $hash" | ||
) | ||
|
||
if ((!file.exists() || file.length() != size) && | ||
!Downloader.hash(file).equals(hash, ignoreCase = true)) { | ||
val url = "$RESOURCES_URL${hash.substring(0, 2)}/$hash" | ||
val response = URL(url).readText() | ||
|
||
file.parentFile.mkdirs() | ||
file.writeText(response) | ||
downloaded.incrementAndGet() | ||
assetDownloaded() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.