Add world decrypt // and package addons to .mcaddon

This commit is contained in:
Li 2025-01-21 22:58:26 +13:00
parent a441bfe929
commit b67526b2cc
15 changed files with 565 additions and 125 deletions

184
McCrypt/PackData/PEntry.cs Normal file
View file

@ -0,0 +1,184 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace McCrypt.PackData
{
public class PEntry
{
private string originalPath;
public String FilePath
{
get
{
return originalPath;
}
}
public String ManifestPath
{
get
{
return Path.Combine(originalPath, "manifest.json");
}
}
public String WorldResourcePacksPath
{
get
{
return Path.Combine(this.FilePath, "world_resource_packs.json");
}
}
public String WorldBehaviourPacksPath
{
get
{
return Path.Combine(this.FilePath, "world_behavior_packs.json");
}
}
public String SubResourcePacks
{
get
{
return Path.Combine(this.FilePath, "resource_packs");
}
}
public String SubBehaviourPacks
{
get
{
return Path.Combine(this.FilePath, "behavior_packs");
}
}
public bool IsEncrypted
{
get
{
if (this.ProductType != "minecraftWorlds") return true;
else return Marketplace.IsLevelEncrypted(this.FilePath);
}
}
public bool HasDependancies
{
get
{
return DependsUUID.Length >= 1;
}
}
public String[] DependsUUID
{
get
{
if (File.Exists(this.ManifestPath))
{
return Manifest.ReadDependancyUuids(this.ManifestPath);
}
else if(this.ProductType == "minecraftWorlds")
{
List<String> uuids = new List<String>();
if(File.Exists(WorldResourcePacksPath)) uuids.AddRange(Manifest.ReadWorldPackList(WorldResourcePacksPath));
if(File.Exists(WorldBehaviourPacksPath)) uuids.AddRange(Manifest.ReadWorldPackList(WorldBehaviourPacksPath));
return uuids.ToArray();
}
else
{
return new String[0] { };
}
}
}
public String Name
{
get
{
if (File.Exists(this.ManifestPath))
return Manifest.ReadName(this.ManifestPath);
else if (File.Exists(Path.Combine(FilePath, "levelname.txt")))
return File.ReadAllText(Path.Combine(FilePath, "levelname.txt"));
else
return "Untitled";
}
}
public String Type {
get
{
if(File.Exists(this.ManifestPath))
return Manifest.ReadType(this.ManifestPath);
else if (File.Exists(Path.Combine(FilePath, "levelname.txt")))
return "minecraftWorlds";
else
return Path.GetFileName(Path.GetDirectoryName(this.FilePath));
}
}
public String ProductType
{
get
{
if (File.Exists(this.ManifestPath))
{
string ptype = Manifest.ReadProductType(this.ManifestPath);
if (ptype == null)
{
string type = Manifest.ReadType(this.ManifestPath);
switch (type)
{
case "resources":
return "resource_packs";
case "skin_pack":
return "skin_packs";
case "world_template":
return "world_templates";
case "data":
return "behaviour_packs";
case "persona_piece":
return "persona";
}
}
return ptype;
}
else if (File.Exists(Path.Combine(FilePath, "levelname.txt")))
{
return "minecraftWorlds";
}
else if (this.HasDependancies)
{
return "addon";
}
else
{
return Path.GetFileName(Path.GetDirectoryName(this.FilePath));
}
}
}
public String Uuid
{
get
{
if(File.Exists(this.ManifestPath))
{
return Manifest.ReadUUID(this.ManifestPath);
}
else
{
return new Guid().ToString();
}
}
}
public PEntry(string path)
{
this.originalPath = path;
}
}
}

104
McCrypt/PackData/PReader.cs Normal file
View file

@ -0,0 +1,104 @@
using McCrypt.PackData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace McCrypt.PackData
{
public class PReader
{
private List<PEntry> entries = new List<PEntry>();
private HashSet<string> hiddenUuids = new HashSet<String>();
public PEntry[] GetDependancies(PEntry baseentry)
{
List<PEntry> dependancyList = new List<PEntry>();
foreach (PEntry pentry in entries)
{
if (baseentry.DependsUUID.Contains<string>(pentry.Uuid))
dependancyList.Add(pentry);
// check sub packs in world templates ..
if(pentry.ProductType == "world_templates")
{
if (Directory.Exists(pentry.SubResourcePacks))
{
foreach (string resourcePack in Directory.GetDirectories(pentry.SubResourcePacks, "*", SearchOption.TopDirectoryOnly))
{
PEntry subPentry = new PEntry(Path.Combine(pentry.SubResourcePacks, resourcePack));
if (baseentry.DependsUUID.Contains<string>(subPentry.Uuid))
dependancyList.Add(subPentry);
}
}
if (Directory.Exists(pentry.SubBehaviourPacks))
{
foreach (string behaviourPack in Directory.GetDirectories(pentry.SubBehaviourPacks, "*", SearchOption.TopDirectoryOnly))
{
PEntry subPentry = new PEntry(Path.Combine(pentry.SubBehaviourPacks, behaviourPack));
if (baseentry.DependsUUID.Contains<string>(subPentry.Uuid))
dependancyList.Add(subPentry);
}
}
}
}
return dependancyList.ToArray();
}
public PEntry[] PEntryList
{
get
{
List<PEntry> publicEntries = new List<PEntry>();
foreach (PEntry entry in entries)
{
if (!hiddenUuids.Contains(entry.Uuid))
{
publicEntries.Add(entry);
}
}
return publicEntries.ToArray();
}
}
public PReader()
{
// search premium cache
foreach (string searchFolder in Config.SearchFolders)
{
foreach (string searchModule in Config.SearchModules)
{
string moduleFolder = Path.Combine(searchFolder, searchModule);
if (Directory.Exists(moduleFolder))
{
foreach (string moduleItem in Directory.GetDirectories(moduleFolder, "*", SearchOption.TopDirectoryOnly))
{
PEntry entry = new PEntry(moduleItem);
if (entry.ProductType == "minecraftWorlds" && !Config.DecryptExistingWorlds) continue;
if (entry.ProductType != "minecraftWorlds" && !hiddenUuids.Contains(entry.Uuid)) foreach (string uuid in entry.DependsUUID) hiddenUuids.Add(uuid);
if (!entry.IsEncrypted) continue;
entries.Add(entry);
}
}
}
}
if (Config.DecryptExistingWorlds && Directory.Exists(Config.WorldsFolder))
{
foreach (string moduleItem in Directory.GetDirectories(Config.WorldsFolder, "*", SearchOption.TopDirectoryOnly))
{
PEntry entry = new PEntry(moduleItem);
if (entry.ProductType != "minecraftWorlds" && !hiddenUuids.Contains(entry.Uuid)) foreach (string uuid in entry.DependsUUID) hiddenUuids.Add(uuid);
if (!entry.IsEncrypted) continue;
entries.Add(entry);
}
}
}
}
}