data_compiler.vala 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class DataCompiler
  8. {
  9. private RuntimeInstance _runtime;
  10. private Guid _id;
  11. private bool _success;
  12. private SourceFunc _compile_callback;
  13. private SourceFunc _refresh_list_callback;
  14. private Gee.ArrayList<Value?> _refresh_list;
  15. public uint _revision;
  16. public signal void start();
  17. public signal void finished(bool success);
  18. public void message(Hashtable msg)
  19. {
  20. // Guid id = Guid.parse((string)msg["id"]);
  21. if (msg.has_key("start")) {
  22. start();
  23. } else if (msg.has_key("success")) {
  24. compile_finished((bool)msg["success"], (uint)(double)msg["revision"]);
  25. }
  26. }
  27. public DataCompiler(RuntimeInstance runtime)
  28. {
  29. _runtime = runtime;
  30. _id = GUID_ZERO;
  31. _success = false;
  32. _revision = 0;
  33. _compile_callback = null;
  34. _refresh_list_callback = null;
  35. _refresh_list = null;
  36. }
  37. // Returns true if success, false otherwise.
  38. public async bool compile(string data_dir, string platform)
  39. {
  40. if (_compile_callback != null)
  41. return false;
  42. _id = Guid.new_guid();
  43. _success = false;
  44. _runtime.send(DataCompilerApi.compile(_id, data_dir, platform));
  45. _compile_callback = compile.callback;
  46. yield;
  47. finished(_success);
  48. return _success;
  49. }
  50. public void compile_finished(bool success, uint revision)
  51. {
  52. _success = success;
  53. _revision = revision;
  54. if (_compile_callback != null)
  55. _compile_callback();
  56. _compile_callback = null;
  57. }
  58. /// Returns the list of resources that have changed since @a since_revision.
  59. public async Gee.ArrayList<Value?> refresh_list(uint since_revision)
  60. {
  61. if (_refresh_list_callback != null)
  62. return new Gee.ArrayList<Value?>();
  63. _runtime.send(DataCompilerApi.refresh_list(since_revision));
  64. _refresh_list_callback = refresh_list.callback;
  65. yield;
  66. return _refresh_list;
  67. }
  68. public void refresh_list_finished(Gee.ArrayList<Value?> resources)
  69. {
  70. unowned GLib.SourceFunc callback = _refresh_list_callback;
  71. _refresh_list_callback = null;
  72. _refresh_list = resources;
  73. if (callback != null)
  74. callback();
  75. }
  76. }
  77. } /* namespace Crown */