Пример #1
0
        TLYFile GetSaveFile()
        {
            TLYFile t = new TLYFile();

            t.Variables = _Vars.ToArray();

            int id = 0;

            foreach (UCTopologyItem u in pItems.Controls)
            {
                u.Item.Id = id;
                id++;

                t.Items.Add(u.Item.Id, new TLYFile.TopologyItem()
                {
                    Item = u.Item, Position = u.Location
                });
            }

            foreach (ConnectedLine line in _Lines)
            {
                t.Relations.Add(new TLYFile.Relation()
                {
                    From = line.FromItem.Id, To = line.ToItem.Id
                });
            }

            return(t);
        }
Пример #2
0
        void Save(string file)
        {
            TLYFile t = GetSaveFile();

            LasterHelper.SetEnvironmentPath(file);

            t.Save(file);
            LastFile = file;
        }
Пример #3
0
        void generateExeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sv = new SaveFileDialog())
            {
                sv.Filter     = "Exe file|*.exe";
                sv.DefaultExt = "exe";

                if (sv.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string pwd = FCreatePassword.ShowForm();
                if (string.IsNullOrEmpty(pwd))
                {
                    return;
                }

                // Copiar librerias
                if (Path.GetDirectoryName(Application.ExecutablePath) != Path.GetDirectoryName(sv.FileName))
                {
                    if (MessageBox.Show("Do you want to copy dll files to?", "Library files", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        foreach (string file in Directory.GetFiles(Path.GetDirectoryName(Application.ExecutablePath), "*.dll", SearchOption.TopDirectoryOnly))
                        {
                            string dest = Path.Combine(Path.GetDirectoryName(sv.FileName), Path.GetFileName(file));
                            if (dest == file)
                            {
                                continue;
                            }

                            if (File.Exists(dest))
                            {
                                File.Delete(dest);
                            }
                            File.Copy(file, dest);
                        }
                    }
                }

                byte[] hash = Encoding.UTF8.GetBytes(pwd);
                hash = HashHelper.HashRaw(HashHelper.EHashType.Sha512, hash, 0, hash.Length);

                TLYFile      t      = GetSaveFile();
                PacketHeader header = new PacketHeader()
                {
                    H = hash,
                    D = Encoding.UTF8.GetBytes(t.Save())
                };

                header.Encrypt(true);

                // Leer exe original
                byte[] ar = File.ReadAllBytes(Application.ExecutablePath);

                using (FileStream fs = new FileStream(sv.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    // Escribir exe
                    fs.Write(ar, 0, ar.Length);

                    // Escribir contenido comprimido, en UTF8
                    ar = Encoding.UTF8.GetBytes(SerializationHelper.Serialize(header, SerializationHelper.EFormat.Json));
                    ar = CompressHelper.Compress(ar, 0, ar.Length, true);
                    fs.Write(ar, 0, ar.Length);

                    // Grabar tamaño añadido
                    ar = BitConverter.GetBytes(ar.Length);
                    fs.Write(ar, 0, ar.Length);

                    // Grabar palabra clave de fin de archivo
                    ar = Encoding.ASCII.GetBytes("PACK");
                    fs.Write(ar, 0, ar.Length);
                }

                Pr.Process.Start(Path.GetDirectoryName(sv.FileName));
            }
        }
Пример #4
0
        void playToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler(playToolStripMenuItem_Click), sender, e);
                return;
            }

            if (!_InPlay && _Inputs.Count <= 0)
            {
                return;
            }

            _InPlay = !_InPlay;

            foreach (UCTopologyItem c in pItems.Controls)
            {
                c.RefreshInPlay(_InPlay);
            }

            playToolStripMenuItem.Visible = playToolStripMenuItem.Enabled = !_InPlay;
            stopToolStripMenuItem.Visible = stopToolStripMenuItem.Enabled = _InPlay;

            if (!_InPlay)
            {
                _Inputs.Stop();
                tPaintPlay.Enabled = false;

                // Restore variable cache
                foreach (ObjectCache v in _VariableCache)
                {
                    v.Restore();
                }
                _VariableCache.Clear();
                propertyGrid1.Refresh();
            }
            else
            {
                _VariableCache.Clear();
                try
                {
                    TLYFile.RemplaceVariables(_Inputs, _Vars.ToArray(), _VariableCache);
                }
                catch (Exception ex)
                {
                    ITopologyItem_OnException(null, ex);
                    return;
                }

                pError.Visible      = false;
                tPaintPlay.Interval = AreInUse.InUseMillisecons / 2;
                tPaintPlay.Enabled  = true;

                foreach (ConnectedLine l in _Lines)
                {
                    l.AreInUse.Clear();
                }

                Thread th = new Thread(new ThreadStart(StartAsync))
                {
                    IsBackground = true,
                };
                th.SetApartmentState(ApartmentState.STA);
                th.Start();
            }

            pItems.Invalidate(true);
        }
