Example #1
0
        public async Task ReadsFile()
        {
            // Make sure we read and write the same filename.
            string guid = string.Format("{0}", Guid.NewGuid());

            // Write a file so we can read it back.
            await WritesFile(guid);

            // Manually set the test credential; in the real-world, QBO will store them in it's CredentialCache.
            var dropbox = new Dropbox(Config, null)
            {
                Credential = credential
            };

            // CacheStream creates a temp file, and deletes the file upon closing. You can debug and inspect it if you want.
            using (var stream = new CacheStream())
            {
                var info = await dropbox.ReadAsync(stream, string.Format("HelloDropbox.{0}.html", guid));

                // Make sure we get info back
                Assert.IsNotNull(info);
                // Make sure there is content.
                Assert.IsTrue(info.Length > 0);
            }
        }
Example #2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int len = 0;

            //If cache is not empty, read from cache
            if (CacheStream.Length > CacheStream.Position)
            {
                len = CacheStream.Read(buffer, offset, count);
                return(len);
            }

            if (NetworkStream == null)
            {
                throw new TcpRemoteCloseException("Remote connection closed");
            }

            //Fill cache
            len = NetworkStream.Read(_Buf, 0, BUF_SIZE);

            if (len == 0)
            {
                return(0);
            }

            CacheStream.Position = 0;
            CacheStream.Write(_Buf, 0, len);
            CacheStream.SetLength(len);
            CacheStream.Position = 0;

            len = CacheStream.Read(buffer, offset, count);

            return(len);
        }
Example #3
0
        public ShaderProperties( )
        {
            InitializeComponent( );

            using (_cache = CacheStream.Open(Path.Combine(Local.MapsDirectory, "ascension.map")))
            {
                dockPanel1.Theme = new VS2013BlueTheme( );
                var asmEditor          = new AsmEditor("Test.glsl");
                var tagList            = new TagList( );
                var shaderPropertyGrid = new ShaderPropertyGrid( );
                tagList.Load(_cache.Index.Where(TagClass.Vrtx).ToList( ));
                tagList.NodeMouseClick +=
                    (TreeNodeMouseClickEventHandler) delegate(object sender, TreeNodeMouseClickEventArgs e)
                {
                    var node = e.Node as TagTreeNode;
                    if (node != null)
                    {
                        using (var cache = CacheStream.Open(Path.Combine(Local.MapsDirectory, "ascension.map"))
                               )
                        {
                            shaderPropertyGrid.DisplayVertexConstants(node.Info, cache);
                            DisplayVertexInstructions(node.Info, cache);
                        }
                    }
                };
                asmEditor.Show(dockPanel1, DockState.Document);
                tagList.Show(dockPanel1, DockState.DockRight);
                shaderPropertyGrid.Show(dockPanel1, DockState.DockLeft);
            }
        }
        private void UploadCache(Stream sourceStream)
        {
            using var cache = new CacheStream(_file, sourceStream, _cloud.Settings.DeduplicateRules);

            if (cache.Process())
            {
                Logger.Debug($"Uploading [{cache.DataCacheName}] {_file.FullPath}");

                OnFileStreamSent();

                _file.Hash = _cloudFileHasher.Hash;
                bool added = _cloud.AddFileInCloud(_file, ConflictResolver.Rewrite)
                             .Result
                             .Success;

                if (!added)
                {
                    UploadFull(cache.Stream, false);
                }
            }
            else
            {
                UploadFull(sourceStream);
            }
        }
Example #5
0
        public void DisplayVertexInstructions(TagDatum vertexDatum, CacheStream cache)
        {
            var vertexBlock = ( VertexShaderBlock )cache.Deserialize(vertexDatum.Identifier);
            var code        = vertexBlock.GeometryClassifications[0].Code;
            var altCode     = vertexBlock.GeometryClassifications[0].CompiledShader;
            var strBuilder  = new StringBuilder();

            foreach (var s in altCode)
            {
                strBuilder.Append("$ " + s + " ");
            }
            strBuilder.AppendLine();
            if (code.Length < 4)
            {
                textEditorControl1.Text = strBuilder.ToString();
                textEditorControl1.Refresh(  );
                return;
            }
            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(code)))
            {
                var version = binaryReader.ReadInt16( );
                strBuilder.AppendLine("#" + version);
                var instructionCount = binaryReader.ReadInt16( );
                for (int i = 0; i < instructionCount; i++)
                {
                    var instructionBytes = binaryReader.ReadBytes(16);
                    var instruction      = new VertexProgramInstruction(instructionBytes);
                    strBuilder.AppendLine(instruction.ToAsm);
                }
            }
            textEditorControl1.Text = strBuilder.ToString( );
            textEditorControl1.Refresh();
        }
