Beispiel #1
0
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, LayerMask.GetMask("Planet")))
            {
                string  planetName = hit.transform.name;
                Vector3 pos        = PlanetPos(hit);

                info = GetSetDataInfo(questDate, planetName);

                planetcanvas.transform.position = pos;
                text.text = info.QuestContent;
            }
        }
    }
Beispiel #2
0
        private void GenerateDataClasses(Database db, List <Type> types, bool singleClass = false, bool recompile = false)
        {
            Sys.Trace("GenerateDataClasses. types.Count = " + types.Count);
            if (types.Count == 0)
            {
                return;
            }

            var erros = new StringBuilder();

            Sys.Trace("db.Provider.GetLastModified()");

            string lastModified = db.Provider.GetLastModified();
            string dir          = GetDirectory(types, db, lastModified);
            string fileCode     = Path.Combine(dir, "Speed.Code.cs");
            string fileDll      = Path.Combine(dir, "Speed." + types[0].Assembly.GetName().Name + ".dll");

            if (!recompile && lastModified != null)
            {
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                Sys.Trace("GetDirectory: " + dir);
                Sys.Trace("fileDll: " + fileDll);

                if (File.Exists(fileDll))
                {
                    Sys.Trace("fileDll Exists");
                    try
                    {
                        var assCache = Assembly.LoadFile(fileDll);

                        var typesDataClass = assCache.GetExportedTypes();

                        foreach (var type in types)
                        {
                            addToCache(type, (DataClass)assCache.CreateInstance("DataClass" + type.Name));
                        }

                        Sys.Trace("fileDll carregada do cache");
                        return;
                    }
                    catch (Exception ex)
                    {
                        Sys.Trace(ex, "Erro ao carregar do cache");
                        //throw;
                        // deu erro? continua e recompila
                    }
                }
            }

            TimerCount tc = new TimerCount("TestSelect");

            Dictionary <Type, dataInfo> dataInfos = new Dictionary <Type, dataInfo>();
            // other usings
            Dictionary <string, string> otherUsings = new Dictionary <string, string>();

            List <string> codes = new List <string>();

            int i = 0;

            foreach (var type in types)
            {
                try
                {
                    DbTableInfo table;
                    var         infos = new Dictionary <string, DbColumnInfo>();
                    string      className;
                    string      tableName;
                    if (type.Name == "VwSisLog")
                    {
                        type.ToString();
                    }
                    string text = CodeGenerator.GenerateDataClassCode(db, type, out className, out tableName,
                                                                      out table, out infos, otherUsings);
                    if (text != null)
                    {
                        codes.Add(text);

                        dataInfo di = new dataInfo {
                            Table = table, Infos = infos, TableName = tableName, ClassName = className
                        };
                        dataInfos.Add(type, di);
                    }
                }
                catch (Exception ex)
                {
                    erros.AppendFormat("Error generating class for type: " + type.Name + " - " + ex.Message);
                }
                i++;
            }

            if (erros.Length > 0)
            {
                Sys.Trace("Erros: " + erros.ToString());
                Database.Errors.Add(erros.ToString());
            }

            StringBuilder b = new StringBuilder();

            b.AppendLine(DataClassTemplate.DATACLASSTEMPLATE_USING);
            foreach (var pair in otherUsings)
            {
                b.AppendLine(pair.Key);
            }
            codes.ForEach(p => b.AppendLine(p));

#if DEBUG
            string code = b.ToString();
#else
            string code = string.Join("\r\n", b.ToString().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Where(line => !string.IsNullOrWhiteSpace(line) && !line.Trim().StartsWith("//")).Select(line => line.Trim()));
#endif
            b.Clear();

            Sys.Trace("code gerado");

            Assembly ass = null;

#if DEBUG
            //Descomentar somente se for necessário. Impacta muito a performance
            try
            {
                string dirAc = Path.Combine(@"..\..\DataClass", types[0].Namespace);
                if (!System.IO.Directory.Exists(dirAc))
                {
                    System.IO.Directory.CreateDirectory(dirAc);
                }
                using (System.IO.StreamWriter w = new System.IO.StreamWriter(Path.Combine(dirAc, "AllClasses.cs"), false))
                    w.Write(code);
            }
            catch { }
#endif

            // para aplicações web, Assembly.GetEntryAssembly() sempre retorna null
            //if (Assembly.GetEntryAssembly() != null)
            //{
            tc.Next("Compile");

            bool hasCache = false;

            Sys.Trace("Compile");
            if (hasCache)
            {
                try
                {
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    else
                    {
                        if (File.Exists(fileCode) && File.Exists(fileDll))
                        {
                            if (File.ReadAllText(fileCode) == code)
                            {
                                try
                                {
                                    ass      = Assembly.LoadFile(fileDll);
                                    hasCache = true;
                                }
                                catch (Exception ex)
                                {
                                    Sys.Trace(ex, "asm1");
                                    ass = null;
                                    ex.ToString();
                                }
                            }
                        }
                    }
                    if (ass == null)
                    {
                        if (File.Exists(fileDll))
                        {
                            File.Delete(fileDll);
                        }

                        Sys.Trace("Compilando em disco");
                        ass = CodeGenerator.Compile(db, types[0], code, "All Classes", fileDll, otherUsings);
                        File.WriteAllText(fileCode, code);
                        Sys.Trace("Compilando no disco: " + fileDll);
                    }
                }
                catch (Exception ex)
                {
                    Sys.Trace(ex, "asm2");
                    ass = null;
                }
            }

            if (ass == null)
            {
                Sys.Trace("Compilando na memória");
                // compila na memória
                if (!singleClass)
                {
                    ass = CodeGenerator.Compile(db, types[0], code, "All Classes", fileDll, otherUsings);
                }
                else
                {
                    ass = CodeGenerator.Compile(db, types[0], code, types[0].FullName, null, otherUsings);
                }
                Sys.Trace("Compilado na memória: " + fileDll);
            }

#if DEBUG2
            var exTypes = ass.GetExportedTypes().Where(p => p.Name.StartsWith("DataClass")).ToList();
#endif


            tc.Next("Depois");
            lock (cache)
            {
                foreach (var pair in dataInfos)
                {
                    Type     type = pair.Key;
                    dataInfo info = pair.Value;
                    if (cache.ContainsKey(type))
                    {
                        cache.Remove(type);
                    }
                    if (cacheByName.ContainsKey(type.Name))
                    {
                        cacheByName.Remove(type.Name);
                    }

                    Type objType = ass.GetType(info.ClassName);

                    if (objType == null)
                    {
                        Database.Errors.Add("Not found in the database: " + type.Name + " - " + info.TableName);
                        continue;
                    }

                    DataClass dc = (DataClass)Activator.CreateInstance(objType);
                    if (dc == null)
                    {
                        continue;
                    }
                    dc.TableInfo   = info.Table;
                    dc.ColumnInfos = info.Infos;
                    addToCache(type, dc);
                }
            }
            string time = tc.ToString();
        }