World.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Solana.Unity.Programs;
  7. using Solana.Unity.Wallet;
  8. using Solana.Unity.Rpc.Models;
  9. using GplSession.Program;
  10. namespace World
  11. {
  12. namespace Program
  13. {
  14. public partial class WorldProgram
  15. {
  16. public static Solana.Unity.Rpc.Models.TransactionInstruction AddEntity(AddEntityAccounts accounts, PublicKey programId = null)
  17. {
  18. programId ??= new(ID);
  19. return AddEntity(accounts, (byte[]) null, programId);
  20. }
  21. public static Solana.Unity.Rpc.Models.TransactionInstruction AddEntity(AddEntityAccounts accounts, string extraSeed, PublicKey programId = null)
  22. {
  23. programId ??= new(ID);
  24. return AddEntity(accounts, System.Text.Encoding.UTF8.GetBytes(extraSeed), programId);
  25. }
  26. public static PublicKey FindSessionTokenPda(PublicKey sessionSigner, PublicKey authority)
  27. {
  28. PublicKey.TryFindProgramAddress(new[]
  29. {
  30. Encoding.UTF8.GetBytes("session_token"),
  31. new PublicKey(WorldProgram.ID).KeyBytes,
  32. sessionSigner.KeyBytes,
  33. authority.KeyBytes
  34. }, new PublicKey(GplSessionProgram.ID), out var pda, out _);
  35. return pda;
  36. }
  37. public static PublicKey FindRegistryPda()
  38. {
  39. PublicKey.TryFindProgramAddress(new[]
  40. {
  41. Encoding.UTF8.GetBytes("registry"),
  42. }, new PublicKey(ID), out var pda, out _);
  43. return pda;
  44. }
  45. public static PublicKey FindWorldPda(UInt64 worldId)
  46. {
  47. PublicKey.TryFindProgramAddress(new[]
  48. {
  49. Encoding.UTF8.GetBytes("world"),
  50. BitConverter.GetBytes(worldId).Reverse().ToArray()
  51. }, new PublicKey(ID), out var pda, out _);
  52. return pda;
  53. }
  54. public static PublicKey FindEntityPda(UInt64 worldId, UInt64 entityId)
  55. {
  56. PublicKey.TryFindProgramAddress(new[]
  57. {
  58. Encoding.UTF8.GetBytes("entity"),
  59. BitConverter.GetBytes(worldId).Reverse().ToArray(),
  60. BitConverter.GetBytes(entityId).Reverse().ToArray()
  61. }, new PublicKey(ID), out var pda, out _);
  62. return pda;
  63. }
  64. public static PublicKey FindEntityPda(UInt64 worldId, byte[] seed)
  65. {
  66. var ZeroArray = new byte[8];
  67. Array.Fill(ZeroArray, (byte) 0);
  68. PublicKey.TryFindProgramAddress(new[]
  69. {
  70. Encoding.UTF8.GetBytes("entity"),
  71. BitConverter.GetBytes(worldId).Reverse().ToArray(),
  72. ZeroArray,
  73. seed
  74. }, new PublicKey(ID), out var pda, out _);
  75. return pda;
  76. }
  77. public static PublicKey FindEntityPda(UInt64 worldId, string seed)
  78. {
  79. return FindEntityPda(worldId, System.Text.Encoding.UTF8.GetBytes(seed));
  80. }
  81. public static PublicKey FindComponentProgramDataPda(PublicKey componentProgramId)
  82. {
  83. PublicKey.TryFindProgramAddress(new[]
  84. {
  85. componentProgramId.KeyBytes,
  86. }, new PublicKey("BPFLoaderUpgradeab1e11111111111111111111111"), out var pda, out _);
  87. return pda;
  88. }
  89. public static PublicKey FindComponentPda(
  90. PublicKey componentProgramId,
  91. PublicKey entity
  92. ) {
  93. return FindComponentPda(componentProgramId, entity, "");
  94. }
  95. public static PublicKey FindComponentPda(
  96. PublicKey componentProgramId,
  97. PublicKey entity,
  98. string seed
  99. ) {
  100. return FindComponentPda(componentProgramId, entity, System.Text.Encoding.UTF8.GetBytes(seed));
  101. }
  102. public static PublicKey FindComponentPda(
  103. PublicKey componentProgramId,
  104. PublicKey entity,
  105. byte[] seed)
  106. {
  107. PublicKey.TryFindProgramAddress(new[]
  108. {
  109. seed, entity.KeyBytes
  110. }, componentProgramId, out var pda, out _);
  111. return pda;
  112. }
  113. /// <summary>
  114. /// Convenience bundle for defining an entity and the associated components.
  115. /// </summary>
  116. [Obsolete("Use Bolt.World.EntityType instead.")]
  117. public class EntityType{
  118. public PublicKey[] Components { get; set; }
  119. public string[] Seeds { get; set; }
  120. public PublicKey Entity { get; set; }
  121. public EntityType(PublicKey entity, PublicKey[] componentsIds)
  122. {
  123. Components = componentsIds;
  124. Seeds = new string[Components.Length];
  125. Entity = entity;
  126. Array.Fill(Seeds, "");
  127. }
  128. public EntityType(PublicKey entity, PublicKey[] componentsIds, string[] seeds)
  129. {
  130. Components = componentsIds;
  131. Seeds = seeds;
  132. Entity = entity;
  133. if (Seeds.Length != Components.Length)
  134. {
  135. throw new ArgumentException("Seeds must be the same length as components");
  136. }
  137. }
  138. public int ComponentsLength()
  139. {
  140. return Components.Length;
  141. }
  142. public PublicKey[] GetComponentsIds()
  143. {
  144. return Components;
  145. }
  146. public PublicKey[] GetComponentsPdas()
  147. {
  148. PublicKey[] pdas = new PublicKey[Components.Length];
  149. for (int i = 0; i < Components.Length; i++)
  150. {
  151. pdas[i] = FindComponentPda(Components[i], Entity, Seeds[i]);
  152. }
  153. return pdas;
  154. }
  155. }
  156. [Obsolete("Use Bolt.World.ApplySystem instead.")]
  157. public static Solana.Unity.Rpc.Models.TransactionInstruction ApplySystem(
  158. PublicKey world,
  159. PublicKey system,
  160. EntityType[] systemInput,
  161. byte[] args,
  162. PublicKey authority,
  163. PublicKey sessionToken = null,
  164. PublicKey programId = null)
  165. {
  166. programId ??= new(WorldProgram.ID);
  167. List<PublicKey> componentIds = new List<PublicKey>();
  168. List<PublicKey> componentPdas = new List<PublicKey>();
  169. foreach (var entity in systemInput)
  170. {
  171. componentIds.AddRange(entity.GetComponentsIds());
  172. componentPdas.AddRange(entity.GetComponentsPdas());
  173. }
  174. if (componentIds.Count != componentPdas.Count)
  175. {
  176. throw new ArgumentException("Component IDs and PDAs must be the same length");
  177. }
  178. Solana.Unity.Rpc.Models.TransactionInstruction instruction;
  179. if (sessionToken != null) {
  180. var apply = new ApplyWithSessionAccounts() {
  181. BoltSystem = system,
  182. Authority = authority,
  183. World = world,
  184. SessionToken = sessionToken,
  185. };
  186. instruction = ApplyWithSession(apply, args, programId);
  187. } else {
  188. var apply = new ApplyAccounts() {
  189. BoltSystem = system,
  190. Authority = authority,
  191. World = world,
  192. };
  193. instruction = Apply(apply, args, programId);
  194. }
  195. for (int i = 0; i < componentIds.Count; i++) {
  196. instruction.Keys.Add(AccountMeta.ReadOnly(componentIds[i], false));
  197. instruction.Keys.Add(AccountMeta.Writable(componentPdas[i], false));
  198. }
  199. if (componentIds.Count > 0) {
  200. // program id delimits the end of the component list
  201. instruction.Keys.Add(AccountMeta.ReadOnly(new PublicKey(WorldProgram.ID), false));
  202. }
  203. return instruction;
  204. }
  205. }
  206. }
  207. }