Example #6
0
        private void LoadMap(string fileName)
        {
            Map = new CacheStream(fileName);

            listBox1.Items.Clear();
            listBox1.Items.AddRange(Map.Index.Where(x => x.Class.ToString() == "hlmt").Select(x => (object)x).ToArray());
            listBox1.DisplayMember = "Path";
            listBox1.SelectedIndex = listBox1.Items.Count > 0 ? 0 : -1;
        }
 public CacheOutputStream(CacheStream stream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     stream.BytesRead += (o, e) => AddToQueue(e.Buffer);
     stream.Closing   += (o, e) => sourceIsClosed = true;
 }
Example #8
0
        public void Test_CacheStream()
        {
            //for (int i = 0; i < 10; i++)
            Parallel.For(0, 32, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 64
            }, i =>
            {
                //using (MemoryStream stream = new MemoryStream())
                using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    using (CacheStream stream = new CacheStream(bufferStream, 1024, _bufferManager))
                    {
                        byte[] buffer = _bufferManager.TakeBuffer(1024 * 1024); //new byte[_random.Next(128, 1024 * 1024 * 10)];
                        long seek     = _random.Next(64, buffer.Length);
                        //long seek = 0;

                        _random.NextBytes(buffer);

                        stream.Write(buffer, 0, buffer.Length);
                        stream.Position = seek;

                        byte[] buff2 = _bufferManager.TakeBuffer(buffer.Length); //new byte[buffer.Length];
                        stream.Read(buff2, (int)seek, buff2.Length - (int)seek);

                        if (!CollectionUtilities.Equals(buffer, (int)seek, buff2, (int)seek, buffer.Length - (int)seek))
                        {
                            Assert.IsTrue(CollectionUtilities.Equals(buffer, (int)seek, buff2, (int)seek, buffer.Length - (int)seek));
                        }

                        _bufferManager.ReturnBuffer(buffer);
                        _bufferManager.ReturnBuffer(buff2);
                    }
            });

            using (MemoryStream mstream = new MemoryStream())
                using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    using (CacheStream stream = new CacheStream(bufferStream, 1024, _bufferManager))
                    {
                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            var v = (byte)_random.Next(0, 255);
                            mstream.WriteByte(v);
                            stream.WriteByte(v);
                        }

                        mstream.Seek(0, SeekOrigin.Begin);
                        stream.Seek(0, SeekOrigin.Begin);

                        Assert.IsTrue(mstream.Length == stream.Length);

                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            Assert.IsTrue(mstream.ReadByte() == stream.ReadByte());
                        }
                    }
        }
Example #9
0
        /// <summary>
        /// This methods inserts data in the cache using cache stream.
        /// </summary>
        /// <param name="key"> The key against which stream will be written. </param>
        /// <param name="writeBuffer"> data that will be written in the stream. </param>
        private static void WriteUsingStream(string key, byte[] writeBuffer)
        {
            // Declaring NCacheStream
            CacheStream stream = _cache.GetCacheStream(key, StreamMode.Write);

            stream.Write(writeBuffer, 0, writeBuffer.Length);
            stream.Close();

            Console.WriteLine("Stream written to cache.");
        }
Example #10
0
        public void DisplayVertexConstants(TagDatum vertexTag, CacheStream cache)
        {
            var items = GetVertexConstants(vertexTag, cache);

            items.Sort((u, v) => u.bytes[0].CompareTo(v.bytes[0]));
            var source = new BindingSource {
                DataSource = items
            };

            dataGridView1.DataSource = source;
        }
Example #11
0
        /// <summary>
        /// This method fetches data from the cache using streams.
        /// </summary>
        /// <param name="key"> The key of the stream that needs to be fetched from the cache. </param>
        private static void ReadUsingStream(string key)
        {
            byte[] readBuffer = new byte[1024];
            CacheStreamAttributes cacheStreamAttributes = new CacheStreamAttributes(StreamMode.Read);
            // StramMode.Read allows only simultaneous reads but no writes!
            CacheStream stream = _cache.GetCacheStream(key, cacheStreamAttributes);
            // Now you have stream perform operations on it just like any regular stream.
            var readCount = stream.Read(readBuffer, 0, readBuffer.Length);

            stream.Close();

            Console.WriteLine("Bytes read = " + readCount);
        }
Example #12
0
 private void ResolveRequestCache(object sender, System.EventArgs e) {
   // is the url in the cache?
   if (mHash.Contains(mApplication.Request.Url)) {
     // Write it back from the cache
     mApplication.Response.Write(mHash[mApplication.Request.Url].ToString());
     // Finish the request
     mApplication.CompleteRequest();
   } else {
     // Create a new filter
     CacheStream mStreamFilter = new CacheStream(mApplication.Response.Filter);
     // Insert it onto the page
     mApplication.Response.Filter = mStreamFilter;
     // Save a reference to the filter in the request context so we can grab it in UpdateRequestCache
     mApplication.Context.Items.Add("mStreamFilter", mStreamFilter);
   }
 }
