WearableItem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 System.Globalization;
  30. using System.IO;
  31. using System.Text.RegularExpressions;
  32. using OpenMetaverse;
  33. namespace OpenSim.Framework
  34. {
  35. public class WearableItem
  36. {
  37. public string WearableName = "";
  38. public WearableType WearType = WearableType.Invalid;
  39. public string ItemInfo = "Created Wearable";
  40. public SortedList<int, VisualSetting> VisualSettings = new SortedList<int, VisualSetting>();
  41. // public LLObject.TextureEntry TextureEntry = null;
  42. //public byte[] TextureEntry = null;
  43. public List<string> TextureStrings = new List<string>();
  44. //permissions
  45. public uint BaseMask = 0;
  46. public uint OwnerMask = 0;
  47. public uint GroupMask = 0;
  48. public uint EveryoneMask = 0;
  49. public uint NextOwnerMask = 0;
  50. public UUID CreatorID = UUID.Zero;
  51. public UUID OwnerID = UUID.Zero;
  52. public UUID LastOwnerID = UUID.Zero;
  53. public UUID GroupID = UUID.Zero;
  54. //sale
  55. public string SaleType = "not";
  56. public int SalePrice = 10;
  57. private string BuildString = "";
  58. public WearableItem(string wearableName, WearableType type)
  59. {
  60. WearableName = wearableName;
  61. WearType = type;
  62. }
  63. public WearableItem(string wearableName)
  64. {
  65. WearableName = wearableName;
  66. WearType = ConvertNameToType(WearableName);
  67. }
  68. public WearableItem(WearableType type)
  69. {
  70. WearType = type;
  71. WearableName = Enum.GetName(typeof(WearableType), type).ToLower();
  72. }
  73. public WearableItem()
  74. {
  75. }
  76. public void AddVisualSetting(VisualSetting setting)
  77. {
  78. if (!VisualSettings.ContainsKey(setting.VisualParam.ParamID))
  79. {
  80. VisualSettings.Add(setting.VisualParam.ParamID, setting);
  81. }
  82. }
  83. public bool TryGetSetting(string paramName, out VisualSetting paramSetting)
  84. {
  85. foreach (VisualSetting setting in VisualSettings.Values)
  86. {
  87. if (setting.VisualParam.Name == paramName)
  88. {
  89. paramSetting = setting;
  90. return true;
  91. }
  92. }
  93. paramSetting = null;
  94. return false;
  95. }
  96. public bool SetParamValue(string paramName, float value)
  97. {
  98. VisualSetting paramSetting;
  99. if (TryGetSetting(paramName, out paramSetting))
  100. {
  101. if ((value >= paramSetting.VisualParam.MinValue) && (value <= paramSetting.VisualParam.MaxValue))
  102. {
  103. paramSetting.Value = value;
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. public void RandomiseValues()
  110. {
  111. foreach (VisualSetting setting in VisualSettings.Values)
  112. {
  113. //int randNum = Util.RandomClass.Next(0, 1000);
  114. float range = setting.VisualParam.MaxValue - setting.VisualParam.MinValue;
  115. // float val = ((float) randNum) / ((float)(1000.0f / range));
  116. float val = (float)Util.RandomClass.NextDouble() * range * 0.2f;
  117. setting.Value = setting.VisualParam.MinValue + (range / 2) + val;
  118. }
  119. }
  120. public WearableType ConvertNameToType(string name)
  121. {
  122. return (WearableType)Enum.Parse(typeof(WearableType), name, true);
  123. }
  124. public string ToAssetFormat()
  125. {
  126. BuildString = "LLWearable version 22\n";
  127. BuildString += "New Item \n";
  128. BuildString += ItemInfo + "\n";
  129. AddSectionStart("permissions");
  130. AddTabbedNameValueLine("base_mask", BaseMask.ToString("00000000"));
  131. AddTabbedNameValueLine("owner_mask", OwnerMask.ToString("00000000"));
  132. AddTabbedNameValueLine("group_mask", GroupMask.ToString("00000000"));
  133. AddTabbedNameValueLine("everyone_mask", EveryoneMask.ToString("00000000"));
  134. AddTabbedNameValueLine("next_owner_mask", NextOwnerMask.ToString("00000000"));
  135. AddTabbedNameValueLine("creator_id", CreatorID.ToString());
  136. AddTabbedNameValueLine("owner_id", OwnerID.ToString());
  137. AddTabbedNameValueLine("last_owner_id", LastOwnerID.ToString());
  138. AddTabbedNameValueLine("group_id", GroupID.ToString());
  139. AddSectionEnd();
  140. AddSectionStart("sale_info");
  141. AddTabbedNameValueLine("sale_type", SaleType.ToString());
  142. AddTabbedNameValueLine("sale_price", SalePrice.ToString());
  143. AddSectionEnd();
  144. AddNameValueLine("type", ((byte)WearType).ToString());
  145. AddNameValueLine("parameters", VisualSettings.Count.ToString());
  146. foreach (KeyValuePair<int, VisualSetting> kp in VisualSettings)
  147. {
  148. AddNameValueLine(kp.Key.ToString(), kp.Value.Value.ToString(CultureInfo.InvariantCulture));
  149. }
  150. if (TextureStrings.Count == 0)
  151. {
  152. AddNameValueLine("textures", "0"); //todo output texture entry
  153. }
  154. else
  155. {
  156. AddNameValueLine("textures", TextureStrings.Count.ToString());
  157. for (int i = 0; i < TextureStrings.Count; i++)
  158. {
  159. BuildString += TextureStrings[i] + "\n";
  160. }
  161. BuildString += "\n";
  162. }
  163. return BuildString;
  164. }
  165. public void SaveToFile(string fileName)
  166. {
  167. File.WriteAllText(fileName, this.ToAssetFormat());
  168. }
  169. public void AddSectionStart(string sectionName)
  170. {
  171. BuildString += "\t" + sectionName + " 0\n";
  172. BuildString += "\t{\n";
  173. }
  174. public void AddSectionEnd()
  175. {
  176. BuildString += "\t}\n";
  177. }
  178. private void AddTabbedNameValueLine(string name, string value)
  179. {
  180. BuildString += "\t\t";
  181. BuildString += name + "\t";
  182. BuildString += value + "\n";
  183. }
  184. private void AddNameValueLine(string name, string value)
  185. {
  186. // BuildString += "\t\t";
  187. BuildString += name + " ";
  188. BuildString += value + "\n";
  189. }
  190. #region Static Methods
  191. public static List<VisualParam> FindParamsForWearable(string wearableName)
  192. {
  193. List<VisualParam> wearableParams = new List<VisualParam>();
  194. foreach (VisualParam param in VisualParams.Params.Values)
  195. {
  196. if (param.Wearable == wearableName)
  197. {
  198. wearableParams.Add(param);
  199. }
  200. }
  201. return wearableParams;
  202. }
  203. public static WearableItem Create(string wearableTypeName)
  204. {
  205. WearableItem wearableItem = new WearableItem(wearableTypeName);
  206. List<VisualParam> typeParams = FindParamsForWearable(wearableTypeName);
  207. foreach (VisualParam param in typeParams)
  208. {
  209. wearableItem.AddVisualSetting(new VisualSetting(param));
  210. }
  211. return wearableItem;
  212. }
  213. public static WearableItem CreateFromAsset(string assetData)
  214. {
  215. UUID creatorID = UUID.Zero;
  216. UUID ownerID = UUID.Zero;
  217. UUID lastOwnerID = UUID.Zero;
  218. UUID groupID = UUID.Zero;
  219. char[] newlineDelimiter = { '\n' };
  220. string[] lines = assetData.Split(newlineDelimiter);
  221. WearableItem wearableObject = null;
  222. Regex r = new Regex("[\t ]+");
  223. bool reachedParams = false;
  224. bool reachedTextures = false;
  225. foreach (string line in lines)
  226. {
  227. string trimLine = line.Trim();
  228. // m_log.Debug("line : " + trimLine);
  229. string[] splitLine = r.Split(trimLine);
  230. if (splitLine.Length > 1)
  231. {
  232. switch (splitLine[0])
  233. {
  234. case "textures":
  235. reachedParams = false;
  236. reachedTextures = true;
  237. break;
  238. case "type":
  239. string wearableTypeName = Enum.GetName(typeof(WearableType), (WearableType)Convert.ToInt32(splitLine[1]));
  240. wearableObject = Create(wearableTypeName.ToLower());
  241. break;
  242. case "parameters":
  243. reachedParams = true;
  244. break;
  245. case "creator_id":
  246. creatorID = new UUID(splitLine[1]);
  247. break;
  248. case "owner_id":
  249. ownerID = new UUID(splitLine[1]);
  250. break;
  251. case "last_owner_id":
  252. lastOwnerID = new UUID(splitLine[1]);
  253. break;
  254. case "group_id":
  255. groupID = new UUID(splitLine[1]);
  256. break;
  257. default:
  258. if ((wearableObject != null) && (reachedParams))
  259. {
  260. int id = Convert.ToInt32(splitLine[0]);
  261. if (wearableObject.VisualSettings.ContainsKey(id))
  262. {
  263. wearableObject.VisualSettings[id].Value = Convert.ToSingle(splitLine[1], CultureInfo.InvariantCulture);
  264. }
  265. }
  266. else if ((wearableObject != null) && (reachedTextures))
  267. {
  268. wearableObject.TextureStrings.Add(line);
  269. }
  270. break;
  271. }
  272. }
  273. }
  274. if (wearableObject != null)
  275. {
  276. wearableObject.CreatorID = creatorID;
  277. wearableObject.OwnerID = ownerID;
  278. wearableObject.LastOwnerID = lastOwnerID;
  279. wearableObject.GroupID = groupID;
  280. }
  281. return wearableObject;
  282. }
  283. #endregion
  284. #region Nested Class
  285. public class VisualSetting
  286. {
  287. public VisualParam VisualParam;
  288. public float Value = 0;
  289. public VisualSetting(VisualParam param, float value)
  290. {
  291. VisualParam = param;
  292. Value = value;
  293. }
  294. public VisualSetting(VisualParam param)
  295. {
  296. VisualParam = param;
  297. Value = param.DefaultValue;
  298. }
  299. }
  300. #endregion
  301. }
  302. }