using System; using System.Collections.Generic; using System.IO; using BrewMonster; using BrewMonster.Scripts; using Cysharp.Threading.Tasks; using UnityEngine; public class GShopLoader : MonoBehaviour { // Addressables-only: do NOT load gshop/gshop1 from file system paths. // These must match the configured Addressables "Address" values in // `Assets/AddressableAssetsData/AssetGroups/configuration.asset`. private const string GSHOP_ADDRESS = "Assets/Addressable/gshop.txt"; private const string GSHOP1_ADDRESS = "Assets/Addressable/gshop1.txt"; [Header("Loaded Data")] public GShopData primaryShop = new GShopData(); public GShopData secondaryShop = new GShopData(); async void Start() { // Wait for AddressableManager to be initialized while (!AddressableManager.Instance.IsInitialized()) { await UniTask.DelayFrame(1); } await LoadGShopData(); } public async UniTask LoadGShopData() { Debug.Log("=== Loading GShop Data ==="); // Load primary shop if (await LoadShopData(GSHOP_ADDRESS, primaryShop)) { Debug.Log($"Primary shop loaded: {primaryShop.items.Count} items, {primaryShop.mainTypes.Count} categories"); //LogShopData("Primary Shop", primaryShop); } // Load secondary shop if (await LoadShopData(GSHOP1_ADDRESS, secondaryShop)) { Debug.Log($"Secondary shop loaded: {secondaryShop.items.Count} items, {secondaryShop.mainTypes.Count} categories"); //LogShopData("Secondary Shop", secondaryShop); } } private async UniTask LoadShopData(string filePath, GShopData shopData) { try { // Load from Addressables var textAsset = await AddressableManager.Instance.LoadTextAssetAsync(filePath); if (textAsset == null) { BMLogger.LogError($"GShopLoader: Failed to load {filePath} from Addressables. File not found or load failed."); return false; } if (textAsset.bytes == null || textAsset.bytes.Length == 0) { BMLogger.LogError($"GShopLoader: {filePath} loaded from Addressables but bytes array is null or empty."); return false; } // Read binary data from TextAsset.bytes using MemoryStream using (MemoryStream ms = new MemoryStream(textAsset.bytes)) using (BinaryReader reader = new BinaryReader(ms)) { // Read timestamp shopData.timestamp = reader.ReadUInt32(); Debug.Log($"Timestamp: 0x{shopData.timestamp:X8}"); // Read number of items int itemCount = reader.ReadInt32(); Debug.Log($"Item count: {itemCount}"); // Read all items for (int i = 0; i < itemCount; i++) { GShopItem item = ReadGShopItem(reader); item.itemIndex = i; shopData.items.Add(item); } // Read 8 main types for (int i = 0; i < 8; i++) { GShopMainType mainType = ReadGShopMainType(reader); shopData.mainTypes.Add(mainType); } } return true; } catch (Exception e) { BMLogger.LogError($"GShopLoader: Error loading GShop data from {filePath}: {e.Message}\nStackTrace: {e.StackTrace}"); return false; } } private GShopItem ReadGShopItem(BinaryReader reader) { GShopItem item = new GShopItem(); item.localId = reader.ReadInt32(); item.mainType = reader.ReadInt32(); item.subType = reader.ReadInt32(); // Read icon path (128 chars) item.icon = reader.ReadString(128); item.id = reader.ReadUInt32(); item.num = reader.ReadUInt32(); // Read buy options (4 options) item.buy = new GShopBuyOption[4]; for (int i = 0; i < 4; i++) { item.buy[i] = new GShopBuyOption { price = reader.ReadUInt32(), endTime = reader.ReadUInt32(), time = reader.ReadUInt32(), startTime = reader.ReadUInt32(), type = reader.ReadInt32(), day = reader.ReadUInt32(), status = reader.ReadUInt32(), flag = reader.ReadUInt32() }; } // Read description (512 wide chars) item.desc = reader.ReadWideString(512); // Read name (32 wide chars) item.name = reader.ReadWideString(32); item.idGift = reader.ReadUInt32(); item.giftNum = reader.ReadUInt32(); item.giftTime = reader.ReadUInt32(); item.logPrice = reader.ReadUInt32(); // Read owner NPCs (8 max) item.ownerNpcs = new uint[8]; for (int i = 0; i < 8; i++) { item.ownerNpcs[i] = reader.ReadUInt32(); } return item; } private GShopMainType ReadGShopMainType(BinaryReader reader) { GShopMainType mainType = new GShopMainType(); // Read main type name (64 wide chars) mainType.name = reader.ReadWideString(64); // Read number of sub-types int subTypeCount = reader.ReadInt32(); mainType.subTypes = new List(); // Read sub-types for (int i = 0; i < subTypeCount; i++) { string subTypeName = reader.ReadWideString(64); mainType.subTypes.Add(subTypeName); } return mainType; } private void LogShopData(string shopName, GShopData shopData) { Debug.Log($"=== {shopName} Information ==="); Debug.Log($"Timestamp: 0x{shopData.timestamp:X8}"); Debug.Log($"Total Items: {shopData.items.Count}"); Debug.Log($"Total Categories: {shopData.mainTypes.Count}"); // Log categories Debug.Log("--- Categories ---"); for (int i = 0; i < shopData.mainTypes.Count; i++) { var category = shopData.mainTypes[i]; Debug.Log($"Category {i}: {category.name}"); foreach (var subType in category.subTypes) { Debug.Log($" - {subType}"); } } // Log first 10 items as sample Debug.Log("--- Sample Items (First 10) ---"); int sampleCount = Mathf.Min(10, shopData.items.Count); for (int i = 0; i < sampleCount; i++) { var item = shopData.items[i]; Debug.Log($"Item {i}:"); Debug.Log($" ID: {item.id}, Name: {item.name}"); Debug.Log($" MainType: {item.mainType}, SubType: {item.subType}"); Debug.Log($" Quantity: {item.num}"); Debug.Log($" Icon: {item.icon}"); Debug.Log($" Description: {item.desc}"); Debug.Log($" Local ID: {item.localId}"); // Log buy options for (int j = 0; j < 4; j++) { var buyOption = item.buy[j]; if (buyOption.price > 0) { Debug.Log($" Buy Option {j}: Price={buyOption.price}, Status={buyOption.status}"); } } if (item.idGift > 0) { Debug.Log($" Gift: ID={item.idGift}, Num={item.giftNum}"); } } Debug.Log($"=== End {shopName} Information ==="); } }