This commit is contained in:
Daniel Mills 2020-09-02 12:29:51 -04:00
parent 6d3a35517b
commit ea5dd69719
7 changed files with 247 additions and 14 deletions

View File

@ -28,6 +28,7 @@ import com.volmit.iris.util.GroupedExecutor;
import com.volmit.iris.util.IO; import com.volmit.iris.util.IO;
import com.volmit.iris.util.IrisLock; import com.volmit.iris.util.IrisLock;
import com.volmit.iris.util.IrisPostBlockFilter; import com.volmit.iris.util.IrisPostBlockFilter;
import com.volmit.iris.util.J;
import com.volmit.iris.util.KList; import com.volmit.iris.util.KList;
import com.volmit.iris.util.MortarPlugin; import com.volmit.iris.util.MortarPlugin;
import com.volmit.iris.util.Permission; import com.volmit.iris.util.Permission;
@ -83,9 +84,15 @@ public class Iris extends MortarPlugin
struct = new StructureManager(); struct = new StructureManager();
proj = new ProjectManager(); proj = new ProjectManager();
board = new IrisBoardManager(); board = new IrisBoardManager();
J.a(() -> IO.delete(getTemp()));
super.onEnable(); super.onEnable();
} }
public static File getTemp()
{
return instance.getDataFolder("cache", "temp");
}
public void onDisable() public void onDisable()
{ {
proj.close(); proj.close();
@ -208,6 +215,29 @@ public class Iris extends MortarPlugin
return ""; return "";
} }
public static File getNonCachedFile(String name, String url)
{
String h = IO.hash(name + "*" + url);
File f = Iris.instance.getDataFile("cache", h.substring(0, 2), h.substring(3, 5), h);
try(BufferedInputStream in = new BufferedInputStream(new URL(url).openStream()); FileOutputStream fileOutputStream = new FileOutputStream(f))
{
byte dataBuffer[] = new byte[1024];
int bytesRead;
while((bytesRead = in.read(dataBuffer, 0, 1024)) != -1)
{
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
}
catch(IOException e)
{
}
return f;
}
public static void warn(String string) public static void warn(String string)
{ {
msg(C.YELLOW + string); msg(C.YELLOW + string);

View File

@ -21,8 +21,10 @@ import org.bukkit.block.Biome;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.zeroturnaround.zip.ZipUtil; import org.zeroturnaround.zip.ZipUtil;
import org.zeroturnaround.zip.commons.FileUtils;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.volmit.iris.gen.IrisChunkGenerator; import com.volmit.iris.gen.IrisChunkGenerator;
import com.volmit.iris.gen.post.Post; import com.volmit.iris.gen.post.Post;
import com.volmit.iris.object.DecorationPart; import com.volmit.iris.object.DecorationPart;
@ -78,6 +80,7 @@ import lombok.Data;
@Data @Data
public class ProjectManager public class ProjectManager
{ {
private KMap<String, String> cacheListing = null;
private IrisChunkGenerator currentProject; private IrisChunkGenerator currentProject;
private TaskExecutor tx = new TaskExecutor(8, Thread.MIN_PRIORITY, "Iris Compiler"); private TaskExecutor tx = new TaskExecutor(8, Thread.MIN_PRIORITY, "Iris Compiler");
private ReentrantLock lock = new ReentrantLock(); private ReentrantLock lock = new ReentrantLock();
@ -107,6 +110,142 @@ public class ProjectManager
}); });
} }
public void downloadSearch(MortarSender sender, String key, boolean trim)
{
String repo = getListing(false).get(key);
if(repo == null)
{
sender.sendMessage("Couldn't find the pack '" + key + "' in the iris repo listing.");
return;
}
sender.sendMessage("Found '" + key + "' in the Iris Listing as " + repo);
try
{
download(sender, repo, trim);
}
catch(JsonSyntaxException | IOException e)
{
sender.sendMessage("Failed to download '" + key + "'.");
}
}
public void download(MortarSender sender, String repo, boolean trim) throws JsonSyntaxException, IOException
{
String url = "https://codeload.github.com/" + repo + "/zip/master";
sender.sendMessage("Downloading " + url);
File zip = Iris.getNonCachedFile("pack-" + trim + "-" + repo, url);
File temp = Iris.getTemp();
File work = new File(temp, "dl-" + UUID.randomUUID());
File packs = Iris.instance.getDataFolder("packs");
sender.sendMessage("Unpacking " + repo);
ZipUtil.unpack(zip, work);
File dir = work.listFiles().length == 1 && work.listFiles()[0].isDirectory() ? work.listFiles()[0] : null;
if(dir == null)
{
sender.sendMessage("Invalid Format. Missing root folder or too many folders!");
return;
}
File dimensions = new File(dir, "dimensions");
if(!(dimensions.exists() && dimensions.isDirectory()))
{
sender.sendMessage("Invalid Format. Missing dimensions folder");
return;
}
if(dimensions.listFiles().length != 1)
{
sender.sendMessage("Dimensions folder must have 1 file in it");
return;
}
File dim = dimensions.listFiles()[0];
if(!dim.isFile())
{
sender.sendMessage("Invalid dimension (folder) in dimensions folder");
return;
}
String key = dim.getName().split("\\Q.\\E")[0];
IrisDimension d = new Gson().fromJson(IO.readAll(dim), IrisDimension.class);
sender.sendMessage("Importing " + d.getName() + " (" + key + ")");
Iris.globaldata.dump();
Iris.globaldata.preferFolder(null);
if(Iris.globaldata.getDimensionLoader().load(key) != null)
{
sender.sendMessage("Another dimension in the packs folder is already using the key " + key + " IMPORT FAILED!");
return;
}
File packEntry = new File(packs, key);
if(packEntry.exists())
{
sender.sendMessage("Another pack is using the key " + key + ". IMPORT FAILED!");
return;
}
FileUtils.copyDirectory(dir, packEntry);
if(trim)
{
sender.sendMessage("Trimming " + key);
File cp = compilePackage(sender, key, false, false);
IO.delete(packEntry);
packEntry.mkdirs();
ZipUtil.unpack(cp, packEntry);
}
sender.sendMessage("Successfully Aquired " + d.getName());
Iris.globaldata.dump();
Iris.globaldata.preferFolder(null);
}
public KMap<String, String> getListing(boolean cached)
{
if(cached && cacheListing != null)
{
return cacheListing;
}
JSONArray a = new JSONArray();
if(cached)
{
a = new JSONArray(Iris.getCached("cachedlisting", "https://raw.githubusercontent.com/VolmitSoftware/Iris/master/listing.json"));
}
else
{
a = new JSONArray(Iris.getNonCached(!cached + "listing", "https://raw.githubusercontent.com/VolmitSoftware/Iris/master/listing.json"));
}
KMap<String, String> l = new KMap<>();
for(int i = 0; i < a.length(); i++)
{
try
{
String m = a.getString(i).trim();
String[] v = m.split("\\Q \\E");
l.put(v[0], v[1]);
}
catch(Throwable e)
{
}
}
return l;
}
public boolean isProjectOpen() public boolean isProjectOpen()
{ {
return currentProject != null; return currentProject != null;
@ -254,7 +393,7 @@ public class ProjectManager
} }
} }
public File compilePackage(MortarSender sender, String dim, boolean obfuscate) public File compilePackage(MortarSender sender, String dim, boolean obfuscate, boolean minify)
{ {
Iris.globaldata.dump(); Iris.globaldata.dump();
Iris.globaldata.preferFolder(null); Iris.globaldata.preferFolder(null);
@ -300,7 +439,7 @@ public class ProjectManager
continue; continue;
} }
String name = UUID.randomUUID().toString().replaceAll("-", ""); String name = !obfuscate ? k : UUID.randomUUID().toString().replaceAll("-", "");
b.append(name); b.append(name);
newNames.add(name); newNames.add(name);
renameObjects.put(k, name); renameObjects.put(k, name);
@ -325,7 +464,7 @@ public class ProjectManager
continue; continue;
} }
String name = UUID.randomUUID().toString().replaceAll("-", ""); String name = !obfuscate ? k : UUID.randomUUID().toString().replaceAll("-", "");
b.append(name); b.append(name);
newNames.add(name); newNames.add(name);
renameObjects.put(k, name); renameObjects.put(k, name);
@ -350,7 +489,7 @@ public class ProjectManager
continue; continue;
} }
String name = UUID.randomUUID().toString().replaceAll("-", ""); String name = !obfuscate ? k : UUID.randomUUID().toString().replaceAll("-", "");
b.append(name); b.append(name);
newNames.add(name); newNames.add(name);
renameObjects.put(k, name); renameObjects.put(k, name);
@ -442,13 +581,13 @@ public class ProjectManager
try try
{ {
a = new JSONObject(new Gson().toJson(dimension)).toString(0); a = new JSONObject(new Gson().toJson(dimension)).toString(minify ? 0 : 4);
IO.writeAll(new File(folder, "dimensions/" + dimension.getLoadKey() + ".json"), a); IO.writeAll(new File(folder, "dimensions/" + dimension.getLoadKey() + ".json"), a);
b.append(IO.hash(a)); b.append(IO.hash(a));
for(IrisGenerator i : generators) for(IrisGenerator i : generators)
{ {
a = new JSONObject(new Gson().toJson(i)).toString(0); a = new JSONObject(new Gson().toJson(i)).toString(minify ? 0 : 4);
IO.writeAll(new File(folder, "generators/" + i.getLoadKey() + ".json"), a); IO.writeAll(new File(folder, "generators/" + i.getLoadKey() + ".json"), a);
b.append(IO.hash(a)); b.append(IO.hash(a));
} }
@ -458,28 +597,28 @@ public class ProjectManager
for(IrisRegion i : regions) for(IrisRegion i : regions)
{ {
a = new JSONObject(new Gson().toJson(i)).toString(0); a = new JSONObject(new Gson().toJson(i)).toString(minify ? 0 : 4);
IO.writeAll(new File(folder, "regions/" + i.getLoadKey() + ".json"), a); IO.writeAll(new File(folder, "regions/" + i.getLoadKey() + ".json"), a);
b.append(IO.hash(a)); b.append(IO.hash(a));
} }
for(IrisStructure i : structures) for(IrisStructure i : structures)
{ {
a = new JSONObject(new Gson().toJson(i)).toString(0); a = new JSONObject(new Gson().toJson(i)).toString(minify ? 0 : 4);
IO.writeAll(new File(folder, "structures/" + i.getLoadKey() + ".json"), a); IO.writeAll(new File(folder, "structures/" + i.getLoadKey() + ".json"), a);
b.append(IO.hash(a)); b.append(IO.hash(a));
} }
for(IrisBiome i : biomes) for(IrisBiome i : biomes)
{ {
a = new JSONObject(new Gson().toJson(i)).toString(0); a = new JSONObject(new Gson().toJson(i)).toString(minify ? 0 : 4);
IO.writeAll(new File(folder, "biomes/" + i.getLoadKey() + ".json"), a); IO.writeAll(new File(folder, "biomes/" + i.getLoadKey() + ".json"), a);
b.append(IO.hash(a)); b.append(IO.hash(a));
} }
for(IrisLootTable i : loot) for(IrisLootTable i : loot)
{ {
a = new JSONObject(new Gson().toJson(i)).toString(0); a = new JSONObject(new Gson().toJson(i)).toString(minify ? 0 : 4);
IO.writeAll(new File(folder, "loot/" + i.getLoadKey() + ".json"), a); IO.writeAll(new File(folder, "loot/" + i.getLoadKey() + ".json"), a);
b.append(IO.hash(a)); b.append(IO.hash(a));
} }
@ -491,7 +630,7 @@ public class ProjectManager
meta.put("hash", finalHash); meta.put("hash", finalHash);
meta.put("time", M.ms()); meta.put("time", M.ms());
meta.put("version", dimension.getVersion()); meta.put("version", dimension.getVersion());
IO.writeAll(new File(folder, "package.json"), meta.toString(0)); IO.writeAll(new File(folder, "package.json"), meta.toString(minify ? 0 : 4));
File p = new File(Iris.instance.getDataFolder(), "exports/" + dimension.getLoadKey() + ".iris"); File p = new File(Iris.instance.getDataFolder(), "exports/" + dimension.getLoadKey() + ".iris");
Iris.info("Compressing Package"); Iris.info("Compressing Package");
ZipUtil.pack(folder, p, 9); ZipUtil.pack(folder, p, 9);

View File

@ -82,11 +82,19 @@ public class CommandIrisCreate extends MortarCommand
else else
{ {
sender.sendMessage("Foind " + type + " dimension in packs folder. Repackaging"); sender.sendMessage("Foind " + type + " dimension in packs folder. Repackaging");
ZipUtil.unpack(Iris.proj.compilePackage(sender, type, true), iris); ZipUtil.unpack(Iris.proj.compilePackage(sender, type, true, true), iris);
} }
File dimf = new File(iris, "dimensions/" + type + ".json"); File dimf = new File(iris, "dimensions/" + type + ".json");
if(!dimf.exists() || !dimf.isFile())
{
Iris.globaldata.dump();
Iris.globaldata.preferFolder(null);
Iris.proj.downloadSearch(sender, type, false);
ZipUtil.unpack(Iris.proj.compilePackage(sender, type, true, true), iris);
}
if(!dimf.exists() || !dimf.isFile()) if(!dimf.exists() || !dimf.isFile())
{ {
sender.sendMessage("Can't find the " + dimf.getName() + " in the dimensions folder of this pack! Failed!"); sender.sendMessage("Can't find the " + dimf.getName() + " in the dimensions folder of this pack! Failed!");

View File

@ -16,6 +16,9 @@ public class CommandIrisStudio extends MortarCommand
@Command @Command
private CommandIrisStudioClose close; private CommandIrisStudioClose close;
@Command
private CommandIrisStudioDownload download;
@Command @Command
private CommandIrisStudioPackage pkg; private CommandIrisStudioPackage pkg;

View File

@ -0,0 +1,50 @@
package com.volmit.iris.command;
import com.volmit.iris.Iris;
import com.volmit.iris.util.C;
import com.volmit.iris.util.J;
import com.volmit.iris.util.MortarCommand;
import com.volmit.iris.util.MortarSender;
public class CommandIrisStudioDownload extends MortarCommand
{
public CommandIrisStudioDownload()
{
super("download", "down", "dl");
requiresPermission(Iris.perm.studio);
setDescription("Download a project.");
setCategory("Studio");
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
if(args.length < 1)
{
sender.sendMessage("/iris std dl " + C.BOLD + "<NAME>");
return true;
}
boolean trim = false;
for(String i : args)
{
if(i.equals("-t") || i.equals("--trim"))
{
trim = true;
}
}
boolean btrim = trim;
J.a(() -> Iris.proj.downloadSearch(sender, args[0], btrim));
return true;
}
@Override
protected String getArgsUsage()
{
return "<name> [-t/--trim]";
}
}

View File

@ -27,6 +27,7 @@ public class CommandIrisStudioPackage extends MortarCommand
J.a(() -> J.a(() ->
{ {
boolean o = false; boolean o = false;
boolean m = true;
for(String i : args) for(String i : args)
{ {
@ -37,7 +38,7 @@ public class CommandIrisStudioPackage extends MortarCommand
} }
String dim = args[0]; String dim = args[0];
Iris.proj.compilePackage(sender, dim, o); Iris.proj.compilePackage(sender, dim, o, m);
}); });
return true; return true;
@ -46,6 +47,6 @@ public class CommandIrisStudioPackage extends MortarCommand
@Override @Override
protected String getArgsUsage() protected String getArgsUsage()
{ {
return "[dimension] [-o]"; return "[dimension] [-o] [-m]";
} }
} }

View File

@ -330,6 +330,8 @@ public class IrisRegion extends IrisRegistrant implements IRare
names.addAll(caveBiomes); names.addAll(caveBiomes);
names.addAll(seaBiomes); names.addAll(seaBiomes);
names.addAll(shoreBiomes); names.addAll(shoreBiomes);
names.addAll(riverBiomes);
names.addAll(lakeBiomes);
spotBiomes.forEach((i) -> names.add(i.getBiome())); spotBiomes.forEach((i) -> names.add(i.getBiome()));
ridgeBiomes.forEach((i) -> names.add(i.getBiome())); ridgeBiomes.forEach((i) -> names.add(i.getBiome()));