/// <summary> /// Add a record that a script is being (begin true) or has completed (begin false) executed. /// </summary> /// <param name="scriptDetails">number and script path</param> /// <param name="begin">Whether we're recording the begin or end of script execution.</param> /// <param name="transaction">A SQL transaction is passed upon the initial write of the record for asynchronously running clients.</param> /// <returns>Pass/fail</returns> internal bool RecordScriptInJournal(ScriptDetails scriptDetails, bool begin, SqlTransaction transaction = null) { Debug.Assert(_failedScripts != null, "HashSet of failed scripts is null."); var result = false; var scriptFileName = Path.GetFileName(scriptDetails.FilePath); if (begin) { DeletePriorEntry(scriptDetails.FileNumber, transaction); } var commandText = GetCommandText(scriptDetails, begin, scriptFileName); using var cmd = new SqlCommand(commandText, _connection, transaction); try { cmd.ExecuteNonQuery(); result = true; } catch (SqlException ex) { result = false; _log.LogInfo($"Error adding or updating migration journal record: {ex.Message}"); } return(result); }
private void TransferRequest(SHTarget Target, HubScriptItem HSI) { ControlFrame.Instance.NavigateTo( PageId.SCRIPT_DETAILS , () => new ScriptDetails(HSI) , (View) => { ScriptDetails SD = ( ScriptDetails )View; SD.UpdateTemplate(HSI); SD.PlaceRequest(Target); } ); }
public void ScriptDetails_FromDLModelShouldMapToScriptDetails() { var newScript = new Script { ID = 1, PilotID = 1, ScriptURL = "https://google.com", ScriptWriter = new User { ID = 1 }, ScriptWriterID = 1 }; var result = ScriptDetails.FromDLModel(newScript); Assert.Equal(newScript.ID, result.ID); Assert.Equal(newScript.ScriptURL, result.ScriptURL); Assert.Equal(newScript.ScriptWriterID, result.Scriptwriter.ID); }
ScriptDetails TryAddScript(MonoScript s) { if (s != null) { ScriptDetails details = FindScriptDetails(s); if (details == null) { details = new ScriptDetails(s); ActiveScriptDetails.Add(details); } return(details); } return(null); }
/// <summary> /// Determine whether we can execute the current script by checking the migration journal table. /// If a lock is able to be acquired, a record is added to the journal table (unless an old one was already present). /// The lock is at the table level so no other records may be queried during the time we're adding our "begin script" record. /// The lock is actually released before the method finishes, so the name is a bit off, but follows a familiar naming pattern. /// Additionally, we're actually doing more than just "locking," we're potentially adding a record to the journal table. /// </summary> /// <param name="script"></param> /// <returns>True if the current script hasn't already been executed or is assumed to have failed in a prior run (<see cref="ScriptFailedInPriorRun"/>) or the journal table just doesn't exist</returns> /// <remarks>Assumption: we are running in a security context that has access to the database.</remarks> internal bool TryAcquireLockFor(ScriptDetails script) { var result = false; AssertOpenConnection(); // We need to ensure that we check for the presence of the record and (if good) add the record in one transaction // otherwise another instance may check in the meantime and we both start the script. try { using SqlTransaction transaction = _connection.BeginTransaction(IsolationLevel.Serializable); // Explicit table lock hint. Not often used, maybe I'm re-inventing a queue here, so be it. // The timeout here is 30 seconds. I couldn't find any way to modify that (not related to connection timeout which is just timeout to initial connection to server). using var cmd = new SqlCommand($"select {_journalTableStructure.CompletedColumn}, {_journalTableStructure.BegunColumn} " + $"from {_journalTableStructure.TableName} ({SQL_SERVER_TABLE_LOCK_HINT}) where {_journalTableStructure.NumberColumn} = '{script.FileNumber}'", _connection, transaction); try { using var reader = cmd.ExecuteReader(CommandBehavior.SingleResult); if (!reader.HasRows || ScriptFailedInPriorRun(reader)) { reader.Close(); // must close before write. result = RecordScriptInJournal(script, true, transaction); transaction.Commit(); } else { reader.Close(); transaction.Rollback(); // should mean another instance already added a row, lock not acquired. } } catch (SqlException ex) { transaction.Rollback(); _log.LogInfo($"Error checking database to determine whether script has already run or obtaining a table lock: {ex.Message}"); } } catch (TransactionAbortedException ex) { _log.LogInfo($"{nameof(TransactionAbortedException)} attempting to record a database migration script: {ex.Message}"); } return(result); }
private string GetCommandText(ScriptDetails scriptDetails, bool begin, string scriptFileName) { var msg = string.Empty; char completed = '1'; if (!begin) { if (_failedScripts.ContainsKey(scriptDetails.FileNumber)) { completed = '0'; msg = _failedScripts[scriptDetails.FileNumber].Replace("'", "''"); // value is err msg } } // Note: table has some defaults. return(begin ? $"insert {_journalTableStructure.TableName}({_journalTableStructure.NumberColumn}, {_journalTableStructure.BegunColumn}) values ('{scriptDetails.FileNumber}', GetUtcDate())" : $"update {_journalTableStructure.TableName} set " + $"{_journalTableStructure.CompletedColumn} = {completed}, " + $"{_journalTableStructure.ScriptColumn} = '{scriptFileName}', " + $"{_journalTableStructure.MessageColumn} = '{msg}', " + $"{_journalTableStructure.SchemaChangedColumn} = {(scriptDetails.SchemaChanging ? '1' : '0')}" + $" where {_journalTableStructure.NumberColumn} = '{scriptDetails.FileNumber}'"); }
void CheckResources() { //Debug.Log("CheckResources"); ActiveTextures.Clear(); ActiveMaterials.Clear(); ActiveMeshDetails.Clear(); ActiveShaderDetails.Clear(); ActiveSoundDetails.Clear(); foreach (LightmapData lightmap in LightmapSettings.lightmaps) { TryAddActiveTextures(lightmap.lightmapNear); TryAddActiveTextures(lightmap.lightmapFar); } Renderer[] renderers = (Renderer[])FindObjectsOfType(typeof(Renderer)); foreach (Renderer renderer in renderers) { //Debug.Log("Renderer is "+renderer.name); foreach (Material material in renderer.sharedMaterials) { MaterialDetails tMaterialDetails = TryAddActiveMaterial(material); if (tMaterialDetails != null) { tMaterialDetails.FoundInGameObjects.Add(renderer.gameObject); } ShaderDetails tShaderDetails = TryAddActiveShader(material.shader); if (tShaderDetails != null) { if (!tShaderDetails.FoundInGameObjects.Contains(renderer.gameObject)) { tShaderDetails.FoundInGameObjects.Add(renderer.gameObject); } } } // add the lightmap reference to the renderer if (renderer.lightmapIndex >= 0 && renderer.lightmapIndex < LightmapSettings.lightmaps.Length) { LightmapData lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex]; TextureDetails lmNear = FindTextureDetails(lightmap.lightmapNear); if (lmNear != null && !lmNear.FoundInRenderers.Contains(renderer)) { lmNear.FoundInRenderers.Add(renderer); } TextureDetails lmFar = FindTextureDetails(lightmap.lightmapFar); if (lmFar != null && !lmFar.FoundInRenderers.Contains(renderer)) { lmFar.FoundInRenderers.Add(renderer); } } } foreach (MaterialDetails tMaterialDetails in ActiveMaterials) { Material tMaterial = tMaterialDetails.material; foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { tMaterial })) { if (obj is Texture) { Texture tTexture = obj as Texture; TextureDetails tTextureDetails = TryAddActiveTextures(tTexture); tTextureDetails.FoundInMaterials.Add(tMaterial); } if (obj is Shader) { Shader shader = obj as Shader; ShaderDetails shaderDetails = TryAddActiveShader(shader); if (!shaderDetails.FoundInMaterials.Contains(tMaterial)) { shaderDetails.FoundInMaterials.Add(tMaterial); } } } } MeshFilter[] meshFilters = (MeshFilter[])FindObjectsOfType(typeof(MeshFilter)); foreach (MeshFilter tMeshFilter in meshFilters) { Mesh tMesh = tMeshFilter.sharedMesh; if (tMesh != null) { MeshDetails details = TryAddActiveMesh(tMesh); if (!details.FoundInGameObjects.Contains(tMeshFilter.gameObject)) { details.FoundInGameObjects.Add(tMeshFilter.gameObject); } } } Light[] lights = (Light[])FindObjectsOfType(typeof(Light)); foreach (Light light in lights) { if (light.cookie) { TextureDetails details = TryAddActiveTextures(light.cookie); if (!details.FoundInLights.Contains(light)) { details.FoundInLights.Add(light); } } } GameObject[] gameObjs = (GameObject[])FindObjectsOfType(typeof(GameObject)); foreach (GameObject obj in gameObjs) { foreach (Object o in EditorUtility.CollectDependencies(new UnityEngine.Object[] { obj })) { if (o is AudioClip) { AudioClip clip = o as AudioClip; SoundDetails details = TryAddAudioClip(clip); if (!details.FoundInGameObjects.Contains(obj)) { details.FoundInGameObjects.Add(obj); } } if (o is MonoScript) { MonoScript script = o as MonoScript; ScriptDetails details = TryAddScript(script); if (!details.FoundInGameObjects.Contains(obj)) { details.FoundInGameObjects.Add(obj); } } } } TotalTextureMemory = 0; foreach (TextureDetails tTextureDetails in ActiveTextures) { TotalTextureMemory += tTextureDetails.memSizeBytes; } TotalMeshVertices = 0; foreach (MeshDetails tMeshDetails in ActiveMeshDetails) { TotalMeshVertices += tMeshDetails.mesh.vertexCount; } // Sort by size, descending ActiveTextures.Sort(delegate(TextureDetails details1, TextureDetails details2) { return(details2.memSizeBytes - details1.memSizeBytes); }); ActiveMeshDetails.Sort(delegate(MeshDetails details1, MeshDetails details2) { return(details2.mesh.vertexCount - details1.mesh.vertexCount); }); }
public ErrCode SaveScript(ScriptDetails sd, out int id) { ErrCode err = SiteProvider.CurrentProvider.SaveScript(sd, out id); return err; }
ScriptDetails TryAddScript( MonoScript s ) { if ( s != null ) { ScriptDetails details = FindScriptDetails( s ); if ( details == null ) { details = new ScriptDetails( s ); ActiveScriptDetails.Add( details ); } return details; } return null; }
// 调用时机:目录列表改变,检索模式改变 void checkResources() { string[] paths = null; // TODO: inputPathList从未被使用,一直使用的是当前的inputPath paths = GetFilePaths <Material>(inputPath, searchOption); //目前只会找到有Material引用的自定义Texture和Shader //对于被引用的默认Texture和Shader也进行显示 //找到material使用的texture和shader foreach (string path in paths) { Material material = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material)); if (material != null) { MaterialDetails tMaterialDetails = new MaterialDetails(); tMaterialDetails.FoundInGameObjects = new List <string>(); tMaterialDetails.name = material.name; tMaterialDetails.path = path; // 对于缩略图进行深拷贝 Texture2D preview = AssetPreview.GetAssetPreview(material); tMaterialDetails.preview = new Texture2D(preview.width, preview.height); tMaterialDetails.preview.SetPixels32(preview.GetPixels32()); tMaterialDetails.preview.Apply(); AllMaterials.Add(tMaterialDetails); foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { material })) { string p = AssetDatabase.GetAssetPath(obj); if (p == defaultPath) { p = defaultPath + "::" + obj.name; } if (obj is Texture) { Texture tTexture = (Texture)obj; int check = 0; foreach (TextureDetails details in AllTextures) { if (details.path == p) { check = 1; details.FoundInMaterials.Add(path); break; } } if (check == 0) { TextureDetails tTextureDetails = new TextureDetails(); tTextureDetails.FoundInMaterials = new List <string>(); tTextureDetails.name = tTexture.name; tTextureDetails.path = p; tTextureDetails.preview = AssetPreview.GetMiniThumbnail(tTexture); tTextureDetails.memSizeBytes = TextureDetails.CalculateTextureSizeBytes(tTexture); tTextureDetails.width = tTexture.width; tTextureDetails.height = tTexture.height; tTextureDetails.FoundInMaterials.Add(path); AllTextures.Add(tTextureDetails); } } else if (obj is Shader) { Shader tShader = (Shader)obj; int check = 0; foreach (ShaderDetails details in AllShaders) { if (details.path == p) { check = 1; details.FoundInMaterials.Add(path); break; } } if (check == 0) { ShaderDetails tShaderDetails = new ShaderDetails(); tShaderDetails.FoundInMaterials = new List <string>(); tShaderDetails.FoundInGameObjects = new List <string>(); tShaderDetails.name = tShader.name; tShaderDetails.path = p; tShaderDetails.FoundInMaterials.Add(path); AllShaders.Add(tShaderDetails); } } } } } paths = GetFilePaths <Mesh>(inputPath, searchOption); foreach (string path in paths) { Mesh mesh = (Mesh)AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)); if (mesh != null) { MeshDetails tMeshDetails = new MeshDetails(); tMeshDetails.FoundInGameObjects = new List <string>(); tMeshDetails.name = mesh.name; tMeshDetails.path = path; tMeshDetails.preview = AssetPreview.GetAssetPreview(mesh); tMeshDetails.vertexCount = mesh.vertexCount; tMeshDetails.triangles = mesh.triangles.Length; AllMeshes.Add(tMeshDetails); } } paths = GetFilePaths <AudioClip>(inputPath, searchOption); foreach (string path in paths) { AudioClip clip = (AudioClip)AssetDatabase.LoadAssetAtPath(path, typeof(AudioClip)); if (clip != null) { SoundDetails tSoundDetails = new SoundDetails(); tSoundDetails.FoundInGameObjects = new List <string>(); tSoundDetails.name = clip.name; tSoundDetails.path = path; tSoundDetails.preview = AssetPreview.GetAssetPreview(clip); AllSounds.Add(tSoundDetails); } } paths = GetFilePaths <MonoScript>(inputPath, searchOption); foreach (string path in paths) { MonoScript script = (MonoScript)AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)); if (script != null) { ScriptDetails tScriptDetails = new ScriptDetails(); tScriptDetails.FoundInGameObjects = new List <string>(); tScriptDetails.name = script.name; tScriptDetails.path = path; AllScripts.Add(tScriptDetails); } } // TODO: 优化查重算法 // TODO: 目前只能找到存为prefab的GameObject // 找到GameObject引用的内建资源也能找到 paths = GetFilePaths <GameObject>(inputPath, searchOption); foreach (string path in paths) { GameObject gameObject = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); if (gameObject != null) { foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] { gameObject })) { string p = AssetDatabase.GetAssetPath(obj); if (p == defaultPath) { p = defaultPath + "::" + obj.name; } if (obj is Material) { Material material = (Material)obj; int check = 1; foreach (MaterialDetails details in AllMaterials) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { MaterialDetails tMaterialDetails = new MaterialDetails(); tMaterialDetails.FoundInGameObjects = new List <string>(); tMaterialDetails.name = material.name; tMaterialDetails.path = path; // 对于material的缩略图进行深拷贝 Texture2D preview = AssetPreview.GetAssetPreview(material); tMaterialDetails.preview = new Texture2D(preview.width, preview.height); tMaterialDetails.preview.SetPixels32(preview.GetPixels32()); tMaterialDetails.preview.Apply(); tMaterialDetails.FoundInGameObjects.Add(path); AllMaterials.Add(tMaterialDetails); } } else if (obj is Mesh) { Mesh mesh = (Mesh)obj; int check = 1; foreach (MeshDetails details in AllMeshes) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { MeshDetails tMeshDetails = new MeshDetails(); tMeshDetails.FoundInGameObjects = new List <string>(); tMeshDetails.name = mesh.name; tMeshDetails.path = path; tMeshDetails.preview = AssetPreview.GetAssetPreview(mesh); tMeshDetails.vertexCount = mesh.vertexCount; tMeshDetails.triangles = mesh.triangles.Length; tMeshDetails.FoundInGameObjects.Add(path); AllMeshes.Add(tMeshDetails); } } else if (obj is AudioClip) { AudioClip clip = (AudioClip)obj; int check = 1; foreach (SoundDetails details in AllSounds) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { SoundDetails tSoundDetails = new SoundDetails(); tSoundDetails.FoundInGameObjects = new List <string>(); tSoundDetails.name = clip.name; tSoundDetails.path = p; tSoundDetails.preview = AssetPreview.GetAssetPreview(clip); tSoundDetails.FoundInGameObjects.Add(path); AllSounds.Add(tSoundDetails); } } else if (obj is MonoScript) { MonoScript script = (MonoScript)obj; int check = 1; foreach (ScriptDetails details in AllScripts) { if (details.path == p) { check = 0; details.FoundInGameObjects.Add(path); break; } } if (check == 1) { ScriptDetails tScriptDetails = new ScriptDetails(); tScriptDetails.FoundInGameObjects = new List <string>(); tScriptDetails.name = script.name; tScriptDetails.path = p; tScriptDetails.FoundInGameObjects.Add(path); AllScripts.Add(tScriptDetails); } } } } } foreach (TextureDetails tTextureDetails in AllTextures) { TotalTextureMemory += tTextureDetails.memSizeBytes; } foreach (MeshDetails tMeshDetails in AllMeshes) { TotalMeshVertices += tMeshDetails.vertexCount; } }