using System; using System.Collections.Generic; using System.Threading.Tasks; using MongoDB.Driver; namespace ETModel { [ObjectSystem] public class DBQueryJsonTaskAwakeSystem : AwakeSystem>> { public override void Awake(DBQueryJsonTask self, string collectionName, string json, ETTaskCompletionSource> tcs) { self.CollectionName = collectionName; self.Json = json; self.Tcs = tcs; } } public sealed class DBQueryJsonTask : DBTask { public string CollectionName { get; set; } public string Json { get; set; } public ETTaskCompletionSource> Tcs { get; set; } public override async ETTask Run() { DBComponent dbComponent = Game.Scene.GetComponent(); try { // 执行查询数据库任务 FilterDefinition filterDefinition = new JsonFilterDefinition(this.Json); IAsyncCursor cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition); List components = await cursor.ToListAsync(); this.Tcs.SetResult(components); } catch (Exception e) { this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e)); } } } }