Files
2025-10-14 10:53:16 +07:00

210 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class GShopLoader : MonoBehaviour
{
[Header("File Paths")]
public string gshopDataPath = "gshop.data";
public string gshop1DataPath = "gshop1.data";
[Header("Loaded Data")]
public GShopData primaryShop = new GShopData();
public GShopData secondaryShop = new GShopData();
void Start()
{
LoadGShopData();
}
public void LoadGShopData()
{
Debug.Log("=== Loading GShop Data ===");
// Load primary shop
if (LoadShopData(gshopDataPath, primaryShop))
{
Debug.Log($"Primary shop loaded: {primaryShop.items.Count} items, {primaryShop.mainTypes.Count} categories");
LogShopData("Primary Shop", primaryShop);
}
// Load secondary shop
if (LoadShopData(gshop1DataPath, secondaryShop))
{
Debug.Log($"Secondary shop loaded: {secondaryShop.items.Count} items, {secondaryShop.mainTypes.Count} categories");
LogShopData("Secondary Shop", secondaryShop);
}
}
private bool LoadShopData(string filePath, GShopData shopData)
{
try
{
string fullPath = Path.Combine(Application.streamingAssetsPath, filePath);
if (!File.Exists(fullPath))
{
Debug.LogError($"GShop file not found: {fullPath}");
return false;
}
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
using (BinaryReader reader = new BinaryReader(fs))
{
// 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);
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)
{
Debug.LogError($"Error loading GShop data from {filePath}: {e.Message}");
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<string>();
// 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}");
// 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 ===");
}
}