AnimationSet.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenMetaverse;
  30. namespace OpenSim.Framework
  31. {
  32. // public delegate bool AnimationSetValidator(UUID animID);
  33. public delegate uint AnimationSetValidator(UUID animID);
  34. public class AnimationSet
  35. {
  36. private bool m_parseError = false;
  37. public const uint createBasePermitions = (uint)(PermissionMask.All); // no export ?
  38. public const uint createNextPermitions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  39. public const uint allowedBasePermitions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  40. public const uint allowedNextPermitions = 0;
  41. public static void setCreateItemPermitions(InventoryItemBase it)
  42. {
  43. if (it == null)
  44. return;
  45. it.BasePermissions = createBasePermitions;
  46. it.CurrentPermissions = createBasePermitions;
  47. // it.GroupPermissions &= allowedPermitions;
  48. it.NextPermissions = createNextPermitions;
  49. // it.EveryOnePermissions &= allowedPermitions;
  50. it.GroupPermissions = 0;
  51. it.EveryOnePermissions = 0;
  52. }
  53. public static void enforceItemPermitions(InventoryItemBase it, bool IsCreator)
  54. {
  55. if (it == null)
  56. return;
  57. uint bp;
  58. uint np;
  59. if (IsCreator)
  60. {
  61. bp = createBasePermitions;
  62. np = createNextPermitions;
  63. }
  64. else
  65. {
  66. bp = allowedBasePermitions;
  67. np = allowedNextPermitions;
  68. }
  69. it.BasePermissions &= bp;
  70. it.CurrentPermissions &= bp;
  71. // it.GroupPermissions &= allowedPermitions;
  72. it.NextPermissions &= np;
  73. // it.EveryOnePermissions &= allowedPermitions;
  74. it.GroupPermissions = 0;
  75. it.EveryOnePermissions = 0;
  76. }
  77. public int AnimationCount { get; private set; }
  78. private Dictionary<string, KeyValuePair<string, UUID>> m_animations = new Dictionary<string, KeyValuePair<string, UUID>>();
  79. public UUID GetAnimation(string index)
  80. {
  81. KeyValuePair<string, UUID> val;
  82. if (m_animations.TryGetValue(index, out val))
  83. return val.Value;
  84. return UUID.Zero;
  85. }
  86. public string GetAnimationName(string index)
  87. {
  88. KeyValuePair<string, UUID> val;
  89. if (m_animations.TryGetValue(index, out val))
  90. return val.Key;
  91. return String.Empty;
  92. }
  93. public void SetAnimation(string index, string name, UUID anim)
  94. {
  95. if (anim.IsZero())
  96. {
  97. m_animations.Remove(index);
  98. return;
  99. }
  100. m_animations[index] = new KeyValuePair<string, UUID>(name, anim);
  101. }
  102. public AnimationSet(Byte[] data)
  103. {
  104. string assetData = System.Text.Encoding.ASCII.GetString(data);
  105. Console.WriteLine("--------------------");
  106. Console.WriteLine("AnimationSet length {0} bytes", assetData.Length);
  107. Console.WriteLine(assetData);
  108. Console.WriteLine("--------------------");
  109. }
  110. public Byte[] ToBytes()
  111. {
  112. // If there was an error parsing the input, we give back an
  113. // empty set rather than the original data.
  114. if (m_parseError)
  115. {
  116. string dummy = "version 1\ncount 0\n";
  117. return System.Text.Encoding.ASCII.GetBytes(dummy);
  118. }
  119. string assetData = String.Format("version 1\ncount {0}\n", m_animations.Count);
  120. foreach (KeyValuePair<string, KeyValuePair<string, UUID>> kvp in m_animations)
  121. assetData += String.Format("{0} {1} {2}\n", kvp.Key, kvp.Value.Value.ToString(), kvp.Value.Key);
  122. return System.Text.Encoding.ASCII.GetBytes(assetData);
  123. }
  124. /*
  125. public bool Validate(AnimationSetValidator val)
  126. {
  127. if (m_parseError)
  128. return false;
  129. List<string> badAnims = new List<string>();
  130. bool allOk = true;
  131. foreach (KeyValuePair<string, KeyValuePair<string, UUID>> kvp in m_animations)
  132. {
  133. if (!val(kvp.Value.Value))
  134. {
  135. allOk = false;
  136. badAnims.Add(kvp.Key);
  137. }
  138. }
  139. foreach (string idx in badAnims)
  140. m_animations.Remove(idx);
  141. return allOk;
  142. }
  143. */
  144. public uint Validate(AnimationSetValidator val)
  145. {
  146. if (m_parseError)
  147. return 0;
  148. uint ret = 0x7fffffff;
  149. uint t;
  150. foreach (KeyValuePair<string, KeyValuePair<string, UUID>> kvp in m_animations)
  151. {
  152. t = val(kvp.Value.Value);
  153. if (t == 0)
  154. return 0;
  155. ret &= t;
  156. }
  157. return ret;
  158. }
  159. }
  160. }