Example #13
0
        public MainForm( )
        {
            InitializeComponent( );

            dockPanel1.Theme = new VS2013BlueTheme( );


            var      fileName = Path.Combine(Local.MapsDirectory, "05b_deltatowers.nomap");
            CacheKey key;

            var directory = Path.GetDirectoryName(fileName);

            if (directory != null)
            {
                var maps         = Directory.GetFiles(directory, "*.map", SearchOption.TopDirectoryOnly);
                var resourceMaps = maps.GroupBy(
                    Halo2.CheckMapType
                    ).Where(x => x.Key == MapType.Shared || x.Key == MapType.MainMenu ||
                            x.Key == MapType.SinglePlayerShared)
                                   .Select(g => g.First()).ToList();
                resourceMaps.ForEach(x =>
                                     Solution.Index.AddCache(CacheStream.Open(x)));
            }
            Solution.Index.AddCache(CacheStream.Open(fileName));
            _cacheStream = CacheStream.Open(fileName);

            dockPanel1.DockBottomPortion = 350f;

            _sceneView = new SceneView( );

            _moonfxshExplorerForm = new MoonfxshExplorer();

            _sceneView.SceneInitialized += delegate
            {
                _moonfxshExplorerForm.LoadTags(_cacheStream.ToArray());
                Solution.SetScenario(( ScenarioBlock )_cacheStream.Index.ScenarioIdent.Get(_cacheStream.GetKey( )));

                _sceneView.Scene.OnFrameReady += delegate
                {
                    this.Text = $@"{1/_sceneView.SceneClock.frameTime :#.###} Update:{ _sceneView.SceneClock.updateTime}";
                };
            };

            _moonfxshExplorerForm.TagItemDoubleClick += (sender, reference) => EditTag(reference);
            _sceneView.Show(dockPanel1, DockState.Document);
            _moonfxshExplorerForm.Show(dockPanel1, DockState.DockBottom);
        }
Example #14
0
        private List <VertexConstantInfo> GetVertexConstants(TagDatum vertexTag, CacheStream cache)
        {
            if (!cache.Index.Contains(vertexTag))
            {
                return(new List <VertexConstantInfo>( ));
            }

            var vertexConstantInfos = new List <VertexConstantInfo>( );

            foreach (var tagDatum in cache.Index.Where(TagClass.Spas))
            {
                var block      = ( ShaderPassBlock )cache.Deserialize(tagDatum.Identifier);
                var usesVertex =
                    block.PostprocessDefinition[0].Implementations.Any(
                        u => u.VertexShader.Ident == vertexTag.Identifier);
                if (!usesVertex)
                {
                    continue;
                }
                foreach (var templateDatum in cache.Index.Where(TagClass.Stem))
                {
                    var templateBlock = ( ShaderTemplateBlock )cache.Deserialize(templateDatum.Identifier);
                    var usesPass      =
                        templateBlock.PostprocessDefinition[0].Passes.Any(
                            v => v.Pass.Ident == tagDatum.Identifier);
                    if (!usesPass)
                    {
                        continue;
                    }
                    foreach (var shaderDatum in cache.Index.Where(TagClass.Shad))
                    {
                        var shaderBlock  = ( ShaderBlock )cache.Deserialize(shaderDatum.Identifier);
                        var usesTemplate = shaderBlock.Template.Ident == templateDatum.Identifier;
                        if (usesTemplate)
                        {
                            var items = GetVertexConstants(shaderBlock, templateBlock, block, vertexTag, tagDatum,
                                                           templateDatum,
                                                           shaderDatum, cache);
                            vertexConstantInfos.AddRange(items);
                        }
                    }
                }
            }
            return(vertexConstantInfos);
        }
Example #15
0
        public void LoadScenarioPallet(CacheStream cacheStream)
        {
            var scenery  = cacheStream.Index.Where(TagClass.Scen);
            var crates   = cacheStream.Index.Where(TagClass.Bloc);
            var weapons  = cacheStream.Index.Where(TagClass.Weap);
            var netgame  = cacheStream.Index.Where(TagClass.Itmc);
            var machines = cacheStream.Index.Where(TagClass.Mach);

            AddListViewItems(scenery, new ListViewGroup(
                                 "Scenery", HorizontalAlignment.Left));
            AddListViewItems(crates, new ListViewGroup(
                                 "Crates", HorizontalAlignment.Left));
            AddListViewItems(weapons, new ListViewGroup(
                                 "Weapons", HorizontalAlignment.Left));
            AddListViewItems(netgame, new ListViewGroup(
                                 "NetGame", HorizontalAlignment.Left));
            AddListViewItems(machines, new ListViewGroup(
                                 "Machines", HorizontalAlignment.Left));
        }
