Advertisement
FunGamesLeaks

shop parser

May 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.86 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package shop;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Font;
  8. import java.awt.FontFormatException;
  9. import java.awt.FontMetrics;
  10. import java.awt.Graphics2D;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20.  
  21. import javax.imageio.ImageIO;
  22.  
  23. import UE4_Packages.AthenaItemDefinition;
  24. import UE4_Packages.DisplayAssetPath;
  25. import UE4_Packages.FortHeroType;
  26. import UE4_Packages.FortWeaponMeleeItemDefinition;
  27. import UE4_Packages.Package;
  28. import UE4_Packages.ReadException;
  29. import UE4_Packages.UTexture2D;
  30.  
  31. import org.json.simple.JSONArray;
  32. import org.json.simple.JSONObject;
  33. import org.json.simple.parser.JSONParser;
  34. import org.json.simple.parser.ParseException;
  35.  
  36. import Converters.ItemDefinitionToBufferedImage;
  37. import Converters.Texture2DToBufferedImage;
  38. import Enums.RarityEnum;
  39. import UE4.GameFile;
  40. import UE4.PakFileReader;
  41. import core.Main;
  42. import logger.Log;
  43. import res.Resources;
  44. import util.Static;
  45.  
  46. /**
  47.  * @author FunGames
  48.  *
  49.  */
  50. public class ShopParser {
  51.  
  52.     public static BufferedImage vbucks = Resources.getVBucksIcon();
  53.  
  54.     public static BufferedImage parseShop(String date, String shop) {
  55.         if (vbucks != null) {
  56.             BufferedImage weekly = null;
  57.             BufferedImage daily = null;
  58.             try {
  59.                 JSONObject store = (JSONObject) new JSONParser().parse(shop);
  60.                 JSONArray storefronts = (JSONArray) store.get("storefronts");
  61.                
  62.                 for (Object storefrontO : storefronts) {
  63.                     JSONObject storefront = (JSONObject) storefrontO;
  64.                     String storefrontName = (String) storefront.get("name");
  65.                     if (storefrontName.equals("BRWeeklyStorefront")) {
  66.                         List<BufferedImage> weeklyStore = parseStore((JSONArray) storefront.get("catalogEntries"));
  67.                         weekly = createShopImage(weeklyStore);
  68.                     }
  69.                     if (storefrontName.equals("BRDailyStorefront")) {
  70.                         List<BufferedImage> dailyStore = parseStore((JSONArray) storefront.get("catalogEntries"));
  71.                         daily = createShopImage(dailyStore);
  72.                     }
  73.                 }
  74.             } catch (ParseException e) {
  75.                 // TODO Auto-generated catch block
  76.                 e.printStackTrace();
  77.             } finally {
  78.                 if(weekly != null && daily != null) {
  79.                     BufferedImage result = createFinalImage(weekly, daily);
  80.                     return result;
  81.                 }
  82.             }
  83.         } else {
  84.             Log.consoleLog(Log.WARN, "Couldn't load Vbucks icon");
  85.         }
  86.         return null;
  87.     }
  88.    
  89.     public static BufferedImage createFinalImage(BufferedImage weekly, BufferedImage daily) {
  90.         Font font;
  91.         try {
  92.             font = Font.createFont(Font.TRUETYPE_FONT, Main.infoProvider.getFontInputStream());
  93.             font = font.deriveFont(Font.BOLD, 100);
  94.         } catch (FontFormatException | IOException e) {
  95.             // TODO Auto-generated catch block
  96.             font = new Font("SansSerif", Font.BOLD, 100);
  97.             e.printStackTrace();
  98.         }
  99.        
  100.         int maxSizeX = Math.max(weekly.getWidth(), daily.getWidth());
  101.        
  102.         int spaceAboveWeeklyStore = 150;
  103.         int spaceBetweenWeeklyAndDailyStore = 150;
  104.         int spaceAfterDailyStore = 100;
  105.        
  106.         int spaceLeft = 100;
  107.         int spaceRight = 100;
  108.        
  109.         int totalX = spaceLeft + maxSizeX + spaceRight;
  110.         int totalY = spaceAboveWeeklyStore + weekly.getHeight() + spaceBetweenWeeklyAndDailyStore + daily.getHeight() + spaceAfterDailyStore;
  111.         BufferedImage shop = new BufferedImage(totalX, totalY, BufferedImage.TYPE_INT_ARGB);
  112.         Graphics2D g2 = shop.createGraphics();
  113.        
  114.         g2.setFont(font);
  115.         g2.setColor(Color.WHITE);
  116.         g2.drawString("FEATURED STORE", spaceLeft + 10, 112);
  117.        
  118.         g2.drawImage(weekly, spaceLeft, spaceAboveWeeklyStore, null);
  119.        
  120.         g2.setFont(font);
  121.         g2.setColor(Color.WHITE);
  122.         g2.drawString("DAILY STORE", spaceLeft + 10, spaceAboveWeeklyStore + weekly.getHeight() + 112);
  123.        
  124.         g2.drawImage(daily, spaceLeft, spaceAboveWeeklyStore + weekly.getHeight() + spaceBetweenWeeklyAndDailyStore, null);
  125.        
  126.         return shop;
  127.        
  128.     }
  129.    
  130.     public static BufferedImage createShopImage(List<BufferedImage> shopIcons) {
  131.         int space = 30;
  132.         if(shopIcons.isEmpty()) {
  133.             return null;
  134.         }
  135.        
  136.         int x = shopIcons.get(0).getWidth();
  137.         int y = shopIcons.get(0).getHeight();
  138.        
  139.         BufferedImage result = new BufferedImage(shopIcons.size() * (x + space) - space, y, BufferedImage.TYPE_INT_ARGB);
  140.         Graphics2D g2 = result.createGraphics();
  141.        
  142.         int cX = 0;
  143.         for(BufferedImage shopIcon : shopIcons) {
  144.             g2.drawImage(shopIcon, cX, 0, null);
  145.             cX += shopIcon.getWidth() + space;
  146.         }
  147.        
  148.         return result;
  149.     }
  150.  
  151.     public static List<BufferedImage> parseStore(JSONArray catalogEntries) {
  152.         List<BufferedImage> shopImages = new ArrayList<>();
  153.  
  154.         for (Object catalogEntryO : catalogEntries) {
  155.             JSONObject catalogEntry = (JSONObject) catalogEntryO;
  156.  
  157.             // Parse price
  158.             long finalPrice = 0;
  159.             JSONArray prices = (JSONArray) catalogEntry.get("prices");
  160.  
  161.             for (Object pO : prices) {
  162.                 JSONObject priceO = (JSONObject) pO;
  163.                 finalPrice += (long) priceO.get("finalPrice");
  164.             }
  165.  
  166.             // Read Display Asset Path if available
  167.             String displayAssetPath = catalogEntry.containsKey("displayAssetPath")
  168.                     ? (String) catalogEntry.get("displayAssetPath")
  169.                     : null;
  170.  
  171.             BufferedImage displayAssetTexture = loadDisplayAssetPath(displayAssetPath);
  172.  
  173.             // Read granted items
  174.             List<String> grantedItems = new ArrayList<>();
  175.             JSONArray grantedItemsArray = (JSONArray) catalogEntry.get("itemGrants");
  176.             for (Object grantedItemO : grantedItemsArray) {
  177.                 JSONObject grantedItemObject = (JSONObject) grantedItemO;
  178.                 String granted = (String) grantedItemObject.get("templateId");
  179.                 String[] parts = granted.split(":");
  180.                 if (parts.length >= 2) {
  181.                     String folder = Main.pakHandler.getFolder(parts[0]);
  182.                     if (folder != null) {
  183.                         String path = folder + parts[1] + ".uasset";
  184.                         String gameSpecific = Package.toGameSpecificName(path, "FortniteGame/");
  185.                         String originalPath = Main.pakHandler.getFilePathIgnoreCase(gameSpecific);
  186.                         grantedItems.add(originalPath);
  187.                     }
  188.  
  189.                 }
  190.  
  191.             }
  192.             if(grantedItems.size() > 0) {
  193.                 BufferedImage featuredImage = null;
  194.                 try {
  195.                 AthenaItemDefinition featured = loadItemDefinition(grantedItems.get(0));
  196.                 featuredImage = makeFeaturedImage(featured, displayAssetTexture, (int) finalPrice);
  197.                
  198.                 Graphics2D g2 = featuredImage.createGraphics();
  199.                 int additionalDrawPosX = 0;
  200.                 int additionalDrawPosY = 0;
  201.                 for(int i=1; i<grantedItems.size(); i++) {
  202.                     AthenaItemDefinition additionalItemDef = loadItemDefinition(grantedItems.get(i));
  203.                     BufferedImage additionalIcon = ItemDefinitionToBufferedImage.getImageNoDesc(additionalItemDef, Main.pakHandler.pakFileReaderIndices(), Main.pakHandler.loadedPaks(), Main.pakHandler.getLoadedGameFilesWithNames(), Static.mountPrefix, Main.infoProvider.getFontInputStream());
  204.                     additionalIcon = ItemDefinitionToBufferedImage.scaleImageByFactor(additionalIcon, Static.STORE_ADDITIONAL_IMAGE_SCALE_FACTOR);
  205.                     g2.drawImage(additionalIcon, additionalDrawPosX, additionalDrawPosY, null);
  206.                     additionalDrawPosY+= additionalIcon.getHeight();
  207.                 }
  208.                 } catch (Exception e) {
  209.                     System.out.println("Failed to load (parts) of shop offer" + grantedItems.get(0));
  210.                 } finally {
  211.                     if(featuredImage != null) {
  212.                         shopImages.add(featuredImage);
  213.                     }
  214.                 }
  215.             }
  216.         }
  217.         return shopImages;
  218.     }
  219.    
  220.     public static BufferedImage makeFeaturedImage(AthenaItemDefinition itemDefinition, BufferedImage displayAssetPath, int price) {
  221.         try {
  222.             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);
  223.         } catch (IOException e) {
  224.             // TODO Auto-generated catch block
  225.             e.printStackTrace();
  226.         } catch (ReadException e) {
  227.             // TODO Auto-generated catch block
  228.             e.printStackTrace();
  229.         }
  230.         return null;
  231.     }
  232.  
  233.     public static AthenaItemDefinition loadItemDefinition(String path) {
  234.         GameFile file = Main.pakHandler.loadGameFileByName(path);
  235.         if (file != null) {
  236.             Package itemDefinitionPackage = Main.pakHandler.loadGameFile(file);
  237.             if (itemDefinitionPackage != null) {
  238.                 for (Object export : itemDefinitionPackage.getExports()) {
  239.                     if (export instanceof AthenaItemDefinition) {
  240.                         AthenaItemDefinition itemDef = (AthenaItemDefinition) export;
  241.                         return itemDef;
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.         return null;
  247.     }
  248.  
  249.     public static BufferedImage loadItemDefinitionAsFeaturedImage(AthenaItemDefinition itemDefinition, int price) {
  250.         Map<String, Integer> pakFileReaderIndices = Main.pakHandler.pakFileReaderIndices();
  251.         PakFileReader[] loadedPaks = Main.pakHandler.loadedPaks();
  252.         Map<String, GameFile> gameFiles = Main.pakHandler.getLoadedGameFilesWithNames();
  253.         String mountPrefix = Static.mountPrefix;
  254.         try {
  255.             BufferedImage icon = ItemDefinitionToBufferedImage.getAsShopImage(itemDefinition, pakFileReaderIndices, loadedPaks, gameFiles, mountPrefix, Main.infoProvider.getFontInputStream(), Resources.getVBucksIcon(), price);
  256.             if(icon != null) {
  257.                
  258.                 return icon;
  259.             }
  260.         } catch (ReadException | IOException e) {
  261.             // TODO Auto-generated catch block
  262.             e.printStackTrace();
  263.         }
  264.         return null;
  265.  
  266.     }
  267.  
  268.     public static BufferedImage loadDisplayAssetPath(String displayAssetPath) {
  269.         if (displayAssetPath != null) {
  270.             String path = Package.toGameSpecificName(displayAssetPath, "FortniteGame/");
  271.             GameFile displayAssetFile = Main.pakHandler.loadGameFileByName(path);
  272.             if (displayAssetFile != null) {
  273.                 Package displayAsset = Main.pakHandler.loadGameFile(displayAssetFile);
  274.                 String detailsTexturePath = null;
  275.                 for (Object export : displayAsset.getExports()) {
  276.                     if (export instanceof DisplayAssetPath) {
  277.                         DisplayAssetPath dap = (DisplayAssetPath) export;
  278.                         detailsTexturePath = dap.getDetailsImage();
  279.                     }
  280.  
  281.                 }
  282.                 if (detailsTexturePath != null) {
  283.                     detailsTexturePath = Package.toGameSpecificName(detailsTexturePath, "FortniteGame/");
  284.                     GameFile textureFile = Main.pakHandler.loadGameFileByName(detailsTexturePath);
  285.                     Package texture = Main.pakHandler.loadGameFile(textureFile);
  286.                     for (Object tExport : texture.getExports()) {
  287.                         if (tExport instanceof UTexture2D) {
  288.                             UTexture2D textureObject = (UTexture2D) tExport;
  289.                             try {
  290.                                 BufferedImage image = Texture2DToBufferedImage.readTexture(textureObject);
  291.                                 if (image != null) {
  292.                                     return image;
  293.                                 }
  294.                             } catch (ReadException | IOException e) {
  295.                                 // TODO Auto-generated catch block
  296.                                 e.printStackTrace();
  297.                             }
  298.                         }
  299.                     }
  300.                 }
  301.             } else {
  302.                 Log.consoleLog(Log.WARN, "Couldn't load package " + path);
  303.             }
  304.         }
  305.         return null;
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement