Skip to content

Commit

Permalink
Imlplemented OptifinePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
FlowArg committed Oct 20, 2020
1 parent 4884726 commit 61a9e4b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@

public class Optifine implements JsonSerializable {
private final String name;
private final String url;
private final int size;

public Optifine(String name, String url, int size) {
public Optifine(String name, int size) {
this.name = name;
this.url = url;
this.size = size;
}

public String getName() {
return this.name;
}

public String getUrl() {
return this.url;
}

public int getSize() {
return this.size;
}
Expand All @@ -36,7 +30,6 @@ public String toJson()
{
final JsonObject obj = new JsonObject();
obj.addProperty("name", this.name);
obj.addProperty("url", this.url);
obj.addProperty("size", this.size);
return obj.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package fr.antoineok.flowupdater.optifineplugin;


import fr.flowarg.flowio.FileUtils;
import fr.flowarg.pluginloaderapi.plugin.Plugin;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Objects;

public class OptifinePlugin extends Plugin {
Expand All @@ -31,28 +34,34 @@ public void onStart() {
public Optifine getOptifine(String optifineVersion) throws IOException {

String name = "OptiFine_" + optifineVersion + ".jar";
if(optifineVersion.startsWith("preview"))
name = optifineVersion + ".jar";

HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse("http://optifine.net/downloadx")).newBuilder();
urlBuilder.addQueryParameter("f", name);
urlBuilder.addQueryParameter("x", getJson(optifineVersion));
urlBuilder.addQueryParameter("x", this.getJson(optifineVersion));

String newUrl = urlBuilder.build().toString();


Request request = new Request.Builder()
.url(newUrl)
.build();
Response response = client.newCall(request).execute();
final int length = Integer.parseInt(Objects.requireNonNull(response.header("Content-Length")));
if(length == 16)
throw new IOException("Given ersion of Optifine not found.");
final int length = Integer.parseInt(Objects.requireNonNull(response.header("Content-Length")));

assert response.body() != null;
final File output = new File(this.getDataPluginFolder(), name);
this.getLogger().info(String.format("Downloading %s from %s...", output.getName(), newUrl));
if(!output.exists() || FileUtils.getFileSizeBytes(output) != length)
Files.copy(response.body().byteStream(), output.toPath(), StandardCopyOption.REPLACE_EXISTING);
response.body().close();

return new Optifine(name, newUrl, length);
if(length <= 25)
throw new IOException("Given version of Optifine not found.");

return new Optifine(name, length);
}


public void shutdownOKHTTP()
{
client.dispatcher().executorService().shutdown();
Expand Down Expand Up @@ -82,8 +91,8 @@ private String getJson(String optifineVersion) {
String[] respLine = resp.split("\n");
response.body().close();
String keyLine = "";
for(String line : respLine){
if(line.contains("downloadx?f=OptiFine")){
for(String line : respLine) {
if(line.contains("downloadx?f=OptiFine")) {
keyLine = line;
break;
}
Expand All @@ -96,7 +105,6 @@ private String getJson(String optifineVersion) {
this.getLogger().printStackTrace(e);
}


return "";
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/flowarg/flowupdater/FlowUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private FlowUpdater(VanillaVersion version, ILogger logger, UpdaterOptions updat
{
this.logger = logger;
this.version = version;
this.logger.info(String.format("------------------------- FlowUpdater for Minecraft %s v%s -------------------------", this.version.getName(), "1.2.4"));
this.logger.info(String.format("------------------------- FlowUpdater for Minecraft %s v%s -------------------------", this.version.getName(), "1.2.5"));
this.externalFiles = externalFiles;
this.postExecutions = postExecutions;
this.forgeVersion = forgeVersion;
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/fr/flowarg/flowupdater/utils/ModFileDeleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ public void delete(Object... parameters) throws Exception
{
if(verifiedFiles.contains(fileInDir))
continue;
if(mods.isEmpty() && allCurseMods.isEmpty())
if(mods.isEmpty() && allCurseMods.isEmpty() && optifineParam == null)
{
if(!verifiedFiles.contains(fileInDir))
badFiles.add(fileInDir);
break;
}
else
{
Expand Down Expand Up @@ -77,19 +76,22 @@ public void delete(Object... parameters) throws Exception
if(optifinePluginLoaded)
{
final Optifine optifine = (Optifine)optifineParam;
if(optifine.getName().equalsIgnoreCase(fileInDir.getName()))
if(optifine != null)
{
if(getFileSizeBytes(fileInDir) == optifine.getSize())
if(optifine.getName().equalsIgnoreCase(fileInDir.getName()))
{
badFiles.remove(fileInDir);
verifiedFiles.add(fileInDir);
if(getFileSizeBytes(fileInDir) == optifine.getSize())
{
badFiles.remove(fileInDir);
verifiedFiles.add(fileInDir);
}
else badFiles.add(fileInDir);
}
else
{
if(!verifiedFiles.contains(fileInDir))
badFiles.add(fileInDir);
}
else badFiles.add(fileInDir);
}
else
{
if(!verifiedFiles.contains(fileInDir))
badFiles.add(fileInDir);
}
}

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/fr/flowarg/flowupdater/utils/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import fr.flowarg.pluginloaderapi.plugin.PluginLoader;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -103,30 +104,28 @@ public void loadCurseMods(File dir, AbstractForgeVersion forgeVersion) throws Ex
forgeVersion.setAllCurseMods(allCurseMods);
}

public void loadOptifine(File dir, AbstractForgeVersion forgeVersion) throws Exception
public void loadOptifine(File dir, AbstractForgeVersion forgeVersion)
{
try
{
Class.forName("fr.antoineok.flowupdater.optifineplugin.OptifinePlugin");
this.optifinePluginLoaded = true;
final OptifinePlugin optifinePlugin = OptifinePlugin.instance;
final Optifine optifine = optifinePlugin.getOptifine(forgeVersion.getOptifine());
final File file = new File(dir, optifine.getName());
if (file.exists())
{
if (getFileSizeBytes(file) != optifine.getSize())
{
file.delete();
this.downloadInfos.getCurseMods().add(optifine);
}
}
else this.downloadInfos.setOptifine(optifine);
}
catch (ClassNotFoundException e)
{
this.optifinePluginLoaded = false;
this.logger.err("Cannot install optifine: OptifinePlugin is not loaded. Please, enable the 'installOptifineAsMod' updater option !");
}
try
{
final OptifinePlugin optifinePlugin = OptifinePlugin.instance;
final Optifine optifine = optifinePlugin.getOptifine(forgeVersion.getOptifine());
this.downloadInfos.setOptifine(optifine);
}
catch (IOException e)
{
this.logger.printStackTrace(e);
}
}

public void configurePLA(File dir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,19 @@ public void installMods(File modsDir, boolean cursePluginLoaded, boolean optifin
});
}

Object ofObj = null;
if(optifinePluginLoaded)
{
if(this.downloadInfos.getOptifine() != null)
{
try
{
final Optifine optifine = (Optifine)this.downloadInfos.getOptifine();
IOUtils.download(this.logger, new URL(optifine.getUrl()), new File(modsDir, optifine.getName()));
ofObj = optifine;
final File pluginDir = new File(modsDir.getParentFile(), "FUPlugins/OptifinePlugin/");
final File optifineFile = new File(modsDir, optifine.getName());
if(!optifineFile.exists())
Files.copy(new File(pluginDir, optifine.getName()).toPath(), optifineFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (MalformedURLException e)
{
this.logger.printStackTrace(e);
Expand All @@ -176,7 +181,7 @@ public void installMods(File modsDir, boolean cursePluginLoaded, boolean optifin
}
}

this.fileDeleter.delete(modsDir, this.mods, cursePluginLoaded, this.allCurseMods, optifinePluginLoaded);
this.fileDeleter.delete(modsDir, this.mods, cursePluginLoaded, this.allCurseMods, optifinePluginLoaded, ofObj);
}

public ModFileDeleter getFileDeleter()
Expand Down

0 comments on commit 61a9e4b

Please sign in to comment.