DBSaveTask.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using MongoDB.Driver;
  3. namespace ETModel
  4. {
  5. [ObjectSystem]
  6. public class DbSaveTaskAwakeSystem : AwakeSystem<DBSaveTask, ComponentWithId, string, ETTaskCompletionSource>
  7. {
  8. public override void Awake(DBSaveTask self, ComponentWithId component, string collectionName, ETTaskCompletionSource tcs)
  9. {
  10. self.Component = component;
  11. self.CollectionName = collectionName;
  12. self.Tcs = tcs;
  13. }
  14. }
  15. public sealed class DBSaveTask : DBTask
  16. {
  17. public ComponentWithId Component;
  18. public string CollectionName { get; set; }
  19. public ETTaskCompletionSource Tcs;
  20. public override async ETTask Run()
  21. {
  22. DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
  23. try
  24. {
  25. // 执行保存数据库任务
  26. await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Component.Id, this.Component, new UpdateOptions {IsUpsert = true});
  27. this.Tcs.SetResult();
  28. }
  29. catch (Exception e)
  30. {
  31. this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {Id}", e));
  32. }
  33. }
  34. }
  35. }