Пример #5
0
        void LoadFile(string fileName)
        {
            TLYFile t = null;

            bool isFile = false;

            try
            {
                if (File.Exists(fileName))
                {
                    t      = TLYFile.LoadFromFile(fileName);
                    isFile = true;
                }
                else
                {
                    t = TLYFile.Load(fileName);
                }
            }
            catch (Exception e)
            {
                ITopologyItem_OnException(null, e);
                return;
            }
            if (t != null)
            {
                NewTopology();

                if (isFile)
                {
                    LasterHelper.SetEnvironmentPath(fileName);
                    LastFile = fileName;
                }
                else
                {
                    LasterHelper.SetEnvironmentPath("");
                    LastFile = "";
                }
                if (t.Variables != null)
                {
                    foreach (Variable v in t.Variables)
                    {
                        _Vars.Add(v.Clone());
                    }
                }

                if (t.Items.Values != null)
                {
                    foreach (TLYFile.TopologyItem i in t.Items.Values)
                    {
                        CreateItem(i.Item, i.Position);
                    }

                    if (t.Relations != null)
                    {
                        foreach (TLYFile.Relation rel in t.Relations)
                        {
                            TLYFile.TopologyItem from, to;
                            if (t.Items.TryGetValue(rel.From, out from) && t.Items.TryGetValue(rel.To, out to) && from != null && to != null)
                            {
                                UCTopologyItem searchFrom = SearchControl(from.Item);
                                UCTopologyItem searchTo   = SearchControl(to.Item);

                                if (searchFrom != null && searchTo != null)
                                {
                                    _Lines.Add(new ConnectedLine()
                                    {
                                        From = searchFrom, To = searchTo
                                    });

                                    if (to.Item is IDataProcess)
                                    {
                                        from.Item.Process.Add((IDataProcess)to.Item);
                                    }
                                }
                            }
                        }
                    }
                }

                pItems.Invalidate();
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            Application.ThreadException += Application_ThreadException;
            // Error al cargar la libería
            ReflectionHelper.RedirectAssembly("Newtonsoft.Json", new Version(9, 0), "30ad4fe6b2a6aeed");

#if DEBUG
            if (Debugger.IsAttached)
            {
                //args = new string[] { "--edit", @"C:\Users\Fernando\Desktop\ejemplo.tly" };
                //args = new string[] { "--install",/* "--name=Laster",*/ @"C:\Users\Fernando\Desktop\ejemplo.tly" };
                //args = new string[] { "--service", @"C:\Users\Fernando\Desktop\ejemplo.tly" };
            }
#endif
            bool          isService = false, isEdit = false;
            List <string> cfgFiles = new List <string>();

            if (args != null && args.Length > 0)
            {
                string name    = "LasterService";
                int    install = 0;
                foreach (string arg in args)
                {
                    if (string.IsNullOrEmpty(arg))
                    {
                        continue;
                    }

                    if (!arg.StartsWith("--") && File.Exists(arg))
                    {
                        cfgFiles.Add(arg);
                    }
                    else
                    {
                        string iz, dr;
                        StringHelper.Split(arg, '=', out iz, out dr);

                        switch (iz)
                        {
                        case "--name": { name = dr.Trim(); break; }

                        case "--service": { isService = true; break; }

                        case "--edit": { isEdit = true; break; }

                        case "--install": { install = 1; break; }

                        case "--uninstall": { install = -1; break; }

                        default:
                        {
                            break;
                        }
                        }
                    }
                }

                if (install != 0)
                {
                    // Requiere acción de instalación
                    if (install == 1)
                    {
                        LasterServiceInstaller.Install(name, cfgFiles.ToArray());
                    }
                    else
                    {
                        LasterServiceInstaller.Uninstall(name);
                    }
                    return;
                }
            }

            bool efects = false;
            Application.OleRequired();
            DataInputCollection inputs = new DataInputCollection();

            // Leer el contenido del final del archivo para ver si contiene una configuración
            byte[] pack = new byte[8];
            using (FileStream fs = new FileStream(Application.ExecutablePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                fs.Seek(fs.Length - pack.Length, SeekOrigin.Begin);
                if (fs.Read(pack, 0, pack.Length) == pack.Length)
                {
                    if (Encoding.ASCII.GetString(pack, pack.Length - 4, 4) == "PACK")
                    {
                        // Sacar el tamaño
                        int l = BitConverter.ToInt32(pack, 0);
                        fs.Seek(fs.Length - pack.Length - l, SeekOrigin.Begin);

                        byte[] data = new byte[l];
                        if (fs.Read(data, 0, l) == l)
                        {
                            // Sacar el contenido
                            data = CompressHelper.Compress(data, 0, l, false);

                            string       json   = Encoding.UTF8.GetString(data);
                            PacketHeader header = SerializationHelper.DeserializeFromJson <PacketHeader>(json);
                            if (header != null)
                            {
                                header.Encrypt(false);

                                json = Encoding.UTF8.GetString(header.D);
                                if (isEdit)
                                {
                                    // Edición
                                    if (!efects)
                                    {
                                        Application.SetCompatibleTextRenderingDefault(true);
                                        Application.EnableVisualStyles();
                                        efects = true;
                                    }

                                    string pwd = FInputText.ShowForm("Edit", "Insert edit password", "", true);
                                    if (!string.IsNullOrEmpty(pwd))
                                    {
                                        byte[] hash = Encoding.UTF8.GetBytes(pwd);
                                        hash = HashHelper.HashRaw(HashHelper.EHashType.Sha512, hash, 0, hash.Length);

                                        for (int x = header.H.Length - 1; x >= 0; x--)
                                        {
                                            if (header.H[x] != hash[x])
                                            {
                                                return;
                                            }
                                        }

                                        cfgFiles.Add(json);
                                    }
                                }
                                else
                                {
                                    // Ejecución

                                    TLYFile file = TLYFile.Load(json);
                                    if (file != null)
                                    {
                                        file.Compile(inputs, Application.ExecutablePath);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Ver si quiere editar o ejecutar una configuración
            if (!isEdit && (inputs == null || inputs.Count == 0))
            {
                foreach (string s in cfgFiles)
                {
                    TLYFile file = TLYFile.LoadFromFile(s);
                    if (file != null)
                    {
                        file.Compile(inputs, s);
                    }
                }
            }

            // Ver si se ejecuta como servicio
            if (isService)
            {
                ITopologyItem.OnException += ITopologyItem_OnException;
                ServiceBase[] ServicesToRun = new ServiceBase[] { new LasterService(inputs) };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                // Ver si se ejecuta o se muestra la edición
                if (inputs != null && inputs.Count > 0)
                {
                    ITopologyItem.OnException += ITopologyItem_OnException;
                    if (inputs.Start())
                    {
                        Application.Run();
                    }
                }
                else
                {
                    if (!efects)
                    {
                        Application.SetCompatibleTextRenderingDefault(true);
                        Application.EnableVisualStyles();
                        efects = true;
                    }

                    // Editar en caso de que haya alguna configuración esa, sino una nueva
                    Application.Run(new FEditTopology(cfgFiles.Count == 0 ? null : cfgFiles[0]));
                }
            }
        }