public Sim_Component(Simulator sim, Effect sim_effect)
 {
     this.sim           = sim;
     this.sim_effect    = sim_effect;
     overlay_effect     = App.content.Load <Effect>("overlay_effect");
     highlight_effect   = App.content.Load <Effect>("UI\\highlight_effect");
     placementtex       = new Texture2D(App.graphics.GraphicsDevice, 81, 81, false, SurfaceFormat.HalfSingle);
     Comp_target        = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
     Highlight_target   = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
     IsEdge_target      = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
     CompType           = new byte[Simulator.SIZEX, Simulator.SIZEY];
     CompGrid           = new int[Simulator.SIZEX / 32, Simulator.SIZEY / 32][];
     CompOverlayGrid    = new List <int> [Simulator.SIZEX / 32, Simulator.SIZEY / 32];
     PinDescGrid        = new List <int> [Simulator.SIZEX / 32, Simulator.SIZEY / 32];
     CompNetwork        = new byte[Simulator.SIZEX, Simulator.SIZEY];
     components         = new Component[10000000];
     pins2check         = new Point[20000000];
     overlaylines       = new VertexPositionLine[1000000];
     emptyComponentID   = new int[10000000];
     CompMayneedoverlay = new List <int>();
     Components_Data    = new List <CompData>();
     string[] Libraries2Load = Directory.GetFiles(@"LIBRARIES\", "*.dcl");
     Sim_INF_DLL.LoadLibrarys(Libraries2Load);
     Sim_INF_DLL.SimFrameStep_maxperframe += DrawAllHighlights;
 }
        public static void OpenFromPath(string filename)
        {
            if (!Simulator.IsSimulating)
            {
                try
                {
                    FileStream stream = new FileStream(filename, FileMode.Open);

                    byte[] intbuffer = new byte[4];

                    stream.Read(intbuffer, 0, 4);
                    int XGridSize = BitConverter.ToInt32(intbuffer, 0);
                    stream.Read(intbuffer, 0, 4);
                    int YGridSize = BitConverter.ToInt32(intbuffer, 0);

                    #region Load Tables and Check for Components

                    stream.Read(intbuffer, 0, 4);
                    int    library_count = BitConverter.ToInt32(intbuffer, 0);
                    string workingPath   = filename;
                    int    pathindex     = 0;
                    for (int i = workingPath.Length - 1; i >= 0; --i)
                    {
                        if (workingPath[i] == '\\')
                        {
                            pathindex = i;
                            break;
                        }
                    }
                    workingPath = workingPath.Remove(pathindex + 1, workingPath.Length - (pathindex + 1));
                    //CompLibrary.AllUsedLibraries.Clear();
                    //Sim_Component.Components_Data.Clear();
                    List <string> Libraries2Load = new List <string>();
                    for (int i = 0; i < library_count; ++i)
                    {
                        string relPath = stream.ReadNullTerminated();
                        if (relPath[0] == '-')
                        {
                            string pathtoexe       = Directory.GetCurrentDirectory();
                            string pathtolibraries = pathtoexe + "\\LIBRARIES\\";
                            string finalrelPath    = relPath.Remove(0, 1);
                            Libraries2Load.Add(pathtolibraries + finalrelPath);
                        }
                        else
                        {
                            string absolutepath = workingPath + relPath;
                            Libraries2Load.Add(absolutepath);
                        }
                        //CompLibrary newlib = new CompLibrary(null, absolutepath);
                        //newlib.Load();
                        //CompLibrary.AllUsedLibraries.Add(newlib);
                    }
                    bool AllLibrarysLoaded = Sim_INF_DLL.LoadLibrarys(Libraries2Load.ToArray());
                    if (!AllLibrarysLoaded)
                    {
                        stream.Close();
                        stream.Dispose();
                        return;
                    }

                    stream.Read(intbuffer, 0, 4);
                    int compdata_count = BitConverter.ToInt32(intbuffer, 0);
                    stream.Read(intbuffer, 0, 4);
                    int        compdatatypes_count = BitConverter.ToInt32(intbuffer, 0);
                    string[]   compdata_names      = new string[compdata_count];
                    List <int> compdatatypes       = new List <int>();
                    for (int i = 0; i < compdata_count; ++i)
                    {
                        compdata_names[i] = stream.ReadNullTerminated();
                    }
                    for (int i = 0; i < compdatatypes_count; ++i)
                    {
                        stream.Read(intbuffer, 0, 4);
                        compdatatypes.Add(BitConverter.ToInt32(intbuffer, 0));
                    }
                    // Check if all Components are loaded
                    bool          AllLoaded          = true;
                    List <string> NotFoundComponents = new List <string>();
                    int[]         compdata_index     = new int[compdata_count];
                    for (int i = 0; i < compdata_count; ++i)
                    {
                        CompData curcomp = Sim_Component.Components_Data.Find(x => x.name == compdata_names[i]);
                        if (curcomp == null && compdatatypes.Exists(x => x == i))
                        {
                            AllLoaded = false;
                            NotFoundComponents.Add(compdata_names[i]);
                        }
                        else
                        {
                            int index = Sim_Component.Components_Data.IndexOf(curcomp);
                            compdata_index[i] = index;
                        }
                    }

                    if (!AllLoaded)
                    {
                        string message = "Following Components not loaded: ";
                        for (int i = 0; i < NotFoundComponents.Count; ++i)
                        {
                            message += NotFoundComponents[i] + " ";
                        }
                        throw new Exception(message);
                    }

                    #endregion

                    Simulator.SIZEX  = Simulator.ProjectSizeX = XGridSize;
                    Simulator.MAXCOO = Simulator.SIZEX - Simulator.BORDERSIZE;

                    Simulator.SIZEY             = Simulator.ProjectSizeY = YGridSize;
                    Simulator.linedrawingmatrix = Matrix.CreateOrthographicOffCenter(0, Simulator.SIZEX + 0.01f, Simulator.SIZEY + 0.01f, 0, 0, 1);
                    Simulator.sim_effect.Parameters["worldsizex"].SetValue(Simulator.SIZEX);
                    Simulator.sim_effect.Parameters["worldsizey"].SetValue(Simulator.SIZEY);

                    Sim_Component.components = new Component[Sim_Component.components.Length];
                    Simulator.networks       = new Network[Simulator.networks.Length];

                    Simulator.IsWire          = new byte[Simulator.SIZEX, Simulator.SIZEY];
                    Simulator.WireIDs         = new int[Simulator.SIZEX / 2, Simulator.SIZEY / 2, Simulator.LAYER_NUM];
                    Simulator.WireIDPs        = new int[Simulator.SIZEX, Simulator.SIZEY];
                    Simulator.CalcGridData    = new byte[Simulator.SIZEX, Simulator.SIZEY];
                    Simulator.CalcGridStat    = new byte[Simulator.SIZEX, Simulator.SIZEY];
                    Simulator.networks        = new Network[10000000];
                    Simulator.emptyNetworkIDs = new int[10000000];

                    Sim_Component.CompGrid        = new int[Simulator.SIZEX / 32, Simulator.SIZEY / 32][];
                    Sim_Component.CompOverlayGrid = new List <int> [Simulator.SIZEX / 32, Simulator.SIZEY / 32];
                    Sim_Component.PinDescGrid     = new List <int> [Simulator.SIZEX / 32, Simulator.SIZEY / 32];

                    Sim_Component.CompNetwork      = new byte[Simulator.SIZEX, Simulator.SIZEY];
                    Sim_Component.components       = new Component[10000000];
                    Sim_Component.pins2check       = new Point[20000000];
                    Sim_Component.overlaylines     = new VertexPositionLine[1000000];
                    Sim_Component.emptyComponentID = new int[10000000];
                    Sim_Component.CompType         = new byte[Simulator.SIZEX, Simulator.SIZEY];

                    Simulator.emptyNetworkIDs_count      = 0;
                    Sim_Component.emptyComponentID_count = 0;
                    Sim_Component.CompMayneedoverlay.Clear();

                    Sim_Component.pins2check_length = 0;
                    Simulator.cursimframe           = 0;

                    Simulator.seclogic_target.Dispose();
                    Simulator.logic_target.Dispose();
                    Simulator.WireCalc_target.Dispose();
                    Sim_Component.Comp_target.Dispose();
                    Sim_Component.Highlight_target.Dispose();
                    Sim_Component.IsEdge_target.Dispose();

                    Simulator.seclogic_target      = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
                    Simulator.logic_target         = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
                    Simulator.WireCalc_target      = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
                    Sim_Component.Comp_target      = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
                    Sim_Component.Highlight_target = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
                    Sim_Component.IsEdge_target    = new RenderTarget2D(App.graphics.GraphicsDevice, Simulator.SIZEX, Simulator.SIZEY, false, SurfaceFormat.HalfSingle, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);

                    Simulator.highestNetworkID = 4;

                    #region LoadWires

                    stream.Read(intbuffer, 0, 4);
                    int wirecount = BitConverter.ToInt32(intbuffer, 0);
                    Simulator.highestNetworkID = 4 + wirecount;
                    for (int i = 4; i < wirecount + 4; ++i)
                    {
                        Network networkbuffer = new Network(i);
                        Simulator.networks[i] = networkbuffer;
                        stream.Read(intbuffer, 0, 4);
                        int linecount = BitConverter.ToInt32(intbuffer, 0);
                        for (int j = 0; j < linecount; ++j)
                        {
                            stream.Read(intbuffer, 0, 1);
                            byte layers = intbuffer[0];
                            stream.Read(intbuffer, 0, 4);
                            int startx = BitConverter.ToInt32(intbuffer, 0);
                            stream.Read(intbuffer, 0, 4);
                            int starty = BitConverter.ToInt32(intbuffer, 0);
                            stream.Read(intbuffer, 0, 4);
                            int endx = BitConverter.ToInt32(intbuffer, 0);
                            stream.Read(intbuffer, 0, 4);
                            int endy = BitConverter.ToInt32(intbuffer, 0);
                            stream.Read(intbuffer, 0, 4);
                            int dirx = BitConverter.ToInt32(intbuffer, 0);
                            stream.Read(intbuffer, 0, 4);
                            int diry = BitConverter.ToInt32(intbuffer, 0);
                            stream.Read(intbuffer, 0, 4);
                            int length = BitConverter.ToInt32(intbuffer, 0);


                            Line_Netw linebuffer = new Line_Netw(new Point(startx, starty), new Point(endx, endy), new Point(dirx, diry), length, layers);
                            networkbuffer.lines.Add(linebuffer);
                        }
                        networkbuffer.PlaceNetwork();
                    }
                    #endregion


                    #region LoadComp

                    stream.Read(intbuffer, 0, 4);
                    int compcount = BitConverter.ToInt32(intbuffer, 0);
                    Sim_Component.nextComponentID = compcount + 1;
                    for (int i = 1; i < compcount + 1; ++i)
                    {
                        stream.Read(intbuffer, 0, 4);
                        Component buffercomp = new Component(compdata_index[BitConverter.ToInt32(intbuffer, 0)], i);
                        stream.Read(intbuffer, 0, 4);
                        int posX = BitConverter.ToInt32(intbuffer, 0);
                        stream.Read(intbuffer, 0, 4);
                        int posY = BitConverter.ToInt32(intbuffer, 0);
                        stream.Read(intbuffer, 0, 1);
                        int rotation = intbuffer[0];
                        stream.Read(intbuffer, 0, 4);
                        int valuebox_length = BitConverter.ToInt32(intbuffer, 0);

                        CompData compdata = Sim_Component.Components_Data[compdata_index[buffercomp.dataID]];
                        for (int j = 0; j < valuebox_length; ++j)
                        {
                            stream.Read(intbuffer, 0, 4);
                            int curval = BitConverter.ToInt32(intbuffer, 0);
                            buffercomp.totalstates[compdata.internalstate_length + compdata.OverlaySeg_length + j] = curval;
                        }

                        Sim_Component.components[i] = buffercomp;
                        buffercomp.Place(new Point(posX, posY), (byte)rotation, true);
                    }
                    Network.Delete(Simulator.FoundNetworks);
                    Simulator.FoundNetworks.Clear();
                    #endregion

                    stream.Close();
                    stream.Dispose();


                    Console.WriteLine("Loading suceeded. Filename: {0}", filename);
                    IsUpToDate = true;
                }
                catch (Exception exp)
                {
                    Console.WriteLine("Loading failed:\n{0}", exp);
                    System.Windows.Forms.MessageBox.Show("Loading failed:\n" + exp.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }