Rremove old load file by dir
This commit is contained in:
@@ -2,108 +2,74 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using BrewMonster;
|
||||
using BrewMonster.Scripts;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public class GShopLoader : MonoBehaviour
|
||||
{
|
||||
[Header("File Paths")]
|
||||
public string gshopDataPath = "gshop.data";
|
||||
public string gshop1DataPath = "gshop1.data";
|
||||
// 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()
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
bool result = await MoveShopDataToPersistentPath();
|
||||
if (!result)
|
||||
// Wait for AddressableManager to be initialized
|
||||
while (!AddressableManager.Instance.IsInitialized())
|
||||
{
|
||||
BMLogger.LogError($"ElementDataMan: Failed to move element file to persistent path");
|
||||
return;
|
||||
await UniTask.DelayFrame(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
LoadGShopData();
|
||||
}
|
||||
|
||||
public async UniTask<bool> MoveShopDataToPersistentPath()
|
||||
{
|
||||
var destinationPath = Path.Combine(UnityEngine.Application.persistentDataPath, gshopDataPath);
|
||||
var sourcePath = Path.Combine(UnityEngine.Application.streamingAssetsPath, gshopDataPath);
|
||||
|
||||
|
||||
if (!File.Exists(destinationPath))
|
||||
{
|
||||
UnityWebRequest request = UnityWebRequest.Get(sourcePath);
|
||||
await request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
BMLogger.LogError($"ElementDataMan: Failed to move element file to persistent path: {request.error}");
|
||||
return false;
|
||||
}
|
||||
File.WriteAllBytes(destinationPath, request.downloadHandler.data);
|
||||
}
|
||||
|
||||
BMLogger.Log($"ElementDataMan: Successfully moved element file to persistent path: {destinationPath}");
|
||||
|
||||
destinationPath = Path.Combine(UnityEngine.Application.persistentDataPath, gshop1DataPath);
|
||||
sourcePath = Path.Combine(UnityEngine.Application.streamingAssetsPath, gshop1DataPath);
|
||||
|
||||
if (!File.Exists(destinationPath))
|
||||
{
|
||||
var request = UnityWebRequest.Get(sourcePath);
|
||||
await request.SendWebRequest();
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
BMLogger.LogError($"ElementDataMan: Failed to move element file to persistent path: {request.error}");
|
||||
return false;
|
||||
}
|
||||
File.WriteAllBytes(destinationPath, request.downloadHandler.data);
|
||||
}
|
||||
|
||||
BMLogger.Log($"ElementDataMan: Successfully moved element file to persistent path: {destinationPath}");
|
||||
return true;
|
||||
await LoadGShopData();
|
||||
}
|
||||
|
||||
public void LoadGShopData()
|
||||
public async UniTask LoadGShopData()
|
||||
{
|
||||
Debug.Log("=== Loading GShop Data ===");
|
||||
|
||||
// Load primary shop
|
||||
if (LoadShopData(gshopDataPath, primaryShop))
|
||||
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 (LoadShopData(gshop1DataPath, secondaryShop))
|
||||
if (await LoadShopData(GSHOP1_ADDRESS, 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)
|
||||
private async UniTask<bool> LoadShopData(string filePath, GShopData shopData)
|
||||
{
|
||||
try
|
||||
{
|
||||
string fullPath = Path.Combine(Application.streamingAssetsPath, filePath);
|
||||
// Load from Addressables
|
||||
var textAsset = await AddressableManager.Instance.LoadTextAssetAsync(filePath);
|
||||
|
||||
if (!File.Exists(fullPath))
|
||||
if (textAsset == null)
|
||||
{
|
||||
Debug.LogError($"GShop file not found: {fullPath}");
|
||||
BMLogger.LogError($"GShopLoader: Failed to load {filePath} from Addressables. File not found or load failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
|
||||
using (BinaryReader reader = new BinaryReader(fs))
|
||||
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();
|
||||
@@ -133,7 +99,7 @@ public class GShopLoader : MonoBehaviour
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"Error loading GShop data from {filePath}: {e.Message}");
|
||||
BMLogger.LogError($"GShopLoader: Error loading GShop data from {filePath}: {e.Message}\nStackTrace: {e.StackTrace}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user