RustDedicated/Rust.FileSystem/FileSystemBackend.cs
2025-08-09 20:48:06 +09:30

92 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public abstract class FileSystemBackend
{
public bool isError;
public string loadingError = "";
public Dictionary<string, UnityEngine.Object> cache = new Dictionary<string, UnityEngine.Object>(StringComparer.OrdinalIgnoreCase);
public GameObject[] LoadPrefabs(string folder)
{
if (!folder.EndsWith("/", StringComparison.CurrentCultureIgnoreCase))
{
Debug.LogWarning("LoadPrefabs - folder should end in '/' - " + folder);
}
if (!folder.StartsWith("assets/", StringComparison.CurrentCultureIgnoreCase))
{
Debug.LogWarning("LoadPrefabs - should start with assets/ - " + folder);
}
return LoadAll<GameObject>(folder, ".prefab");
}
public GameObject LoadPrefab(string filePath)
{
if (!filePath.StartsWith("assets/", StringComparison.CurrentCultureIgnoreCase))
{
Debug.LogWarning("LoadPrefab - should start with assets/ - " + filePath);
}
return Load<GameObject>(filePath);
}
public string[] FindAll(string folder, string search = "")
{
return LoadAssetList(folder, search);
}
public T[] LoadAll<T>(string folder, string search = "") where T : UnityEngine.Object
{
List<T> list = new List<T>();
string[] array = FindAll(folder, search);
foreach (string filePath in array)
{
T val = Load<T>(filePath);
if (val != null)
{
list.Add(val);
}
}
return list.ToArray();
}
public T Load<T>(string filePath) where T : UnityEngine.Object
{
T val = null;
if (cache.ContainsKey(filePath))
{
val = cache[filePath] as T;
}
else
{
val = LoadAsset<T>(filePath);
if (val != null)
{
cache.Add(filePath, val);
}
}
return val;
}
protected void LoadError(string err)
{
Debug.LogError(err);
loadingError = err;
isError = true;
}
public virtual List<string> UnloadBundles(string partialName)
{
return new List<string>(0);
}
protected abstract T LoadAsset<T>(string filePath) where T : UnityEngine.Object;
protected abstract string[] LoadAssetList(string folder, string search);
public abstract T[] LoadAllFromBundle<T>(string bundleName, string editorSearch) where T : UnityEngine.Object;
public abstract bool HasAsset(string path);
}