public static void ClearOutput(Func <string, bool> filter /*= null*/) { //if( filter == null ) // filter = s => s.StartsWith( OutputAssemblyName ) && ( s.EndsWith( ".dll" ) || s.EndsWith( ".pdb" ) ); foreach (var path in GetAllOutputAssemblies(OutputDir, filter)) { if (!IOUtility.IsFileLocked(path)) { File.Delete(path); } } }
/// <summary> /// Compile assembly /// </summary> /// <param name="rebuild"></param> /// <param name="outputAssemblyNameOverride"></param> /// <returns></returns> public static bool Compile(bool rebuild, string outputAssemblyNameOverride = null, string outputDirOverride = null) { var config = new VisualStudioSolutionUtility.BuildConfig(); config.BuildConfiguration = "Release"; //config.BuildConfiguration = ProjectSettings.Get.CSharpEditorBuildConfiguration; //config.BuildConfiguration = BuildConfiguration; config.OutDir = outputDirOverride ?? OutputDir; config.OutputAssemblyName = outputAssemblyNameOverride ?? OutputAssemblyName; if (IOUtility.IsFileLocked(Path.Combine(config.OutDir, config.OutputAssemblyName + ".dll"))) { Log.Error($"\'{config.OutputAssemblyName}.dll\' compilation skipped because output file is locked."); return(false); // throw exception or warn message ? } return(VisualStudioSolutionUtility.BuildSolution(ProjectDir, Name, config, rebuild)); }
/// <summary> /// Compile assembly with unique name (if locked). or skip compilation if up to date. /// </summary> /// <param name="rebuild"></param> /// <returns>unique name if default assembly locked</returns> public static string CompileIfRequired(bool rebuild, bool clearOutput) { /* * Compilation algorithm when launching a player or editor: * * Clear all Project_#id#.dll (for example when you start the editor) * * If for Project.dll compilation is needed (dll outdated or missing): * If Project.dll is locked: * compile and return Project_#id#.dll for loading * else: * compile and return the Project.dll for loading * else: * return Project.dll for loading * * !!!! strange effect: * when you msbuild compile, unused Project_#id#.dll is deleted without any request. * it's ok for us but it's unexpected. */ if (clearOutput) // use regex ? { ClearOutput(s => s.StartsWith(OutputAssemblyName + "_") && (s.EndsWith(".dll") || s.EndsWith(".pdb"))); } string outputAssemblyName = OutputAssemblyName; if (rebuild || CompilationIsRequired()) { string fullPath = Path.Combine(OutputDir, outputAssemblyName + ".dll"); if (File.Exists(fullPath) && IOUtility.IsFileLocked(fullPath)) { outputAssemblyName = Name + "_" + Process.GetCurrentProcess().Id; } Compile(rebuild, outputAssemblyName); } return(outputAssemblyName); }
public void Initialize() { //create cache folder if (!Directory.Exists(CacheFolder)) { Directory.CreateDirectory(CacheFolder); } ////get file paths //string textCachePath = Path.Combine( CacheFolder, textCacheName ); //string cacheAssemblyPath = Path.Combine( CacheFolder, cacheAssemblyName ); //init cache database database = new LiteDatabase(DatabaseFileName); //recompile cache if need to update bool needCompile = false; if (ScriptingCSharpEngine.CanCompileScripts) { //check dll is not in the list of precompiled dlls var compiledDllsCollection = database.GetCollection <DatabaseCompiledAssemblyDllItem>("compiledDlls"); if (compiledDllsCollection.FindOne(Query.EQ("ApplicationType", EngineApp.ApplicationType.ToString())) == null) { needCompile = true; } // if text cache exists and assembly is absent if (!File.Exists(AssemblyFileName)) { needCompile = true; } // don't compile if assembly cache exist but locked. if (needCompile && File.Exists(AssemblyFileName) && IOUtility.IsFileLocked(AssemblyFileName)) { //Log.Info( "Script Cache can not be updated because locked" ); needCompile = false; } } if (needCompile) { var scriptsToCompile = GetScriptsToCompile(); if (scriptsToCompile.Count != 0) { if (CompileAssemblyDll(scriptsToCompile)) { //register dll as compiled var compiledDllsCollection = database.GetCollection <DatabaseCompiledAssemblyDllItem>("compiledDlls"); try { if (compiledDllsCollection.FindOne(Query.EQ("ApplicationType", EngineApp.ApplicationType.ToString())) == null) { compiledDllsCollection.Insert(new DatabaseCompiledAssemblyDllItem { ApplicationType = EngineApp.ApplicationType.ToString() }); } } catch { } } else { //unable to compile //clear database database.DropCollection("scripts"); database.DropCollection("compiledDlls"); //delete dll if exists DeleteAssemblyDllFile(); } } else { DeleteAssemblyDllFile(); } } else { #if DEPLOY //on UWP, Android scripts compiled inside Project.dll if (EngineApp.ProjectAssembly != null) { FillLoadedAssemblyDllTypes(EngineApp.ProjectAssembly); } #else //try load dll if (File.Exists(AssemblyFileName)) { if (!LoadAssemblyDll()) { DeleteAssemblyDllFile(); } } #endif } }
public void Initialize() { //create cacher folder if (!Directory.Exists(CacheFolder)) { Directory.CreateDirectory(CacheFolder); } ////get file paths //string textCachePath = Path.Combine( CacheFolder, textCacheName ); //string cacheAssemblyPath = Path.Combine( CacheFolder, cacheAssemblyName ); //init cache database database = new LiteDatabase(DatabaseFileName); //recompile cache if need to update bool needCompile = false; if (ScriptingCSharpEngine.CanCompileScripts) { //check dll is not in the list of precompiled dlls var compiledDllsCollection = database.GetCollection <CompiledAssemblyDllItem>("compiledDlls"); if (compiledDllsCollection.FindOne(Query.EQ("ApplicationType", EngineApp.ApplicationType.ToString())) == null) { needCompile = true; } //// if text cache changed //if( File.Exists( Path.Combine( CacheFolder, "cache_changed" ) ) ) // needCompile = true; // if text cache exists and assembly is absent if (/*File.Exists( textCachePath ) &&*/ !File.Exists(AssemblyFileName)) { needCompile = true; } // don't compile if assembly cache exist but locked. if (needCompile && File.Exists(AssemblyFileName) && IOUtility.IsFileLocked(AssemblyFileName)) { //Log.Info( "Script Cache can not be updated because locked" ); needCompile = false; } } if (needCompile) { var scriptsCollection = database.GetCollection <DatabaseItem>("scripts"); var scriptsToCompile = new List <string>(scriptsCollection.FindAll().Select(i => FromBase64(i.ScriptBase64))); //var scriptsToCompile = new List<string>( scriptsCollection.FindAll().Select( i => i.ScriptBase64 ) ); if (scriptsToCompile.Count != 0) { if (CompileAssemblyDll(scriptsToCompile)) { //register dll as compiled var compiledDllsCollection = database.GetCollection <CompiledAssemblyDllItem>("compiledDlls"); try { if (compiledDllsCollection.FindOne(Query.EQ("ApplicationType", EngineApp.ApplicationType.ToString())) == null) { compiledDllsCollection.Insert(new CompiledAssemblyDllItem { ApplicationType = EngineApp.ApplicationType.ToString() }); } } catch { } } else { //unable to compile //clear database database.DropCollection("scripts"); database.DropCollection("compiledDlls"); //delete dll if exists DeleteAssemblyDllFile(); } } else { DeleteAssemblyDllFile(); } //try //{ //} //catch( Exception e ) //{ // Log.Info( "Script cache compilation failed. " + e.Message ); // Clear(); // //File.Delete( textCachePath );// clear cache //} //xx xx; //File.Delete( Path.Combine( CacheFolder, "cache_changed" ) ); } else { //try load dll if (File.Exists(AssemblyFileName)) { if (!LoadAssemblyDll()) { DeleteAssemblyDllFile(); } } } }