AnimationSet.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 class AnimationSet
  34. {
  35. private bool m_parseError = false;
  36. public const uint createBasePermitions = (uint)(PermissionMask.All); // no export ?
  37. public const uint createNextPermitions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  38. public const uint allowedBasePermitions = (uint)(PermissionMask.Copy | PermissionMask.Modify);
  39. public const uint allowedNextPermitions = 0;
  40. public static void setCreateItemPermitions(InventoryItemBase it)
  41. {
  42. if (it == null)
  43. return;
  44. it.BasePermissions = createBasePermitions;
  45. it.CurrentPermissions = createBasePermitions;
  46. // it.GroupPermissions &= allowedPermitions;
  47. it.NextPermissions = createNextPermitions;
  48. // it.EveryOnePermissions &= allowedPermitions;
  49. it.GroupPermissions = 0;
  50. it.EveryOnePermissions = 0;
  51. }
  52. public static void enforceItemPermitions(InventoryItemBase it, bool IsCreator)
  53. {
  54. if (it == null)
  55. return;
  56. uint bp;
  57. uint np;
  58. if (IsCreator)
  59. {
  60. bp = createBasePermitions;
  61. np = createNextPermitions;
  62. }
  63. else
  64. {
  65. bp = allowedBasePermitions;
  66. np = allowedNextPermitions;
  67. }
  68. it.BasePermissions &= bp;
  69. it.CurrentPermissions &= bp;
  70. // it.GroupPermissions &= allowedPermitions;
  71. it.NextPermissions &= np;
  72. // it.EveryOnePermissions &= allowedPermitions;
  73. it.GroupPermissions = 0;
  74. it.EveryOnePermissions = 0;
  75. }
  76. public int AnimationCount { get; private set; }
  77. private Dictionary<string, KeyValuePair<string, UUID>> m_animations = new Dictionary<string, KeyValuePair<string, UUID>>();
  78. public UUID GetAnimation(string index)
  79. {
  80. KeyValuePair<string, UUID> val;
  81. if (m_animations.TryGetValue(index, out val))
  82. return val.Value;
  83. return UUID.Zero;
  84. }
  85. public string GetAnimationName(string index)
  86. {
  87. KeyValuePair<string, UUID> val;
  88. if (m_animations.TryGetValue(index, out val))
  89. return val.Key;
  90. return String.Empty;
  91. }
  92. public void SetAnimation(string index, string name, UUID anim)
  93. {
  94. if (anim == UUID.Zero)
  95. {
  96. m_animations.Remove(index);
  97. return;
  98. }
  99. m_animations[index] = new KeyValuePair<string, UUID>(name, anim);
  100. }
  101. public AnimationSet(Byte[] data)
  102. {
  103. string assetData = System.Text.Encoding.ASCII.GetString(data);
  104. Console.WriteLine("--------------------");
  105. Console.WriteLine("AnimationSet length {0} bytes", assetData.Length);
  106. Console.WriteLine(assetData);
  107. Console.WriteLine("--------------------");
  108. }
  109. public Byte[] ToBytes()
  110. {
  111. // If there was an error parsing the input, we give back an
  112. // empty set rather than the original data.
  113. if (m_parseError)
  114. {
  115. string dummy = "version 1\ncount 0\n";
  116. return System.Text.Encoding.ASCII.GetBytes(dummy);
  117. }
  118. string assetData = String.Format("version 1\ncount {0}\n", m_animations.Count);
  119. foreach (KeyValuePair<string, KeyValuePair<string, UUID>> kvp in m_animations)
  120. assetData += String.Format("{0} {1} {2}\n", kvp.Key, kvp.Value.Value.ToString(), kvp.Value.Key);
  121. return System.Text.Encoding.ASCII.GetBytes(assetData);
  122. }
  123. public bool Validate(AnimationSetValidator val)
  124. {
  125. if (m_parseError)
  126. return false;
  127. List<string> badAnims = new List<string>();
  128. bool allOk = true;
  129. foreach (KeyValuePair<string, KeyValuePair<string, UUID>> kvp in m_animations)
  130. {
  131. if (!val(kvp.Value.Value))
  132. {
  133. allOk = false;
  134. badAnims.Add(kvp.Key);
  135. }
  136. }
  137. foreach (string idx in badAnims)
  138. m_animations.Remove(idx);
  139. return allOk;
  140. }
  141. }
  142. }