ViewerDaycycle.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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.Collections.Generic;
  28. using OpenMetaverse;
  29. using OpenMetaverse.StructuredData;
  30. namespace OpenSim.Framework
  31. {
  32. public class DayCycle
  33. {
  34. public struct TrackEntry
  35. {
  36. public float time;
  37. public string frameName;
  38. public TrackEntry(float t, string f)
  39. {
  40. time = t;
  41. frameName = f;
  42. }
  43. }
  44. public class CompareTrackEntries : IComparer<TrackEntry>
  45. {
  46. public int Compare(TrackEntry x, TrackEntry y)
  47. {
  48. return x.time.CompareTo(y.time);
  49. }
  50. }
  51. public bool IsStaticDayCycle = false;
  52. public List<TrackEntry> waterTrack = new List<TrackEntry>();
  53. public List<TrackEntry> skyTrack0 = new List<TrackEntry>();
  54. public List<TrackEntry>[] skyTracks = new List<TrackEntry>[3];
  55. public Dictionary<string, SkyData> skyframes = new Dictionary<string, SkyData>();
  56. public Dictionary<string, WaterData> waterframes = new Dictionary<string, WaterData>();
  57. public string Name;
  58. public void FromWLOSD(OSDArray array)
  59. {
  60. CompareTrackEntries cte = new CompareTrackEntries();
  61. TrackEntry track;
  62. OSDArray skytracksArray = null;
  63. if (array.Count > 1)
  64. skytracksArray = array[1] as OSDArray;
  65. if(skytracksArray != null)
  66. {
  67. foreach (OSD setting in skytracksArray)
  68. {
  69. OSDArray innerSetting = setting as OSDArray;
  70. if(innerSetting != null)
  71. {
  72. track = new TrackEntry((float)innerSetting[0].AsReal(), innerSetting[1].AsString());
  73. skyTrack0.Add(track);
  74. }
  75. }
  76. skyTrack0.Sort(cte);
  77. }
  78. OSDMap skyFramesArray = null;
  79. if (array.Count > 2)
  80. skyFramesArray = array[2] as OSDMap;
  81. if(skyFramesArray != null)
  82. {
  83. foreach (KeyValuePair<string, OSD> kvp in skyFramesArray)
  84. {
  85. SkyData sky = new SkyData();
  86. sky.FromWLOSD(kvp.Key, kvp.Value);
  87. skyframes[kvp.Key] = sky;
  88. }
  89. }
  90. WaterData water = new WaterData();
  91. OSDMap watermap = null;
  92. if(array.Count > 3)
  93. watermap = array[3] as OSDMap;
  94. if(watermap != null)
  95. water.FromWLOSD("WLWater", watermap);
  96. waterframes["WLWater"] = water;
  97. track = new TrackEntry(-1f, "WLWater");
  98. waterTrack.Add(track);
  99. Name = "WLDaycycle";
  100. if (skyTrack0.Count == 1 && skyTrack0[0].time == -1f)
  101. IsStaticDayCycle = true;
  102. }
  103. public void ToWLOSD(ref OSDArray array)
  104. {
  105. OSDArray track = new OSDArray();
  106. foreach (TrackEntry te in skyTrack0)
  107. track.Add(new OSDArray { te.time, te.frameName });
  108. array[1] = track;
  109. OSDMap frames = new OSDMap();
  110. foreach (KeyValuePair<string, SkyData> kvp in skyframes)
  111. frames[kvp.Key] = kvp.Value.ToWLOSD();
  112. array[2] = frames;
  113. if(waterTrack.Count > 0)
  114. {
  115. TrackEntry te = waterTrack[0];
  116. if(waterframes.TryGetValue(te.frameName, out WaterData water))
  117. array[3] = water.ToWLOSD();
  118. }
  119. else
  120. array[3] = new OSDMap();
  121. }
  122. public bool replaceWaterFromOSD(string name, OSDMap map)
  123. {
  124. WaterData water = new WaterData();
  125. if(string.IsNullOrWhiteSpace(name))
  126. name = "Water";
  127. try
  128. {
  129. water.FromOSD(name, map);
  130. }
  131. catch
  132. {
  133. return false;
  134. }
  135. waterframes.Clear();
  136. waterframes[name] = water;
  137. waterTrack.Clear();
  138. TrackEntry t = new TrackEntry()
  139. {
  140. time = -1,
  141. frameName = name
  142. };
  143. waterTrack.Add(t);
  144. return true;
  145. }
  146. public bool replaceSkyFromOSD(string name, OSDMap map)
  147. {
  148. SkyData sky = new SkyData();
  149. if (string.IsNullOrWhiteSpace(name))
  150. name = "Sky";
  151. try
  152. {
  153. sky.FromOSD(name, map);
  154. }
  155. catch
  156. {
  157. return false;
  158. }
  159. skyframes.Clear();
  160. skyframes[name] = sky;
  161. TrackEntry t = new TrackEntry()
  162. {
  163. time = -1,
  164. frameName = name
  165. };
  166. skyTrack0.Clear();
  167. skyTrack0.Add(t);
  168. skyTracks = new List<TrackEntry>[3];
  169. return true;
  170. }
  171. public void FromOSD(OSDMap map)
  172. {
  173. CompareTrackEntries cte = new CompareTrackEntries();
  174. OSD otmp;
  175. if(map.TryGetValue("frames", out otmp) && otmp is OSDMap)
  176. {
  177. OSDMap mframes = otmp as OSDMap;
  178. foreach(KeyValuePair<string, OSD> kvp in mframes)
  179. {
  180. OSDMap v = kvp.Value as OSDMap;
  181. if(v.TryGetValue("type", out otmp))
  182. {
  183. string type = otmp;
  184. if (type.Equals("water"))
  185. {
  186. WaterData water = new WaterData();
  187. water.FromOSD(kvp.Key, v);
  188. waterframes[kvp.Key] = water;
  189. }
  190. else if (type.Equals("sky"))
  191. {
  192. SkyData sky = new SkyData();
  193. sky.FromOSD(kvp.Key, v);
  194. skyframes[kvp.Key] = sky;
  195. }
  196. }
  197. }
  198. }
  199. if (map.TryGetValue("name", out otmp))
  200. Name = otmp;
  201. else
  202. Name ="DayCycle";
  203. OSDArray track;
  204. if (map.TryGetValue("tracks", out otmp) && otmp is OSDArray)
  205. {
  206. OSDArray tracks = otmp as OSDArray;
  207. if(tracks.Count > 0)
  208. {
  209. track = tracks[0] as OSDArray;
  210. if (track != null && track.Count > 0)
  211. {
  212. for (int i = 0; i < track.Count; ++i)
  213. {
  214. OSDMap d = track[i] as OSDMap;
  215. if (d.TryGetValue("key_keyframe", out OSD dtime))
  216. {
  217. if (d.TryGetValue("key_name", out OSD dname))
  218. {
  219. TrackEntry t = new TrackEntry()
  220. {
  221. time = dtime,
  222. frameName = dname
  223. };
  224. waterTrack.Add(t);
  225. }
  226. }
  227. }
  228. waterTrack.Sort(cte);
  229. }
  230. }
  231. if (tracks.Count > 1)
  232. {
  233. track = tracks[1] as OSDArray;
  234. if (track != null && track.Count > 0)
  235. {
  236. for (int i = 0; i < track.Count; ++i)
  237. {
  238. OSDMap d = track[i] as OSDMap;
  239. if (d.TryGetValue("key_keyframe", out OSD dtime))
  240. {
  241. if (d.TryGetValue("key_name", out OSD dname))
  242. {
  243. TrackEntry t = new TrackEntry();
  244. t.time = dtime;
  245. t.frameName = dname;
  246. skyTrack0.Add(t);
  247. }
  248. }
  249. }
  250. skyTrack0.Sort(cte);
  251. }
  252. }
  253. if (tracks.Count > 2)
  254. {
  255. for(int st = 2, dt = 0; st < tracks.Count && dt < 3; ++st, ++dt)
  256. {
  257. track = tracks[st] as OSDArray;
  258. if(track != null && track.Count > 0)
  259. {
  260. skyTracks[dt] = new List<TrackEntry>();
  261. for (int i = 0; i < track.Count; ++i)
  262. {
  263. OSDMap d = track[i] as OSDMap;
  264. if (d.TryGetValue("key_keyframe", out OSD dtime))
  265. {
  266. if (d.TryGetValue("key_name", out OSD dname))
  267. {
  268. TrackEntry t = new TrackEntry();
  269. t.time = dtime;
  270. t.frameName = dname;
  271. skyTracks[dt].Add(t);
  272. }
  273. }
  274. }
  275. skyTracks[dt].Sort(cte);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. public OSDMap ToOSD()
  282. {
  283. OSDMap cycle = new OSDMap();
  284. OSDMap frames = new OSDMap();
  285. foreach (KeyValuePair<string, WaterData> kvp in waterframes)
  286. {
  287. frames[kvp.Key] = kvp.Value.ToOSD();
  288. }
  289. foreach (KeyValuePair<string, SkyData> kvp in skyframes)
  290. {
  291. frames[kvp.Key] = kvp.Value.ToOSD();
  292. }
  293. cycle["frames"] = frames;
  294. cycle["name"] = Name;
  295. OSDArray tracks = new OSDArray();
  296. OSDArray track = new OSDArray();
  297. OSDMap tmp;
  298. foreach (TrackEntry te in waterTrack)
  299. {
  300. tmp = new OSDMap();
  301. if (te.time < 0)
  302. tmp["key_keyframe"] = 0f;
  303. else
  304. tmp["key_keyframe"] = te.time;
  305. tmp["key_name"] = te.frameName;
  306. track.Add(tmp);
  307. }
  308. tracks.Add(track);
  309. track = new OSDArray();
  310. foreach (TrackEntry te in skyTrack0)
  311. {
  312. tmp = new OSDMap();
  313. if (te.time < 0)
  314. tmp["key_keyframe"] = 0f;
  315. else
  316. tmp["key_keyframe"] = te.time;
  317. tmp["key_name"] = te.frameName;
  318. track.Add(tmp);
  319. }
  320. tracks.Add(track);
  321. for(int st = 0; st < 3; ++st)
  322. {
  323. track = new OSDArray();
  324. if(skyTracks[st] != null)
  325. {
  326. foreach (TrackEntry te in skyTracks[st])
  327. {
  328. tmp = new OSDMap();
  329. if (te.time < 0)
  330. tmp["key_keyframe"] = 0f;
  331. else
  332. tmp["key_keyframe"] = te.time;
  333. tmp["key_name"] = te.frameName;
  334. track.Add(tmp);
  335. }
  336. }
  337. tracks.Add(track);
  338. }
  339. cycle["tracks"] = tracks;
  340. cycle["type"] = "daycycle";
  341. return cycle;
  342. }
  343. public void GatherAssets(Dictionary<UUID, sbyte> uuids)
  344. {
  345. foreach (WaterData wd in waterframes.Values)
  346. {
  347. wd.GatherAssets(uuids);
  348. }
  349. foreach (SkyData sd in skyframes.Values)
  350. {
  351. sd.GatherAssets(uuids);
  352. }
  353. }
  354. }
  355. }