private static string ResolveFilePath(MyAbstractMemoryBlock memoryBlock, string globalDataFolder)
        {
            string fileName = GetTempStorage(memoryBlock.Owner.Owner) + "\\" + GetNodeFolder(memoryBlock.Owner) + "\\" + GetFileName(memoryBlock);

            if (!File.Exists(fileName))
            {
                MyWorkingNode node = memoryBlock.Owner as MyWorkingNode;

                if (!string.IsNullOrEmpty(node.DataFolder))
                {
                    fileName = node.DataFolder;
                }
                else if (!string.IsNullOrEmpty(globalDataFolder))
                {
                    fileName = globalDataFolder + "\\" + GetNodeFolder(memoryBlock.Owner);
                }
                else
                {
                    throw new FileNotFoundException(
                              "File not found in temporal folder and no data folder defined: " + fileName);
                }

                fileName += "\\" + GetFileName(memoryBlock);
            }

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("Memory block file not found: " + fileName);
            }

            return(fileName);
        }
        public void SaveBlock(MyAbstractMemoryBlock memoryBlock)
        {
            int   length = buffer.Length;
            SizeT size   = memoryBlock.GetSize();

            while (size > length)
            {
                length *= 2;
            }

            if (length != buffer.Length)
            {
                buffer = new byte[length];
            }
            memoryBlock.GetBytes(buffer);

            string tempFolder = GetTempStorage(memoryBlock.Owner.Owner) + "\\" + GetNodeFolder(memoryBlock.Owner);

            Directory.CreateDirectory(tempFolder);

            string filePath = tempFolder + "\\" + GetFileName(memoryBlock);

            try
            {
                BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create));

                writer.Write(buffer, 0, size);
                writer.Close();
            }
            catch (Exception e)
            {
                MyLog.WARNING.WriteLine("Memory block saving failed (" + memoryBlock.Owner.Name + "." + memoryBlock.Name + "): " + e.Message);
            }
        }
        public void SaveBlock(MyAbstractMemoryBlock memoryBlock)
        {
            int   length = m_buffer.Length;
            SizeT size   = memoryBlock.GetSize();

            while (size > length)
            {
                length *= 2;
            }

            if (length != m_buffer.Length)
            {
                m_buffer = new byte[length];
            }
            memoryBlock.GetBytes(m_buffer);

            string tempFolder = GetTempStorage(memoryBlock.Owner.Owner) + "\\" + GetNodeFolder(memoryBlock.Owner);

            Directory.CreateDirectory(tempFolder);

            string filePath = tempFolder + "\\" + GetFileName(memoryBlock);

            try
            {
                using (var writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
                {
                    writer.Write(m_buffer, 0, size);
                }
            }
            catch (Exception e)
            {
                MyLog.WARNING.WriteLine("Memory block saving failed (node: {0} (id: {1}), block: {2}): {3}", memoryBlock.Owner.Name,
                                        memoryBlock.Owner.Id, memoryBlock.Name, e.Message);
            }
        }
Пример #4
0
        public void RemoveBlock(MyNode holder, MyAbstractMemoryBlock block)
        {
            if (m_memoryBlocks.ContainsKey(holder))
            {
                m_memoryBlocks[holder].Remove(block);

                if (m_memoryBlocks[holder].Count == 0)
                {
                    m_memoryBlocks.Remove(holder);
                }
            }
        }
Пример #5
0
        internal MyAbstractMemoryBlock CreateMemoryBlock(MyNode holder, Type blockType)
        {
            MyAbstractMemoryBlock block = (MyAbstractMemoryBlock)Activator.CreateInstance(blockType, true);

            block.Owner = holder;

            if (!m_memoryBlocks.ContainsKey(holder))
            {
                m_memoryBlocks[holder] = new List <MyAbstractMemoryBlock>();
            }
            m_memoryBlocks[holder].Add(block);

            return(block);
        }
        public void LoadBlock(MyAbstractMemoryBlock memoryBlock, string globalDataFolder)
        {
            int   length = m_buffer.Length;
            SizeT size   = memoryBlock.GetSize();

            while (size > length)
            {
                length *= 2;
            }

            if (length != m_buffer.Length)
            {
                m_buffer = new byte[length];
            }

            try
            {
                string filePath = ResolveFilePath(memoryBlock, globalDataFolder);
                long   fileSize = new FileInfo(filePath).Length;

                if (fileSize != size)
                {
                    throw new InvalidDataException("Different size of a stored memory block (" + fileSize + " B != " + size + " B)");
                }

                using (var reader = new BinaryReader(File.OpenRead(filePath)))
                {
                    reader.Read(m_buffer, 0, size);
                }

                memoryBlock.Fill(m_buffer);
            }
            catch (Exception e)
            {
                MyLog.WARNING.WriteLine("Memory block loading failed (node: {0} (id: {1}), block: {2}): {3}", memoryBlock.Owner.Name,
                                        memoryBlock.Owner.Id, memoryBlock.Name, e.Message);
            }
        }
        public void LoadBlock(MyAbstractMemoryBlock memoryBlock, string globalDataFolder)
        {
            int   length = buffer.Length;
            SizeT size   = memoryBlock.GetSize();

            while (size > length)
            {
                length *= 2;
            }

            if (length != buffer.Length)
            {
                buffer = new byte[length];
            }

            try
            {
                string filePath = ResolveFilePath(memoryBlock, globalDataFolder);
                long   fileSize = new FileInfo(filePath).Length;

                if (fileSize != size)
                {
                    throw new InvalidDataException("Different size of a stored memory block (" + fileSize + " B != " + size + " B)");
                }

                BinaryReader reader = new BinaryReader(File.OpenRead(filePath));
                reader.Read(buffer, 0, size);
                reader.Close();

                memoryBlock.Fill(buffer);
            }
            catch (Exception e)
            {
                MyLog.WARNING.WriteLine("Memory block loading failed (" + memoryBlock.Owner.Name + "." + memoryBlock.Name + "): " + e.Message);
            }
        }
 public static string GetUniqueName(MyAbstractMemoryBlock memoryBlock)
 {
     return(memoryBlock.Owner.Id.ToString() + "#" + memoryBlock.Name);
 }
 public static string GetFileName(MyAbstractMemoryBlock memoryBlock)
 {
     return(memoryBlock.Name + ".mb");
 }
Пример #10
0
 internal abstract void ApplyAttribute(MyAbstractMemoryBlock myAbstractMemoryBlock);