Example #16
0
        private void DisplayVertexInstructions(TagDatum vertexDatum, CacheStream cache)
        {
            foreach (var documentsDockContent in documentsDockContents)
            {
                documentsDockContent.DockHandler.DockPanel = null;
                documentsDockContent.Dispose(  );
            }
            var vertexBlock = ( VertexShaderBlock )cache.Deserialize(vertexDatum.Identifier);

            for (var i = 0; i < vertexBlock.GeometryClassifications.Length; i++)
            {
                var asmEditor  = new AsmEditor($"vertex_{i}.glsl");
                var code       = vertexBlock.GeometryClassifications[i].Code;
                var altCode    = vertexBlock.GeometryClassifications[i].CompiledShader;
                var strBuilder = new StringBuilder( );
                foreach (var s in altCode)
                {
                    strBuilder.Append("$ " + s + " ");
                }
                strBuilder.AppendLine( );
                if (code.Length < 4)
                {
                    asmEditor.SetText(strBuilder.ToString( ));
                    return;
                }
                using (var binaryReader = new BinaryReader(new MemoryStream(code)))
                {
                    var version = binaryReader.ReadInt16( );
                    strBuilder.AppendLine("#" + version);
                    var instructionCount = binaryReader.ReadInt16( );
                    for (var index = 0; index < instructionCount; index++)
                    {
                        var instructionBytes = binaryReader.ReadBytes(16);
                        var instruction      = new VertexProgramInstruction(instructionBytes);
                        strBuilder.AppendLine(instruction.ToAsm);
                    }
                }
                documentsDockContents.Add(asmEditor);
                asmEditor.SetText(strBuilder.ToString( ));
                asmEditor.Show(dockPanel1, DockState.Document);
            }
        }
Example #17
0
        public void LoadMaterials(IList <GlobalGeometryMaterialBlock> materials, CacheStream cacheStream,
                                  IList <int> indices = null)
        {
            //for ( var index = 0; index < materials.Count; index++ )
            //{
            //    var globalGeometryMaterialBlock = materials[ index ];
            //    var shaderBlock = globalGeometryMaterialBlock.Shader.Get<ShaderBlock>( );
            //    ShaderPostprocessBitmapNewBlock[] textures;
            //    var material = new MaterialShader( shaderBlock, cacheStream, out textures );

            //    foreach ( var shaderPostprocessBitmapNewBlock in textures )
            //    {
            //        LoadTextureGroup(shaderPostprocessBitmapNewBlock.BitmapGroup);
            //    }

            //    Materials[
            //        indices != null && index < indices.Count
            //            ? ( TagIdent ) indices[ index ]
            //            : globalGeometryMaterialBlock.Shader.Ident ] = material;
            //}
        }
Example #18
0
        public static IEnumerable <CacheStream> GetAllMaps()
        {
            var filenames = Directory.GetFiles(Local.MapsDirectory, "*.map", SearchOption.AllDirectories);

            foreach (var filename in filenames)
            {
                CacheStream testmap = null;
                try
                {
                    testmap = new CacheStream(filename);
                }
                catch (InvalidDataException)
                {
                    continue;
                }
                using (testmap)
                {
                    yield return(testmap);
                }
            }
        }
