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
            }
        }
Exemple #2
0
            public override bool Creation(NewObjectCell.ObjectCreationContext context)
            {
                if (Window.IsFileCreation())
                {
                    context.disableFileCreation = true;

                    GetCreateCSharpClassInfo(out var csharpRealFileName, out var csharpClassName, out var csharpClassNameWithoutNamespace);

                    try
                    {
                        //main file
                        {
                            string name       = Template.Name + ".scene";
                            var    sourceFile = VirtualPathUtility.GetRealPathByVirtual(@"Base\Tools\NewResourceTemplates\" + name);

                            //copy scene file

                            var text = File.ReadAllText(sourceFile);

                            if (CreateCSharpClass)
                            {
                                text = text.Replace(".component NeoAxis.Component_Scene", ".component " + csharpClassName);
                            }

                            File.WriteAllText(context.fileCreationRealFileName, text);

                            //copy additional folder if exist
                            var sourceFolderPath = sourceFile + "_Files";
                            if (Directory.Exists(sourceFolderPath))
                            {
                                var destFolderPath = context.fileCreationRealFileName + "_Files";
                                IOUtility.CopyDirectory(sourceFolderPath, destFolderPath);
                            }
                        }

                        //cs file
                        if (CreateCSharpClass)
                        {
                            string code = @"using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using NeoAxis;

namespace Project
{
	public class {Name} : {Base}
	{
	}
}";

                            code = code.Replace("{Name}", csharpClassNameWithoutNamespace);
                            code = code.Replace("{Base}", Window.SelectedType.Name);

                            File.WriteAllText(csharpRealFileName, code);
                        }
                    }
                    catch (Exception e)
                    {
                        EditorMessageBox.ShowWarning(e.Message);
                        //Log.Warning( e.Message );
                        return(false);
                    }

                    if (CreateCSharpClass)
                    {
                        //add to Project.csproj
                        {
                            var toAdd = new List <string>();

                            var fileName = Path.Combine("Assets", VirtualPathUtility.GetVirtualPathByReal(csharpRealFileName));
                            toAdd.Add(fileName);

                            if (CSharpProjectFileUtility.UpdateProjectFile(toAdd, null, out var error))
                            {
                                if (toAdd.Count > 1)
                                {
                                    Log.Info(EditorLocalization.Translate("General", "Items have been added to the Project.csproj."));
                                }
                                else
                                {
                                    Log.Info(EditorLocalization.Translate("General", "The item has been added to the Project.csproj."));
                                }
                            }
                            else
                            {
                                EditorMessageBox.ShowWarning(error);
                                //Log.Warning( error );
                                return(false);
                            }
                        }

                        Window.DisableUnableToCreateReason = true;

                        //restart application
                        var text = EditorLocalization.Translate("General", "To apply changes need restart the editor. Restart?\r\n\r\nThe editor must be restarted to compile and enable a new created C# class.");
                        if (EditorMessageBox.ShowQuestion(text, EMessageBoxButtons.YesNo) == EDialogResult.Yes)
                        {
                            EditorAPI.BeginRestartApplication();
                        }

                        Window.DisableUnableToCreateReason = false;
                    }
                }

                return(true);
            }
Exemple #3
0
        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();
                    }
                }
            }
        }