improve: 整体健壮性

This commit is contained in:
2026-04-09 17:37:03 +08:00
parent 156285a691
commit 66c63896b0
4 changed files with 23 additions and 17 deletions
@@ -31,15 +31,12 @@ namespace OCES.Haptic
{
if (bytes == null)
return false;
using (MemoryStream memoryStream = new(bytes))
using MemoryStream memoryStream = new(bytes);
using (BinaryReader br = new(memoryStream))
{
using (var br = new BinaryReader(memoryStream))
{
data.DeSerialize(br);
br.Close();
}
memoryStream.Close();
data.DeSerialize(br);
}
memoryStream.Close();
return true;
}
}
@@ -20,6 +20,12 @@ namespace OCES.Haptic
public void Play(uint hapticId, bool isDirectCall = true)
{
if (this.m_hapticObjects is null)
{
Debug.LogError($"[Haptic System] Config not loaded!");
return;
}
HapticObject hapticObject = this.m_hapticObjects.QueryById(hapticId);
if (hapticObject != null)
{
@@ -52,7 +58,7 @@ namespace OCES.Haptic
}
break;
case HapticType.Emphasis:
if (hapticObject.Amplitude < 0f || hapticObject.Frequency < 0f)
if (hapticObject.Amplitude <= 0f || hapticObject.Frequency <= 0f)
{
Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude or frequency." +
"Please check the datatable.");
@@ -61,7 +67,7 @@ namespace OCES.Haptic
HapticPatterns.PlayEmphasis(hapticObject.Amplitude, hapticObject.Frequency);
break;
case HapticType.Constant:
if (hapticObject.Amplitude < 0f || hapticObject.Frequency < 0f || hapticObject.Duration < 0f)
if (hapticObject.Amplitude <= 0f || hapticObject.Frequency <= 0f || hapticObject.Duration <= 0f)
{
Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude, frequency or duration." +
"Please check the datatable.");
@@ -73,10 +79,10 @@ namespace OCES.Haptic
case HapticType.Advance:
if (Enum.TryParse(hapticObject.FallbackPreset, out HapticPatterns.PresetType fallbackPreset))
{
HapticController.fallbackPreset = fallbackPreset;
HapticClip hapticClip = GetHapticClip(hapticObject);
if (hapticClip)
{
HapticController.fallbackPreset = fallbackPreset;
HapticController.Stop();
HapticController.Play(hapticClip);
}
@@ -114,9 +120,15 @@ namespace OCES.Haptic
this.m_hapticObjects = HapticConfigLoader.Load<HapticObjectConfig>(k_hapticConfigPath + "HapticObject");
}
void OnDestroy()
{
if (Instance == this) Instance = null;
}
static HapticClip GetHapticClip(HapticObject hapticObject)
{
return Resources.Load<HapticClip>(k_hapticResourcesPath + hapticObject.Payload);
//TODO: cache HapticClips into a Dictionary<string, HapticClip> or using AsyncLoad
}
static class HapticConfigLoader
@@ -125,7 +137,11 @@ namespace OCES.Haptic
{
TextAsset bytes = Resources.Load<TextAsset>(tableName);
if (!bytes)
{
Debug.LogError($"未找到表 {tableName}");
return default;
}
IBinarySerializable data = new T();
bool readOk = FileManager.ReadBinaryDataFromBytes(bytes.bytes, ref data);
if (readOk)