Example #19
0
        public async Task WritesFile(string guid = null)
        {
            if (guid == null)
            {
                guid = string.Format("{0}", Guid.NewGuid());
            }

            // Manually set the test credential; in the real-world, QBO will store them in it's CredentialCache.
            var dropbox = new Dropbox(Config, null)
            {
                Credential = credential
            };

            using (var stream = new CacheStream("Samples/HelloDropbox.html", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                // Make sure we have a unique file, so folks can see the new file in Dropbox
                var info = await dropbox.WriteAsync(stream, string.Format("HelloDropbox.{0}.html", guid));

                Assert.IsNotNull(info);
            }
        }
Example #20
0
        public override Stream Export(BufferManager bufferManager)
        {
            using (DeadlockMonitor.Lock(this.ThisLock))
            {
                List <Stream> streams  = new List <Stream>();
                Encoding      encoding = new UTF8Encoding(false);

                // Command
                if (this.Command != null)
                {
                    BufferStream bufferStream = new BufferStream(bufferManager);
                    bufferStream.SetLength(5);
                    bufferStream.Seek(5, SeekOrigin.Begin);

                    using (CacheStream cacheStream = new CacheStream(bufferStream, 1024, true, bufferManager))
                        using (StreamWriter writer = new StreamWriter(cacheStream, encoding))
                        {
                            writer.Write(this.Command);
                        }

                    bufferStream.Seek(0, SeekOrigin.Begin);
                    bufferStream.Write(NetworkConverter.GetBytes((int)bufferStream.Length - 5), 0, 4);
                    bufferStream.WriteByte((byte)SerializeId.Command);

                    streams.Add(bufferStream);
                }
                // Content
                if (this.Content != null)
                {
                    BufferStream bufferStream = new BufferStream(bufferManager);
                    bufferStream.Write(NetworkConverter.GetBytes((int)this.Content.Count), 0, 4);
                    bufferStream.WriteByte((byte)SerializeId.Content);
                    bufferStream.Write(this.Content.Array, this.Content.Offset, this.Content.Count);

                    streams.Add(bufferStream);
                }

                return(new JoinStream(streams));
            }
        }
Example #21
0
        public DatumViewer( )
        {
            InitializeComponent( );

            _cacheStream = CacheStream.Open(Path.Combine(Local.MapsDirectory, "headlong.map"));
            var objectListView       = new TagDatumView( );
            var guerillaPropertyView = new GuerillaBlockPropertyViewer( );

            objectListView.LoadTagDatums(_cacheStream.Index);
            objectListView.NodeMouseDoubleClick += (sender, args) =>
            {
                if (objectListView.SelectedNode != null && objectListView.SelectedNode.Tag is TagDatum)
                {
                    var guerillaBlock = _cacheStream.Deserialize((( TagDatum )objectListView.SelectedNode.Tag).Identifier);
                    guerillaPropertyView.LoadGuerillaBlocks(guerillaBlock);
                }
            };

            guerillaPropertyView.Show(dockPanel1, DockState.Document);

            objectListView.Show(dockPanel1, DockState.DockLeft);
        }
Example #22
0
        private List <VertexConstantInfo> GetVertexConstants(ShaderBlock shaderBlock,
                                                             ShaderTemplateBlock shaderTemplateBlock,
                                                             ShaderPassBlock shaderPassBlock, TagDatum vrtxDatum, TagDatum spasDatum, TagDatum stemDatum,
                                                             TagDatum shaderDatum, CacheStream cache)
        {
            var vertexConstantInfos = new List <VertexConstantInfo>( );
            var passes =
                shaderTemplateBlock.PostprocessDefinition[0].Passes.Where(u => u.Pass.Ident == spasDatum.Identifier);

            foreach (var pass in passes)
            {
                for (int i = pass.Implementations.Index; i < pass.Implementations.Index + pass.Implementations.Length; i++)
                {
                    var index =
                        shaderTemplateBlock.PostprocessDefinition[0].Implementations[i].VertexConstants.Index;
                    var length =
                        shaderTemplateBlock.PostprocessDefinition[0].Implementations[i].VertexConstants.Length;
                    for (int j = index; j < index + length; j++)
                    {
                        var sourceIndex = shaderTemplateBlock.PostprocessDefinition[0].Remappings[j].SourceIndex;
                        var bytes       = shaderTemplateBlock.PostprocessDefinition[0].Remappings[j].fieldskip;

                        var vertexConstant = shaderBlock.PostprocessDefinition[0].VertexConstants[sourceIndex];

                        var info = new VertexConstantInfo
                        {
                            source   = sourceIndex,
                            bytes    = bytes,
                            value    = new Vector4(vertexConstant.Vector3, vertexConstant.W),
                            stringId = Halo2.Strings[new StringIdent(BitConverter.ToInt16(bytes, 0), 0)]
                        };
                        vertexConstantInfos.Add(info);
                    }
                }
            }
            return(vertexConstantInfos);
        }
Example #23
0
        public async override void OnNavigatedTo(INavigationParameters parameters)
        {
            base.OnNavigatedTo(parameters);

            if (parameters.ContainsKey("ImageInfos"))
            {
                parameters.TryGetValue("ImageInfos", out List <string> images);

                foreach (var url in images)
                {
                    try
                    {
                        var config = new FFImageLoading.Config.Configuration()
                        {
                            ExecuteCallbacksOnUIThread = true
                        };
                        byte[]      byteArray;
                        CacheStream cacheStream = null;
                        try
                        {
                            cacheStream = await ImageService.Instance.Config.DownloadCache.DownloadAndCacheIfNeededAsync(url,
                                                                                                                         ImageService.Instance.LoadUrl(url),
                                                                                                                         ImageService.Instance.Config,
                                                                                                                         CancellationToken.None);
                        }
                        catch (Exception)
                        {
                            var timg = "Wesley.Client.Resources/images/loading.gif";
                            cacheStream = await ImageService.Instance.Config.DownloadCache.DownloadAndCacheIfNeededAsync(timg,
                                                                                                                         ImageService.Instance.LoadUrl(timg),
                                                                                                                         ImageService.Instance.Config,
                                                                                                                         CancellationToken.None);
                        }

                        if (cacheStream != null)
                        {
                            using (cacheStream.ImageStream)
                                using (var memoryStream = new MemoryStream())
                                {
                                    await cacheStream.ImageStream.CopyToAsync(memoryStream);

                                    byteArray = memoryStream.ToArray();
                                }

                            var tp = new CustomStreamImageSource()
                            {
                                Key    = url,
                                Stream = (token) => Task.FromResult <Stream>(new MemoryStream(byteArray))
                            };

                            ImagesUrls.Add(tp);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                ;

                if (ImagesUrls.Count > 0)
                {
                    this.ImagesUrls = new ObservableRangeCollection <ImageSource>(ImagesUrls);
                }
            }
        }
Example #24
0
        public virtual void Load(string directoryPath)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            // Tempファイルを削除する。
            {
                foreach (var path in Directory.GetFiles(directoryPath, "*", SearchOption.TopDirectoryOnly))
                {
                    var ext = Path.GetExtension(path);

                    if (ext == ".tmp")
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            LockedHashSet <string> successNames = new LockedHashSet <string>();

            // DataContractSerializerのBinaryバージョン
            foreach (var extension in new string[] { ".v2", ".v2.bak" })
            {
                Parallel.ForEach(Directory.GetFiles(directoryPath), new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 8
                }, configPath =>
                {
                    if (!configPath.EndsWith(extension))
                    {
                        return;
                    }

                    var name = Path.GetFileName(configPath.Substring(0, configPath.Length - extension.Length));
                    if (successNames.Contains(name))
                    {
                        return;
                    }

                    Content content = null;
                    if (!_dic.TryGetValue(name, out content))
                    {
                        return;
                    }

                    try
                    {
                        using (FileStream stream = new FileStream(configPath, FileMode.Open))
                            using (CacheStream cacheStream = new CacheStream(stream, _cacheSize, BufferManager.Instance))
                                using (GZipStream decompressStream = new GZipStream(cacheStream, CompressionMode.Decompress))
                                {
                                    //using (XmlDictionaryReader xmlDictionaryReader = XmlDictionaryReader.CreateTextReader(decompressStream, XmlDictionaryReaderQuotas.Max))
                                    using (XmlDictionaryReader xml = XmlDictionaryReader.CreateBinaryReader(decompressStream, XmlDictionaryReaderQuotas.Max))
                                    {
                                        var deserializer = new DataContractSerializer(content.Type);
                                        content.Value    = deserializer.ReadObject(xml);
                                    }
                                }

                        successNames.Add(name);
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e);
                    }
                });
            }

            // DataContractSerializerのTextバージョン
            // 互換性は高いが処理速度が遅い。
            foreach (var extension in new string[] { ".gz", ".gz.bak" })
            {
                foreach (var configPath in Directory.GetFiles(directoryPath))
                {
                    if (!configPath.EndsWith(extension))
                    {
                        continue;
                    }

                    var name = Path.GetFileName(configPath.Substring(0, configPath.Length - extension.Length));
                    if (successNames.Contains(name))
                    {
                        continue;
                    }

                    Content content = null;
                    if (!_dic.TryGetValue(name, out content))
                    {
                        continue;
                    }

                    try
                    {
                        using (FileStream stream = new FileStream(configPath, FileMode.Open))
                            using (CacheStream cacheStream = new CacheStream(stream, _cacheSize, BufferManager.Instance))
                                using (GZipStream decompressStream = new GZipStream(cacheStream, CompressionMode.Decompress))
                                {
                                    using (XmlDictionaryReader xml = XmlDictionaryReader.CreateTextReader(decompressStream, XmlDictionaryReaderQuotas.Max))
                                    {
                                        var deserializer = new DataContractSerializer(content.Type);
                                        content.Value = deserializer.ReadObject(xml);
                                    }
                                }

                        successNames.Add(name);
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e);
                    }
                }
            }

            sw.Stop();
            Debug.WriteLine("Settings Load {0} {1}", Path.GetFileName(directoryPath), sw.ElapsedMilliseconds);
        }
Example #25
0
        public virtual void Save(string directoryPath)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            Parallel.ForEach(_dic, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            }, item =>
            {
                try
                {
                    var name  = item.Key;
                    var type  = item.Value.Type;
                    var value = item.Value.Value;

                    string uniquePath = null;

                    using (FileStream stream = SettingsBase.GetUniqueFileStream(Path.Combine(directoryPath, name + ".tmp")))
                        using (CacheStream cacheStream = new CacheStream(stream, _cacheSize, BufferManager.Instance))
                        {
                            uniquePath = stream.Name;

                            using (GZipStream compressStream = new GZipStream(cacheStream, CompressionMode.Compress))
                                using (XmlDictionaryWriter xml = XmlDictionaryWriter.CreateBinaryWriter(compressStream))
                                {
                                    var serializer = new DataContractSerializer(type);

                                    serializer.WriteStartObject(xml, value);
                                    xml.WriteAttributeString("xmlns", "xa", "http://www.w3.org/2000/xmlns/", "http://schemas.microsoft.com/2003/10/Serialization/");
                                    xml.WriteAttributeString("xmlns", "xc", "http://www.w3.org/2000/xmlns/", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
                                    xml.WriteAttributeString("xmlns", "xb", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/2001/XMLSchema");
                                    serializer.WriteObjectContent(xml, value);
                                    serializer.WriteEndObject(xml);
                                }
                        }

                    string newPath = Path.Combine(directoryPath, name + ".v2");
                    string bakPath = Path.Combine(directoryPath, name + ".v2.bak");

                    if (File.Exists(newPath))
                    {
                        if (File.Exists(bakPath))
                        {
                            File.Delete(bakPath);
                        }

                        File.Move(newPath, bakPath);
                    }

                    File.Move(uniquePath, newPath);

                    {
                        foreach (var extension in new string[] { ".gz", ".gz.bak" })
                        {
                            string deleteFilePath = Path.Combine(directoryPath, name + extension);

                            if (File.Exists(deleteFilePath))
                            {
                                File.Delete(deleteFilePath);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warning(e);
                }
            });

            sw.Stop();
            Debug.WriteLine("Settings Save {0} {1}", Path.GetFileName(directoryPath), sw.ElapsedMilliseconds);
        }
Example #26
0
            public override Stream Export(BufferManager bufferManager)
            {
                using (DeadlockMonitor.Lock(this.ThisLock))
                {
                    List <Stream> streams  = new List <Stream>();
                    Encoding      encoding = new UTF8Encoding(false);

                    // Name
                    if (this.Name != null)
                    {
                        BufferStream bufferStream = new BufferStream(bufferManager);
                        bufferStream.SetLength(5);
                        bufferStream.Seek(5, SeekOrigin.Begin);

                        using (CacheStream cacheStream = new CacheStream(bufferStream, 1024, true, bufferManager))
                            using (StreamWriter writer = new StreamWriter(cacheStream, encoding))
                            {
                                writer.Write(this.Name);
                            }

                        bufferStream.Seek(0, SeekOrigin.Begin);
                        bufferStream.Write(NetworkConverter.GetBytes((int)bufferStream.Length - 5), 0, 4);
                        bufferStream.WriteByte((byte)SerializeId.Name);

                        streams.Add(bufferStream);
                    }
                    // State
                    if (this.State != 0)
                    {
                        BufferStream bufferStream = new BufferStream(bufferManager);
                        bufferStream.SetLength(5);
                        bufferStream.Seek(5, SeekOrigin.Begin);

                        using (CacheStream cacheStream = new CacheStream(bufferStream, 1024, true, bufferManager))
                            using (StreamWriter writer = new StreamWriter(cacheStream, encoding))
                            {
                                writer.Write(this.State.ToString());
                            }

                        bufferStream.Seek(0, SeekOrigin.Begin);
                        bufferStream.Write(NetworkConverter.GetBytes((int)bufferStream.Length - 5), 0, 4);
                        bufferStream.WriteByte((byte)SerializeId.State);

                        streams.Add(bufferStream);
                    }
                    // Managers
                    foreach (var m in this.Managers)
                    {
                        BufferStream bufferStream = new BufferStream(bufferManager);
                        bufferStream.Write(NetworkConverter.GetBytes((int)m.Length), 0, 4);
                        bufferStream.WriteByte((byte)SerializeId.Manager);
                        bufferStream.Write(m, 0, m.Length);

                        streams.Add(bufferStream);
                    }
                    // Members
                    foreach (var m in this.Members)
                    {
                        BufferStream bufferStream = new BufferStream(bufferManager);
                        bufferStream.Write(NetworkConverter.GetBytes((int)m.Length), 0, 4);
                        bufferStream.WriteByte((byte)SerializeId.Member);
                        bufferStream.Write(m, 0, m.Length);

                        streams.Add(bufferStream);
                    }

                    return(new AddStream(streams));
                }
            }
Example #27
0
 internal static void SetActiveMap(CacheStream mapstream)
 {
     mapStream = mapstream;
 }
Example #28
0
        public static void Compress(Stream inStream, Stream outStream, BufferManager bufferManager)
        {
            var info = new ProcessStartInfo(_path);

            info.CreateNoWindow         = true;
            info.UseShellExecute        = false;
            info.RedirectStandardInput  = true;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError  = true;

            info.Arguments = "--compress --format=xz -4 --threads=1 --stdout";

            using (var inCacheStream = new CacheStream(inStream, 1024 * 32, bufferManager))
                using (var outCacheStream = new CacheStream(outStream, 1024 * 32, bufferManager))
                {
                    using (Process process = Process.Start(info))
                    {
                        process.PriorityClass = ProcessPriorityClass.Idle;

                        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
                        {
                            Log.Error(e.Data);
                        };

                        Exception threadException = null;

                        var thread = new Thread(new ThreadStart(() =>
                        {
                            try
                            {
                                byte[] buffer = bufferManager.TakeBuffer(1024 * 32);

                                try
                                {
                                    using (var standardOutputStream = process.StandardOutput.BaseStream)
                                    {
                                        int length = 0;

                                        while ((length = standardOutputStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            outCacheStream.Write(buffer, 0, length);
                                        }
                                    }
                                }
                                finally
                                {
                                    bufferManager.ReturnBuffer(buffer);
                                }
                            }
                            catch (Exception e)
                            {
                                threadException = e;
                            }
                        }));
                        thread.IsBackground = true;
                        thread.Start();

                        try
                        {
                            byte[] buffer = bufferManager.TakeBuffer(1024 * 32);

                            try
                            {
                                using (var standardInputStream = process.StandardInput.BaseStream)
                                {
                                    int length = 0;

                                    while ((length = inCacheStream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        standardInputStream.Write(buffer, 0, length);
                                    }
                                }
                            }
                            finally
                            {
                                bufferManager.ReturnBuffer(buffer);
                            }
                        }
                        catch (Exception)
                        {
                            throw;
                        }

                        thread.Join();
                        if (threadException != null)
                        {
                            throw threadException;
                        }

                        process.WaitForExit();
                    }
                }
        }
Example #29
0
 private static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     var _cacheStream = CacheStream.Open(Path.Combine(Local.MapsDirectory, "ascension.map"));
 }
Example #30
0
        public bool Validate(MoonfishTagGroup validateTag, IEnumerable <MoonfishTagGroup> tagPool, string[] filenames)
        {
            error         = false;
            _pointersList = new List <Tuple <BlamPointer, ElementArray> >();
            var filename = string.Format(@"{1}\test_analysis\analysis.txt",
                                         validateTag.Class.ToTokenString(), Local.MapsDirectory);
            var stringWriter = File.AppendText(filename);

            _writeMessage = (stringWriter.WriteLine);

            var offset       = 0;
            var elementArray = CompileElementArray(validateTag, tagPool, offset);

            elementArray.count = 1;

            foreach (var file in filenames)
            {
                using (var map = new CacheStream(file))
                {
                    var binaryReader = new BinaryReader(map);

                    //OnWriteMessage(string.Format("Begin ({0})", map.MapName));

                    foreach (var tag in map.Index)
                    {
                        error = false;
                        if (!(tag.Class == validateTag.Class))
                        {
                            continue;
                        }

                        var metaTableMemory = map.DefaultMemoryBlock;
                        if (tag.Class == (TagClass)"sbsp" || tag.Class == (TagClass)"ltmp")
                        {
                            metaTableMemory =
                                map.StructureMemoryBlocks[map.StructureMemoryBlockBindings[tag.Identifier]];
                            map.ActiveAllocation(StructureCache.VirtualStructureCache0 +
                                                 map.StructureMemoryBlockBindings[tag.Identifier]);
                        }
                        var virtualTagMemory = new VirtualMappedAddress
                        {
                            Address = tag.VirtualAddress,
                            Length  = tag.Length
                        };

                        _isValidDelegate             = metaTableMemory.Contains;
                        _isPointerOwnedByTagDelegate = virtualTagMemory.Contains;
                        _pointersList.Clear();

                        offset = (int)map.Seek(tag.Identifier);


                        elementArray.virtualAddress = map.Index[tag.Identifier].VirtualAddress;
                        ValidateTagBlock(elementArray, elementArray.ToFixedArrayPointer(), binaryReader, ref offset);

                        if (error)
                        {
                            OnWriteMessage(string.Format("Tag ({0}.{1})", tag.Path, validateTag.Class.ToTokenString()));
                        }

                        stringWriter.Flush();
                    }

                    Console.WriteLine(@"Parsed ({0})", map.Header.Name);
                }
            }
            stringWriter.Close();


            return(error);
        }