using System; using System.Threading.Tasks; using MongoDB.Driver; namespace ETModel { [ObjectSystem] public class DbSaveTaskAwakeSystem : AwakeSystem> { public override void Awake(DBSaveTask self, Component entity, string collectionName, TaskCompletionSource tcs) { self.Component = entity; self.CollectionName = collectionName; self.Tcs = tcs; } } public sealed class DBSaveTask : DBTask { public Component Component; public string CollectionName { get; set; } public TaskCompletionSource Tcs; public override async Task Run() { DBComponent dbComponent = Game.Scene.GetComponent(); try { // 执行保存数据库任务 await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Component.Id, this.Component, new UpdateOptions {IsUpsert = true}); this.Tcs.SetResult(true); } catch (Exception e) { this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {Id}", e)); } } } }