import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class SaveGameReader {

    public static void main(String[] args) {
        String saveGameFolderPath = "/storage/emulated/0/Android/data/com.tencent.tmgp.pubgmhd/files/UE4Game/ShadowTrackerExtra/ShadowTrackerExtra/Saved/SaveGames";
      
        try {
            Path savePath = Path.of(saveGameFolderPath);
            
            if (!Files.exists(savePath)) {
                System.out.println("Save folder not exist: " + saveGameFolderPath);
                return;
            }
            
            if (Files.isDirectory(savePath)) {
                List<Path> saveFiles = readSaveFiles(savePath);
                System.out.println("Found " + saveFiles.size() + " save files:");
                
                for (Path file : saveFiles) {
                    System.out.println("Reading file: " + file.getFileName());
                    readSaveFile(file);
                    System.out.println("----------------------------");
                }
            } else {
                readSaveFile(savePath);
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private static List<Path> readSaveFiles(Path folderPath) throws IOException {
        List<Path> saveFiles = new ArrayList<>();
        
        Files.walk(folderPath, 1)
            .filter(Files::isRegularFile)
            .forEach(saveFiles::add);
            
        return saveFiles;
    }
    
    private static void readSaveFile(Path filePath) {
        try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(filePath, StandardOpenOption.READ)) {
            
            long fileSize = fileChannel.size();
            System.out.println("File size: " + fileSize + " bytes");
            
            ByteBuffer headerBuffer = ByteBuffer.allocate(128);
            fileChannel.read(headerBuffer);
            headerBuffer.flip();
            
            System.out.println("First 128 bytes (hex):");
            byte[] headerBytes = new byte[128];
            headerBuffer.get(headerBytes);
            
            for (int i = 0; i < headerBytes.length; i++) {
                System.out.printf("%02X ", headerBytes[i]);
                if ((i + 1) % 16 == 0) {
                    System.out.println();
                }
            }
            System.out.println();
            
            System.out.println("First 128 bytes (text):");
            String headerText = new String(headerBytes, "UTF-8");
            for (char c : headerText.toCharArray()) {
                if (Character.isLetterOrDigit(c) || Character.isWhitespace(c) || ".,;:-_()[]{}".indexOf(c) >= 0) {
                    System.out.print(c);
                } else {
                    System.out.print(".");
                }
            }
            System.out.println();
            
        } catch (IOException e) {
            System.err.println("Read file failed: " + filePath.getFileName());
            e.printStackTrace();
        }
    }
    
    private static void readEntireSaveFile(Path filePath) {
        try {
            byte[] fileContent = Files.readAllBytes(filePath);
            
            System.out.println("File: " + filePath.getFileName());
            System.out.println("Size: " + fileContent.length + " bytes");
            
            if (fileContent.length > 0) {
                System.out.println("First 256 bytes (hex):");
                int bytesToShow = Math.min(fileContent.length, 256);
                for (int i = 0; i < bytesToShow; i++) {
                    System.out.printf("%02X ", fileContent[i]);
                    if ((i + 1) % 16 == 0) {
                        System.out.println();
                    }
                }
                System.out.println();
            }
            
        } catch (IOException e) {
            System.err.println("Read file failed: " + filePath.getFileName());
            e.printStackTrace();
        }
    }
}