Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package shop;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontFormatException;
- import java.awt.FontMetrics;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.imageio.ImageIO;
- import UE4_Packages.AthenaItemDefinition;
- import UE4_Packages.DisplayAssetPath;
- import UE4_Packages.FortHeroType;
- import UE4_Packages.FortWeaponMeleeItemDefinition;
- import UE4_Packages.Package;
- import UE4_Packages.ReadException;
- import UE4_Packages.UTexture2D;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import Converters.ItemDefinitionToBufferedImage;
- import Converters.Texture2DToBufferedImage;
- import Enums.RarityEnum;
- import UE4.GameFile;
- import UE4.PakFileReader;
- import core.Main;
- import logger.Log;
- import res.Resources;
- import util.Static;
- /**
- * @author FunGames
- *
- */
- public class ShopParser {
- public static BufferedImage vbucks = Resources.getVBucksIcon();
- public static BufferedImage parseShop(String date, String shop) {
- if (vbucks != null) {
- BufferedImage weekly = null;
- BufferedImage daily = null;
- try {
- JSONObject store = (JSONObject) new JSONParser().parse(shop);
- JSONArray storefronts = (JSONArray) store.get("storefronts");
- for (Object storefrontO : storefronts) {
- JSONObject storefront = (JSONObject) storefrontO;
- String storefrontName = (String) storefront.get("name");
- if (storefrontName.equals("BRWeeklyStorefront")) {
- List<BufferedImage> weeklyStore = parseStore((JSONArray) storefront.get("catalogEntries"));
- weekly = createShopImage(weeklyStore);
- }
- if (storefrontName.equals("BRDailyStorefront")) {
- List<BufferedImage> dailyStore = parseStore((JSONArray) storefront.get("catalogEntries"));
- daily = createShopImage(dailyStore);
- }
- }
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if(weekly != null && daily != null) {
- BufferedImage result = createFinalImage(weekly, daily);
- return result;
- }
- }
- } else {
- Log.consoleLog(Log.WARN, "Couldn't load Vbucks icon");
- }
- return null;
- }
- public static BufferedImage createFinalImage(BufferedImage weekly, BufferedImage daily) {
- Font font;
- try {
- font = Font.createFont(Font.TRUETYPE_FONT, Main.infoProvider.getFontInputStream());
- font = font.deriveFont(Font.BOLD, 100);
- } catch (FontFormatException | IOException e) {
- // TODO Auto-generated catch block
- font = new Font("SansSerif", Font.BOLD, 100);
- e.printStackTrace();
- }
- int maxSizeX = Math.max(weekly.getWidth(), daily.getWidth());
- int spaceAboveWeeklyStore = 150;
- int spaceBetweenWeeklyAndDailyStore = 150;
- int spaceAfterDailyStore = 100;
- int spaceLeft = 100;
- int spaceRight = 100;
- int totalX = spaceLeft + maxSizeX + spaceRight;
- int totalY = spaceAboveWeeklyStore + weekly.getHeight() + spaceBetweenWeeklyAndDailyStore + daily.getHeight() + spaceAfterDailyStore;
- BufferedImage shop = new BufferedImage(totalX, totalY, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2 = shop.createGraphics();
- g2.setFont(font);
- g2.setColor(Color.WHITE);
- g2.drawString("FEATURED STORE", spaceLeft + 10, 112);
- g2.drawImage(weekly, spaceLeft, spaceAboveWeeklyStore, null);
- g2.setFont(font);
- g2.setColor(Color.WHITE);
- g2.drawString("DAILY STORE", spaceLeft + 10, spaceAboveWeeklyStore + weekly.getHeight() + 112);
- g2.drawImage(daily, spaceLeft, spaceAboveWeeklyStore + weekly.getHeight() + spaceBetweenWeeklyAndDailyStore, null);
- return shop;
- }
- public static BufferedImage createShopImage(List<BufferedImage> shopIcons) {
- int space = 30;
- if(shopIcons.isEmpty()) {
- return null;
- }
- int x = shopIcons.get(0).getWidth();
- int y = shopIcons.get(0).getHeight();
- BufferedImage result = new BufferedImage(shopIcons.size() * (x + space) - space, y, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2 = result.createGraphics();
- int cX = 0;
- for(BufferedImage shopIcon : shopIcons) {
- g2.drawImage(shopIcon, cX, 0, null);
- cX += shopIcon.getWidth() + space;
- }
- return result;
- }
- public static List<BufferedImage> parseStore(JSONArray catalogEntries) {
- List<BufferedImage> shopImages = new ArrayList<>();
- for (Object catalogEntryO : catalogEntries) {
- JSONObject catalogEntry = (JSONObject) catalogEntryO;
- // Parse price
- long finalPrice = 0;
- JSONArray prices = (JSONArray) catalogEntry.get("prices");
- for (Object pO : prices) {
- JSONObject priceO = (JSONObject) pO;
- finalPrice += (long) priceO.get("finalPrice");
- }
- // Read Display Asset Path if available
- String displayAssetPath = catalogEntry.containsKey("displayAssetPath")
- ? (String) catalogEntry.get("displayAssetPath")
- : null;
- BufferedImage displayAssetTexture = loadDisplayAssetPath(displayAssetPath);
- // Read granted items
- List<String> grantedItems = new ArrayList<>();
- JSONArray grantedItemsArray = (JSONArray) catalogEntry.get("itemGrants");
- for (Object grantedItemO : grantedItemsArray) {
- JSONObject grantedItemObject = (JSONObject) grantedItemO;
- String granted = (String) grantedItemObject.get("templateId");
- String[] parts = granted.split(":");
- if (parts.length >= 2) {
- String folder = Main.pakHandler.getFolder(parts[0]);
- if (folder != null) {
- String path = folder + parts[1] + ".uasset";
- String gameSpecific = Package.toGameSpecificName(path, "FortniteGame/");
- String originalPath = Main.pakHandler.getFilePathIgnoreCase(gameSpecific);
- grantedItems.add(originalPath);
- }
- }
- }
- if(grantedItems.size() > 0) {
- BufferedImage featuredImage = null;
- try {
- AthenaItemDefinition featured = loadItemDefinition(grantedItems.get(0));
- featuredImage = makeFeaturedImage(featured, displayAssetTexture, (int) finalPrice);
- Graphics2D g2 = featuredImage.createGraphics();
- int additionalDrawPosX = 0;
- int additionalDrawPosY = 0;
- for(int i=1; i<grantedItems.size(); i++) {
- AthenaItemDefinition additionalItemDef = loadItemDefinition(grantedItems.get(i));
- BufferedImage additionalIcon = ItemDefinitionToBufferedImage.getImageNoDesc(additionalItemDef, Main.pakHandler.pakFileReaderIndices(), Main.pakHandler.loadedPaks(), Main.pakHandler.getLoadedGameFilesWithNames(), Static.mountPrefix, Main.infoProvider.getFontInputStream());
- additionalIcon = ItemDefinitionToBufferedImage.scaleImageByFactor(additionalIcon, Static.STORE_ADDITIONAL_IMAGE_SCALE_FACTOR);
- g2.drawImage(additionalIcon, additionalDrawPosX, additionalDrawPosY, null);
- additionalDrawPosY+= additionalIcon.getHeight();
- }
- } catch (Exception e) {
- System.out.println("Failed to load (parts) of shop offer" + grantedItems.get(0));
- } finally {
- if(featuredImage != null) {
- shopImages.add(featuredImage);
- }
- }
- }
- }
- return shopImages;
- }
- public static BufferedImage makeFeaturedImage(AthenaItemDefinition itemDefinition, BufferedImage displayAssetPath, int price) {
- try {
- return displayAssetPath != null ? ItemDefinitionToBufferedImage.getAsShopImage(itemDefinition, Main.infoProvider.getFontInputStream(), Resources.getVBucksIcon(), price, displayAssetPath) : ItemDefinitionToBufferedImage.getAsShopImage(itemDefinition, Main.pakHandler.pakFileReaderIndices(), Main.pakHandler.loadedPaks(), Main.pakHandler.getLoadedGameFilesWithNames(), Static.mountPrefix, Main.infoProvider.getFontInputStream(), Resources.getVBucksIcon(), price);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ReadException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public static AthenaItemDefinition loadItemDefinition(String path) {
- GameFile file = Main.pakHandler.loadGameFileByName(path);
- if (file != null) {
- Package itemDefinitionPackage = Main.pakHandler.loadGameFile(file);
- if (itemDefinitionPackage != null) {
- for (Object export : itemDefinitionPackage.getExports()) {
- if (export instanceof AthenaItemDefinition) {
- AthenaItemDefinition itemDef = (AthenaItemDefinition) export;
- return itemDef;
- }
- }
- }
- }
- return null;
- }
- public static BufferedImage loadItemDefinitionAsFeaturedImage(AthenaItemDefinition itemDefinition, int price) {
- Map<String, Integer> pakFileReaderIndices = Main.pakHandler.pakFileReaderIndices();
- PakFileReader[] loadedPaks = Main.pakHandler.loadedPaks();
- Map<String, GameFile> gameFiles = Main.pakHandler.getLoadedGameFilesWithNames();
- String mountPrefix = Static.mountPrefix;
- try {
- BufferedImage icon = ItemDefinitionToBufferedImage.getAsShopImage(itemDefinition, pakFileReaderIndices, loadedPaks, gameFiles, mountPrefix, Main.infoProvider.getFontInputStream(), Resources.getVBucksIcon(), price);
- if(icon != null) {
- return icon;
- }
- } catch (ReadException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public static BufferedImage loadDisplayAssetPath(String displayAssetPath) {
- if (displayAssetPath != null) {
- String path = Package.toGameSpecificName(displayAssetPath, "FortniteGame/");
- GameFile displayAssetFile = Main.pakHandler.loadGameFileByName(path);
- if (displayAssetFile != null) {
- Package displayAsset = Main.pakHandler.loadGameFile(displayAssetFile);
- String detailsTexturePath = null;
- for (Object export : displayAsset.getExports()) {
- if (export instanceof DisplayAssetPath) {
- DisplayAssetPath dap = (DisplayAssetPath) export;
- detailsTexturePath = dap.getDetailsImage();
- }
- }
- if (detailsTexturePath != null) {
- detailsTexturePath = Package.toGameSpecificName(detailsTexturePath, "FortniteGame/");
- GameFile textureFile = Main.pakHandler.loadGameFileByName(detailsTexturePath);
- Package texture = Main.pakHandler.loadGameFile(textureFile);
- for (Object tExport : texture.getExports()) {
- if (tExport instanceof UTexture2D) {
- UTexture2D textureObject = (UTexture2D) tExport;
- try {
- BufferedImage image = Texture2DToBufferedImage.readTexture(textureObject);
- if (image != null) {
- return image;
- }
- } catch (ReadException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- } else {
- Log.consoleLog(Log.WARN, "Couldn't load package " + path);
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement