From 98551a5856cc86014017a013f59677dd2ec2420c Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sat, 15 Oct 2022 02:13:16 +0100 Subject: [PATCH] use new c# features(sugar) on uuidgather --- .../Region/Framework/Scenes/UuidGatherer.cs | 233 ++++++++---------- 1 file changed, 109 insertions(+), 124 deletions(-) diff --git a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs index 7874070ccc..6734605b67 100644 --- a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs +++ b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs @@ -27,15 +27,12 @@ using System; using System.Collections.Generic; -using System.IO; using System.Reflection; using System.Text; using log4net; using OpenMetaverse; -using OpenMetaverse.Assets; using OpenMetaverse.StructuredData; using OpenSim.Framework; -using OpenSim.Region.Framework.Scenes.Serialization; using OpenSim.Services.Interfaces; using OpenSimAssetType = OpenSim.Framework.SLUtil.OpenSimAssetType; @@ -54,7 +51,7 @@ namespace OpenSim.Region.Framework.Scenes { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - private static HashSet ToSkip = new HashSet() + private static readonly HashSet ToSkip = new() { new UUID("11111111-1111-0000-0000-000100bba000"), new UUID("5a9f4a74-30f2-821c-b88d-70499d3e7183"), @@ -248,10 +245,7 @@ namespace OpenSim.Region.Framework.Scenes { get { - if (Complete) - return null; - else - return m_assetUuidsToInspect.Peek(); + return Complete ? null : m_assetUuidsToInspect.Peek(); } } @@ -381,19 +375,19 @@ namespace OpenSim.Region.Framework.Scenes try { Primitive.TextureEntry textureEntry = part.Shape.Textures; - if (textureEntry != null) + if (textureEntry is not null) { // Get the prim's default texture. This will be used for faces which don't have their own texture - if (textureEntry.DefaultTexture != null) + if (textureEntry.DefaultTexture is not null) RecordTextureEntryAssetUuids(textureEntry.DefaultTexture); - if (textureEntry.FaceTextures != null) + if (textureEntry.FaceTextures is not null) { // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture) int nsides = part.GetNumberOfSides(); foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures) { - if (texture != null) + if (texture is not null) RecordTextureEntryAssetUuids(texture); if(--nsides <= 0) break; @@ -407,7 +401,7 @@ namespace OpenSim.Region.Framework.Scenes if (part.Shape.ProjectionTextureUUID.IsNotZero()) GatheredUuids[part.Shape.ProjectionTextureUUID] = (sbyte)AssetType.Texture; - if(part.Shape.RenderMaterials != null && part.Shape.RenderMaterials.entries != null) + if(part.Shape.RenderMaterials is not null && part.Shape.RenderMaterials.entries is not null) { for(int j = 0; j < part.Shape.RenderMaterials.entries.Length; ++j) { @@ -427,7 +421,7 @@ namespace OpenSim.Region.Framework.Scenes { try { - Primitive.ParticleSystem ps = new Primitive.ParticleSystem(part.ParticleSystem, 0); + Primitive.ParticleSystem ps = new(part.ParticleSystem, 0); if (ps.Texture.IsNotZero()) GatheredUuids[ps.Texture] = (sbyte)AssetType.Texture; } @@ -447,7 +441,7 @@ namespace OpenSim.Region.Framework.Scenes AddForInspection(tii.AssetID, (sbyte)tii.Type); } - if(part.Animations != null && part.Animations.Count > 0) + if(part.Animations is not null && part.Animations.Count > 0) { foreach(UUID id in part.Animations.Keys) { @@ -462,7 +456,7 @@ namespace OpenSim.Region.Framework.Scenes } catch (Exception e) { - m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e); + m_log.Error($"[UUID GATHERER]: Failed to get part - {e.Message}"); } } if(sceneObject.TemporaryInstance) @@ -502,7 +496,7 @@ namespace OpenSim.Region.Framework.Scenes if (report && FailedUUIDs.Count > 0) { - StringBuilder sb = new StringBuilder(512); + StringBuilder sb = new(512); int i = FailedUUIDs.Count; sb.Append("[UUID GATHERER]: UUIDs that are not assets or really missing assets:\n\t"); foreach (UUID id in FailedUUIDs) @@ -554,7 +548,7 @@ namespace OpenSim.Region.Framework.Scenes catch (Exception e) { if(verbose) - m_log.ErrorFormat("[UUID GATHERER]: Failed to get asset {0} : {1}", assetUuid, e.Message); + m_log.Error($"[UUID GATHERER]: Failed to get asset {assetUuid} : {e.Message}"); ErrorCount++; FailedUUIDs.Add(assetUuid); return; @@ -624,7 +618,7 @@ namespace OpenSim.Region.Framework.Scenes catch (Exception e) { if(verbose) - m_log.ErrorFormat("[UUID GATHERER]: Failed to gather uuids for asset with id {0} type {1}: {2}", assetUuid, assetType, e.Message); + m_log.Error($"[UUID GATHERER]: Failed to gather uuids for asset with id {assetUuid} type {assetType}: {e.Message}"); GatheredUuids.Remove(assetUuid); ErrorCount++; FailedUUIDs.Add(assetUuid); @@ -649,28 +643,29 @@ namespace OpenSim.Region.Framework.Scenes return; try { - if ((sbyte)AssetType.Bodypart == assetType - || (sbyte)AssetType.Clothing == assetType - || (sbyte)AssetType.Gesture == assetType - || (sbyte)AssetType.Notecard == assetType - || (sbyte)AssetType.LSLText == assetType - || (sbyte)OpenSimAssetType.Material == assetType - || (sbyte)AssetType.Object == assetType - || (sbyte)AssetType.Settings == assetType - || (sbyte)AssetType.Material == assetType) + switch (assetType) { - AddForInspection(assetUuid); - } - else - { - GatheredUuids[assetUuid] = assetType; + case (sbyte)AssetType.Bodypart: + case (sbyte)AssetType.Clothing: + case (sbyte)AssetType.Gesture: + case (sbyte)AssetType.Notecard: + case (sbyte)AssetType.LSLText: + case (sbyte)OpenSimAssetType.Material: + case (sbyte)AssetType.Object: + case (sbyte)AssetType.Settings: + case (sbyte)AssetType.Material: + AddForInspection(assetUuid); + break; + + default: + GatheredUuids[assetUuid] = assetType; + break; } } catch (Exception) { - m_log.ErrorFormat( - "[UUID GATHERER]: Failed to gather uuids for asset id {0}, type {1}", - assetUuid, assetType); + m_log.Error( + $"[UUID GATHERER]: Failed to gather uuids for asset id {assetUuid}, type {assetType}"); throw; } } @@ -700,61 +695,54 @@ namespace OpenSim.Region.Framework.Scenes private void RecordMaterialsUuids(SceneObjectPart part) { // scan thru the dynAttrs map of this part for any textures used as materials - OSD osdMaterials = null; if(part.DynAttrs == null) return; lock (part.DynAttrs) { - if (part.DynAttrs.ContainsStore("OpenSim", "Materials")) - { - OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials"); + if (!part.DynAttrs.TryGetStore("OpenSim", "Materials", out OSDMap materialsStore)) + return; - if (materialsStore == null) - return; + if (materialsStore == null) + return; - materialsStore.TryGetValue("Materials", out osdMaterials); - } + materialsStore.TryGetValue("Materials", out OSD osdMaterials); - if (osdMaterials != null) + if (osdMaterials is OSDArray matsArr) { //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd)); - if (osdMaterials is OSDArray) + foreach (OSDMap matMap in matsArr) { - OSDArray matsArr = osdMaterials as OSDArray; - foreach (OSDMap matMap in matsArr) + try { - try + if (matMap.ContainsKey("Material")) { - if (matMap.ContainsKey("Material")) + OSDMap mat = matMap["Material"] as OSDMap; + if (mat.TryGetValue("NormMap", out OSD tmap)) { - OSDMap mat = matMap["Material"] as OSDMap; - if (mat.ContainsKey("NormMap")) + UUID normalMapId = tmap.AsUUID(); + if (normalMapId.IsNotZero()) { - UUID normalMapId = mat["NormMap"].AsUUID(); - if (!normalMapId.IsZero()) - { - GatheredUuids[normalMapId] = (sbyte)AssetType.Texture; - //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString()); - } - } - if (mat.ContainsKey("SpecMap")) - { - UUID specularMapId = mat["SpecMap"].AsUUID(); - if (!specularMapId.IsZero()) - { - GatheredUuids[specularMapId] = (sbyte)AssetType.Texture; - //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString()); - } + GatheredUuids[normalMapId] = (sbyte)AssetType.Texture; + //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString()); } } + if (mat.TryGetValue("SpecMap", out OSD tsmap)) + { + UUID specularMapId = tsmap.AsUUID(); + if (specularMapId.IsNotZero()) + { + GatheredUuids[specularMapId] = (sbyte)AssetType.Texture; + //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString()); + } + } + } - } - catch (Exception e) - { - m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message); - } + } + catch (Exception e) + { + m_log.Warn($"[UUID Gatherer]: exception getting materials: {e.Message}"); } } } @@ -784,7 +772,7 @@ namespace OpenSim.Region.Framework.Scenes return; List ids = Util.GetUUIDsOnData(textAsset.Data, 0, textAsset.Data.Length); - if (ids == null || ids.Count == 0) + if (ids is null) return; for (int i = 0; i < ids.Count; ++i) @@ -799,7 +787,7 @@ namespace OpenSim.Region.Framework.Scenes private void RecordNoteCardEmbeddedAssetUuids(AssetBase textAsset) { List ids = SLUtil.GetEmbeddedAssetIDs(textAsset.Data); - if(ids == null || ids.Count == 0) + if(ids is null) return; for(int i = 0; i < ids.Count; ++i) @@ -811,18 +799,18 @@ namespace OpenSim.Region.Framework.Scenes } } - private static byte[] wearableSeps = new byte[]{(byte)' ', (byte)'\t'}; + private static readonly byte[] wearableSeps = new byte[]{(byte)' ', (byte)'\t'}; /// /// Record the uuids referenced by the given wearable asset /// /// private void RecordWearableAssetUuids(AssetBase asset) { - if (asset.Data == null || asset.Data.Length < 64) + if (asset.Data is null || asset.Data.Length < 64) return; try { - osUTF8Slice ostmp = new osUTF8Slice(asset.Data); + osUTF8Slice ostmp = new(asset.Data); if (!ostmp.SkipLine()) // version return; if (!ostmp.SkipLine()) // name @@ -868,7 +856,7 @@ namespace OpenSim.Region.Framework.Scenes if(texparts.Length <2 || texparts[1].Length < 36) continue; texparts[1].SelfTrim(wearableSeps); - if (UUID.TryParse(texparts[1].ToString(), out UUID id) && !id.IsZero()) + if (UUID.TryParse(texparts[1].ToString(), out UUID id) && id.IsNotZero()) GatheredUuids[id] = (sbyte)AssetType.Texture; } } @@ -882,7 +870,7 @@ namespace OpenSim.Region.Framework.Scenes private int getxmlNode(osUTF8Slice data, out osUTF8Slice h) { h = data; - int st = -1; + int st; while ((st = data.IndexOf('<')) >= 0) { if (st > 0 && data[st - 1] == (byte)'\\') @@ -892,7 +880,7 @@ namespace OpenSim.Region.Framework.Scenes if (st < 0) return -1; ++st; - int ed = -1; + int ed; while ((ed = data.IndexOf('>')) >= 0) { if (data[st - 1] == (byte)'\\') @@ -971,20 +959,20 @@ namespace OpenSim.Region.Framework.Scenes // bad ugly - private static byte[] UUIDB = osUTF8.GetASCIIBytes("UUID"); - private static byte[] uuidB = osUTF8.GetASCIIBytes("uuid"); - private static byte[] SOPAnimsB = osUTF8.GetASCIIBytes("SOPAnims"); - private static byte[] CollisionSoundB = osUTF8.GetASCIIBytes("CollisionSound"); - private static byte[] SoundIDB = osUTF8.GetASCIIBytes("SoundID"); - private static byte[] SculptTextureB = osUTF8.GetASCIIBytes("SculptTexture"); - private static byte[] ExtraParamsB = osUTF8.GetASCIIBytes("ExtraParams"); - private static byte[] ParticleSystemB = osUTF8.GetASCIIBytes("ParticleSystem"); - private static byte[] TextureEntryB = osUTF8.GetASCIIBytes("TextureEntry"); - private static byte[] TaskInventoryB = osUTF8.GetASCIIBytes("TaskInventory"); - private static byte[] endTaskInventoryB = osUTF8.GetASCIIBytes("/TaskInventory"); - private static byte[] AssetIDB = osUTF8.GetASCIIBytes("AssetID"); - private static byte[] texturesB = osUTF8.GetASCIIBytes("textures"); - private static byte[] parametersB = osUTF8.GetASCIIBytes("parameters"); + private static readonly byte[] UUIDB = osUTF8.GetASCIIBytes("UUID"); + private static readonly byte[] uuidB = osUTF8.GetASCIIBytes("uuid"); + private static readonly byte[] SOPAnimsB = osUTF8.GetASCIIBytes("SOPAnims"); + private static readonly byte[] CollisionSoundB = osUTF8.GetASCIIBytes("CollisionSound"); + private static readonly byte[] SoundIDB = osUTF8.GetASCIIBytes("SoundID"); + private static readonly byte[] SculptTextureB = osUTF8.GetASCIIBytes("SculptTexture"); + private static readonly byte[] ExtraParamsB = osUTF8.GetASCIIBytes("ExtraParams"); + private static readonly byte[] ParticleSystemB = osUTF8.GetASCIIBytes("ParticleSystem"); + private static readonly byte[] TextureEntryB = osUTF8.GetASCIIBytes("TextureEntry"); + private static readonly byte[] TaskInventoryB = osUTF8.GetASCIIBytes("TaskInventory"); + private static readonly byte[] endTaskInventoryB = osUTF8.GetASCIIBytes("/TaskInventory"); + private static readonly byte[] AssetIDB = osUTF8.GetASCIIBytes("AssetID"); + private static readonly byte[] texturesB = osUTF8.GetASCIIBytes("textures"); + private static readonly byte[] parametersB = osUTF8.GetASCIIBytes("parameters"); /// /// Get all the asset uuids associated with a given object. This includes both those directly associated with @@ -994,11 +982,9 @@ namespace OpenSim.Region.Framework.Scenes /// private void RecordSceneObjectAssetUuids(AssetBase sceneObjectAsset) { - osUTF8Slice data = new osUTF8Slice(sceneObjectAsset.Data); + osUTF8Slice data = new(sceneObjectAsset.Data); - int next; - osUTF8Slice nodeName; - while ((next = getxmlNode(data, out nodeName)) > 0) + while (getxmlNode(data, out osUTF8Slice nodeName) > 0) { if (nodeName.StartsWith((byte)'/')) continue; @@ -1017,7 +1003,7 @@ namespace OpenSim.Region.Framework.Scenes int pos = 2; while (--count >= 0) { - UUID id = new UUID(abytes, pos); + UUID id = new(abytes, pos); if (id.IsZero()) break; if (!ToSkip.Contains(id) && @@ -1039,21 +1025,21 @@ namespace OpenSim.Region.Framework.Scenes { if (!nodeName.EndsWith((byte)'d')) continue; - if (TryGetxmlUUIDValue(data, out UUID id) && !id.IsZero()) + if (TryGetxmlUUIDValue(data, out UUID id) && id.IsNotZero()) GatheredUuids[id] = (sbyte)AssetType.Sound; } else if (nodeName.StartsWith(SoundIDB)) { if (nodeName.EndsWith((byte)'/')) continue; - if (TryGetxmlUUIDValue(data, out UUID id) && !id.IsZero()) + if (TryGetxmlUUIDValue(data, out UUID id) && id.IsNotZero()) GatheredUuids[id] = (sbyte)AssetType.Sound; } else if (nodeName.StartsWith(SculptTextureB)) { if (nodeName.EndsWith((byte)'/')) continue; - if (TryGetxmlUUIDValue(data, out UUID id) && !id.IsZero()) + if (TryGetxmlUUIDValue(data, out UUID id) && id.IsNotZero()) GatheredUuids[id] = (sbyte)AssetType.Texture; // can be mesh but no prob } else if (nodeName.StartsWith(ExtraParamsB)) @@ -1065,10 +1051,10 @@ namespace OpenSim.Region.Framework.Scenes { try { - PrimitiveBaseShape ps = new PrimitiveBaseShape(); + PrimitiveBaseShape ps = new(); ps.ReadInExtraParamsBytes(exbytes); UUID teid = ps.ProjectionTextureUUID; - if (!teid.IsZero() && + if (teid.IsNotZero() && !ToSkip.Contains(teid) && !FailedUUIDs.Contains(teid)) { @@ -1098,9 +1084,9 @@ namespace OpenSim.Region.Framework.Scenes { try { - Primitive.ParticleSystem ps = new Primitive.ParticleSystem(psbytes, 0); + Primitive.ParticleSystem ps = new(psbytes, 0); UUID teid = ps.Texture; - if (!teid.IsZero() && + if (teid.IsNotZero() && !ToSkip.Contains(teid) && !FailedUUIDs.Contains(teid)) { @@ -1120,19 +1106,19 @@ namespace OpenSim.Region.Framework.Scenes { try { - Primitive.TextureEntry te = new Primitive.TextureEntry(tebytes, 0, tebytes.Length); - if (te != null) + Primitive.TextureEntry te = new(tebytes, 0, tebytes.Length); + if (te is not null) { // Get the prim's default texture. This will be used for faces which don't have their own texture - if (te.DefaultTexture != null) + if (te.DefaultTexture is not null) RecordTextureEntryAssetUuids(te.DefaultTexture); - if (te.FaceTextures != null) + if (te.FaceTextures is not null) { // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture) foreach (Primitive.TextureEntryFace texture in te.FaceTextures) { - if (texture != null) + if (texture is not null) RecordTextureEntryAssetUuids(texture); } } @@ -1146,11 +1132,11 @@ namespace OpenSim.Region.Framework.Scenes { if (nodeName.EndsWith((byte)'/')) continue; - while ((next = getxmlNode(data, out nodeName)) > 0) + while (getxmlNode(data, out nodeName) > 0) { if (nodeName.StartsWith(AssetIDB)) { - if (TryGetxmlUUIDValue(data, out UUID id) && !id.IsZero()) + if (TryGetxmlUUIDValue(data, out UUID id) && id.IsNotZero()) AddForInspection(id); } else if (nodeName.StartsWith(endTaskInventoryB)) @@ -1166,7 +1152,7 @@ namespace OpenSim.Region.Framework.Scenes /// private void RecordGestureAssetUuids(AssetBase gestureAsset) { - osUTF8Slice osdata = new osUTF8Slice(gestureAsset.Data); + osUTF8Slice osdata = new(gestureAsset.Data); if (!osdata.SkipLine()) // version return; @@ -1194,7 +1180,6 @@ namespace OpenSim.Region.Framework.Scenes return; osUTF8Slice id; - UUID uid; switch(type) { case 0: // animation @@ -1203,7 +1188,7 @@ namespace OpenSim.Region.Framework.Scenes return; if (!osdata.ReadLine(out id)) // uuid return; - if (osUTF8Slice.TryParseUUID(id, out uid) && !uid.IsZero()) + if (osUTF8Slice.TryParseUUID(id, out UUID uid) && uid.IsNotZero()) GatheredUuids[uid] = type == 0 ? (sbyte)AssetType.Animation : (sbyte)AssetType.Sound; if (!osdata.SkipLine()) // flags return; @@ -1226,9 +1211,8 @@ namespace OpenSim.Region.Framework.Scenes /// private void RecordMaterialAssetUuids(AssetBase materialAsset) { - osUTF8Slice data = new osUTF8Slice(materialAsset.Data); - int next; - while ((next = getxmlNode(data, out osUTF8Slice header)) > 0) + osUTF8Slice data = new(materialAsset.Data); + while (getxmlNode(data, out osUTF8Slice header) > 0) { if (header.StartsWith((byte)'/')) continue; @@ -1240,7 +1224,7 @@ namespace OpenSim.Region.Framework.Scenes if(indx < 0) continue; osUTF8Slice tmp = data.SubUTF8(0, indx); - if(osUTF8Slice.TryParseUUID(tmp, out UUID id) && !id.IsZero()) + if(osUTF8Slice.TryParseUUID(tmp, out UUID id) && id.IsNotZero()) GatheredUuids[id] = (sbyte)AssetType.Texture; data.SubUTF8Self(indx + 1); } @@ -1273,11 +1257,12 @@ namespace OpenSim.Region.Framework.Scenes public AssetBase FetchAsset(UUID assetID) { - AssetBase asset = m_assetService.Get(assetID.ToString(), m_assetServerURL, true); - if (asset != null) - m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL); + string IDstr = assetID.ToString(); + AssetBase asset = m_assetService.Get(IDstr, m_assetServerURL, true); + if (asset is null) + m_log.Debug($"[HGUUIDGatherer]: Failed to fetch asset {IDstr} from {m_assetServerURL}"); else - m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL); + m_log.Debug($"[HGUUIDGatherer]: Copied asset {IDstr} from {m_assetServerURL} to local asset server"); return asset; }