use Stream instead of FileStream

This commit is contained in:
Le Duc Anh
2025-12-25 10:50:39 +07:00
parent 9dc74c7346
commit c0541f562d
6 changed files with 39 additions and 32 deletions
@@ -5,7 +5,6 @@ using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
namespace BrewMonster.Scripts
{
@@ -67,7 +67,7 @@ public class AAssit
/// re-seeked before reading the count and each element.</param>
/// <typeparam name="T">Element type to deserialize. Must be blittable/marshallable via <see cref="Marshal.PtrToStructure(System.IntPtr,System.Type)"/>.</typeparam>
/// <returns>The populated array when <c>count</c> > 0; otherwise null.</returns>
public static T[] ReadArrayPointerFromBinary<T>(FileStream stream, ref long readBytes, long fileOffset = -1)
public static T[] ReadArrayPointerFromBinary<T>(Stream stream, ref long readBytes, long fileOffset = -1)
{
// seek to the fileOffset if it's >= 0
if (fileOffset >= 0)
@@ -112,7 +112,7 @@ public class AAssit
return buffer;
}
public static bool GetBoolFromFileStream(FileStream fs, ref long readBytes)
public static bool GetBoolFromFileStream(Stream fs, ref long readBytes)
{
byte[] buffer = new byte[1];
int bytesRead = fs.Read(buffer, 0, 1);
@@ -123,7 +123,7 @@ public class AAssit
return buffer[0] != 0;
}
public static int GetIntFromFileStream(FileStream fs, ref long readBytes)
public static int GetIntFromFileStream(Stream fs, ref long readBytes)
{
byte[] buffer = new byte[4];
int bytesRead = fs.Read(buffer, 0, 4);
@@ -134,7 +134,7 @@ public class AAssit
return BitConverter.ToInt32(buffer, 0);
}
public static uint GetUIntFromFileStream(FileStream fs, ref long readBytes)
public static uint GetUIntFromFileStream(Stream fs, ref long readBytes)
{
byte[] buffer = new byte[4];
int bytesRead = fs.Read(buffer, 0, 4);
@@ -145,7 +145,7 @@ public class AAssit
return BitConverter.ToUInt32(buffer, 0);
}
public static long GetLongFromFileStream(FileStream fs, ref long readBytes)
public static long GetLongFromFileStream(Stream fs, ref long readBytes)
{
byte[] buffer = new byte[8];
int bytesRead = fs.Read(buffer, 0, 8);
@@ -1,4 +1,6 @@
using System;
using BrewMonster.Scripts;
using Cysharp.Threading.Tasks;
using ModelRenderer.Scripts.GameData;
using UnityEngine;
@@ -15,12 +17,21 @@ namespace BrewMonster
}
public async void Initialize()
{
await InitializeInternal();
}
public async UniTask InitializeInternal()
{
_elementDataMan = new();
_instance = this;
try
{
while (!AddressableManager.Instance.IsInitialized())
{
await UniTask.DelayFrame(1);
}
var result = await _elementDataMan.load_data();
if (result == -1)
{
@@ -1,4 +1,6 @@
using BrewMonster;
using BrewMonster.Scripts;
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -300,26 +302,28 @@ namespace ModelRenderer.Scripts.GameData
return true;
}
public async Task<int> load_data(string pathname = "")
public async UniTask<int> load_data(string pathname = "")
{
#if UNITY_ANDROID && !UNITY_EDITOR
var success = await MoveElementFileToPersistentPath();
if (!success)
{
BMLogger.LogError("ElementDataMan: Failed to move element file to persistent path");
return -1;
}
// #if UNITY_ANDROID && !UNITY_EDITOR
// var success = await MoveElementFileToPersistentPath();
// if (!success)
// {
// BMLogger.LogError("ElementDataMan: Failed to move element file to persistent path");
// return -1;
// }
pathname = Path.Combine(UnityEngine.Application.persistentDataPath, "elements.data");
#endif
if (string.IsNullOrEmpty(pathname))
{
pathname = Path.Combine(UnityEngine.Application.streamingAssetsPath, "elements.data");
}
// pathname = Path.Combine(UnityEngine.Application.persistentDataPath, "elements.data");
// #endif
// if (string.IsNullOrEmpty(pathname))
// {
// pathname = Path.Combine(UnityEngine.Application.streamingAssetsPath, "elements.data");
// }
if (!File.Exists(pathname)) return -1;
// if (!File.Exists(pathname)) return -1;
using (var file = new FileStream(pathname, FileMode.Open, FileAccess.Read))
var dataFile = await AddressableManager.Instance.LoadTextAssetAsync("elements.data");
using (var file = new MemoryStream(dataFile.bytes))
{
long dwRead = 0;
int version = AAssit.GetIntFromFileStream(file, ref dwRead);
@@ -3924,8 +3924,8 @@ namespace BrewMonster
public int num_option; // number of options
public option[] options; // options pointer
public void Read(FileStream file)
public void Read(Stream file)
{
long dwRead = file.Position;
id = AAssit.GetIntFromFileStream(file, ref dwRead);
@@ -3943,7 +3943,7 @@ namespace BrewMonster
public window[] windows; // windows pointer
public void Read(FileStream file)
public void Read(Stream file)
{
long dwRead = file.Position;
id_talk = AAssit.GetUIntFromFileStream(file, ref dwRead);
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: d0c06c588e2a6442488a3542551fb243
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: