Beispiel #1
0
        static void WriteLut(Entry[] lut, string path)
        {
            BdsVersion fileVersion     = new BdsVersion();
            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());

            fileVersion.Major    = assemblyVersion.Major;
            fileVersion.Minor    = assemblyVersion.Minor;
            fileVersion.Revision = assemblyVersion.Revision;
            fileVersion.Build    = assemblyVersion.Build;
            fileVersion.ScmInfo  = assemblyVersion.ScmInfo;

            string dir = Path.GetDirectoryName(path);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (BinaryWriter wr = new BinaryWriter(File.Open(path, FileMode.Create)))
            {
                DataSerializationHelper.WriteHeader(wr, fileVersion, SER_FMT_VER);
                wr.Write(lut.Length);
                for (int i = 0; i < lut.Length; ++i)
                {
                    wr.Write(lut[i].CardSet);
                    wr.Write(lut[i].Ahvo);
                }
            }
        }
Beispiel #2
0
        static void WriteTable(UInt32[] table, string path)
        {
            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());
            BdsVersion fileVersion     = new BdsVersion();

            fileVersion.Major    = assemblyVersion.Major;
            fileVersion.Minor    = assemblyVersion.Minor;
            fileVersion.Revision = assemblyVersion.Revision;
            fileVersion.ScmInfo  = assemblyVersion.ScmInfo;

            string dir = Path.GetDirectoryName(path);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (BinaryWriter wr = new BinaryWriter(File.Open(path, FileMode.Create)))
            {
                fileVersion.Write(wr);
                wr.Write(table.Length);
                for (int i = 0; i < table.Length; ++i)
                {
                    wr.Write(table[i]);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Load precalcuation table for fast calculation.
        /// If not called by user, will be called automatically as soon as
        /// it is necessary (which may slow-down the very first calculation).
        /// </summary>
        public static void LoadLut()
        {
            string path = GetLutPath();

            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());

            using (BinaryReader r = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                BdsVersion fileVersion = new BdsVersion();
                fileVersion.Read(r);
                if (assemblyVersion.Major != fileVersion.Major ||
                    assemblyVersion.Minor != fileVersion.Minor ||
                    assemblyVersion.Revision != fileVersion.Revision)
                {
                    throw new ApplicationException(
                              String.Format("Wrong file version: expected: {0:x8}, was: {1:x8}, file: {2}",
                                            assemblyVersion, fileVersion, path));
                }
                int count = r.ReadInt32();
                _lut = new Precalculated[count];

                for (int i = 0; i < count; ++i)
                {
                    _lut[i].Read(r);
                }
            }
        }
Beispiel #4
0
        static Entry[] ReadTable(string path)
        {
            Entry[] table = null;
            using (BinaryReader r = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                BdsVersion fileVersion = new BdsVersion();
                fileVersion.Read(r);
                if (Version.Major != fileVersion.Major ||
                    Version.Minor != fileVersion.Minor ||
                    Version.Revision != fileVersion.Revision)
                {
                    throw new ApplicationException(
                              String.Format("Wrong file version: expected: {0:x8}, was: {1:x8}, file: {2}",
                                            Version, fileVersion, path));
                }
                int count = r.ReadInt32();
                table = new Entry[count];

                for (int i = 0; i < count; ++i)
                {
                    table[i].key   = r.ReadUInt32();
                    table[i].value = r.ReadSingle();
                }
            }
            return(table);
        }
Beispiel #5
0
        static UInt32[] ReadTable(string path)
        {
            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());

            UInt32 [] table = null;
            using (BinaryReader r = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                BdsVersion fileVersion = new BdsVersion();
                fileVersion.Read(r);
                if (assemblyVersion.Major != fileVersion.Major ||
                    assemblyVersion.Minor != fileVersion.Minor ||
                    assemblyVersion.Revision != fileVersion.Revision)
                {
                    throw new ApplicationException(
                              String.Format("Wrong file version: expected: {0:x8}, was: {1:x8}, file: {2}",
                                            assemblyVersion, fileVersion, path));
                }
                int count = r.ReadInt32();
                table = new UInt32[count];

                for (int i = 0; i < count; ++i)
                {
                    table[i] = r.ReadUInt32();
                }
            }
            return(table);
        }
Beispiel #6
0
        private void ReadInternal(BinaryReader r)
        {
            Version = new BdsVersion();
            Version.Read(r);
            int serFmtVer = r.ReadInt32();

            if (serFmtVer > SERIALIZATION_FORMAT_VERSION)
            {
                throw new ApplicationException(string.Format(
                                                   "Unsupported serialization format: {0}, max supported: {1}",
                                                   serFmtVer, SERIALIZATION_FORMAT_VERSION));
            }

            if (serFmtVer == 1)
            {
                throw new ApplicationException("Serialization format version 1 is not supported");
            }

            int nodeSerFmtVer = 0;

            if (serFmtVer >= 3)
            {
                nodeSerFmtVer = r.ReadInt32();
            }

            IClusterNode       root;
            string             typeName = r.ReadString();
            ClassFactoryParams p        = new ClassFactoryParams(typeName, "");

            root = ClassFactory.CreateInstance <IClusterNode>(p);
            root.Read(r, nodeSerFmtVer);
            Root = root;
        }
        /// <summary>
        /// Saves the generated states as a look-up table.
        /// Format:
        /// BdsVersion.
        /// UInt32 format ID.
        /// UInt32 LUT size (number of UInt32 entries).
        /// UInt32 [] LUT.
        /// </summary>
        public void SaveLut(string path, UInt32 formatId)
        {
            string dirName = Path.GetDirectoryName(path);

            if (!Directory.Exists(dirName))
            {
                Directory.CreateDirectory(dirName);
            }
            using (BinaryWriter w = new BinaryWriter(File.Open(path, FileMode.Create, FileAccess.Write)))
            {
                BdsVersion version = new BdsVersion(Assembly.GetExecutingAssembly());
                version.Write(w);
                w.Write(formatId);
                w.Write((UInt32)_states.Count * 52);
                for (int s = 0; s < _states.Count; ++s)
                {
                    for (int i = 0; i < 52; ++i)
                    {
                        UInt32 value = (UInt32)_states[s].Table[i];
                        if (_states[s].Key.HandSize < _maxHandSize - 1)
                        {
                            value *= 52;
                        }
                        w.Write(value);
                    }
                }
            }
        }
Beispiel #8
0
        public static void Precalculate()
        {
            DateTime startTime = DateTime.Now;

            string lutPath = GetLutPath();

            if (File.Exists(lutPath))
            {
                // Do not ovewriting an existing file to save time.
                Console.WriteLine("LUT file {0} already exist, exiting. Delete the file to recalculate.", lutPath);
                return;
            }

            Precalculated[] precalcArray = new Precalculated[HePocket.Count * (HePocket.Count + 1) / 2];
            int             i            = 0;

            Console.WriteLine("Calculating for");
            for (int pk1 = 0; pk1 < HePocket.Count; pk1++)
            {
                Console.Write(" {0}", (HePocketKind)pk1);
                for (int pk2 = pk1; pk2 < HePocket.Count; pk2++)
                {
                    Result r = Calculate((HePocketKind)pk1, (HePocketKind)pk2);
                    precalcArray[i].PocketKind1 = (byte)pk1;
                    precalcArray[i].PocketKind2 = (byte)pk2;
                    precalcArray[i].Count       = (byte)r.Count;
                    precalcArray[i].Equity      = r.Equity;
                    ++i;
                }
            }
            Console.WriteLine();

            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());
            BdsVersion fileVersion     = new BdsVersion();

            fileVersion.Major       = assemblyVersion.Major;
            fileVersion.Minor       = assemblyVersion.Minor;
            fileVersion.Revision    = assemblyVersion.Revision;
            fileVersion.ScmInfo     = assemblyVersion.ScmInfo;
            fileVersion.Description = "HE heads-up pocket equity LUT.";

            string dir = Path.GetDirectoryName(lutPath);

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

            using (BinaryWriter wr = new BinaryWriter(File.Open(lutPath, FileMode.Create)))
            {
                fileVersion.Write(wr);
                wr.Write(precalcArray.Length);
                for (i = 0; i < precalcArray.Length; ++i)
                {
                    precalcArray[i].Write(wr);
                }
            }
            Console.WriteLine("Calculated in {0} s", (DateTime.Now - startTime).TotalSeconds);
        }
 public void Read(string fileName)
 {
     using (BinaryReader r = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read)))
     {
         Version = new BdsVersion();
         Version.Read(r);
         Data = r.ReadString();
     }
 }
Beispiel #10
0
        private static BdsVersion ProcessAssembly(string fileName)
        {
            // Use LoadFrom() instead of ReflectionOnlyLoadFrom(), because
            // the latter requires all dependencies to be loaded manually.
            Assembly   assembly = Assembly.LoadFrom(fileName);
            BdsVersion ver      = new BdsVersion(assembly);

            return(ver);
        }
Beispiel #11
0
        private static BdsVersion ProcessBinary(string fileName)
        {
            BdsVersion ver = new BdsVersion();

            using (BinaryReader r = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                ver.Read(r);
            }
            return(ver);
        }
Beispiel #12
0
        private static void Replace(string fileName, BdsVersion newVersion)
        {
            string ext = Path.GetExtension(fileName).ToLower(CultureInfo.InvariantCulture);

            if (ext == ".exe" || ext == ".dll" || ext == ".xml")
            {
                throw new ApplicationException(string.Format("Expected data file, was {0}", ext));
            }
            BdsVersion.ReplaceInDataFile(fileName,
                                         (ref BdsVersion curVersion) => curVersion = newVersion);
        }
Beispiel #13
0
 public ActionTree(GameDefinition gameDef, Bucketizer bucketizer)
 {
     Version    = new BdsVersion(Assembly.GetExecutingAssembly());
     GameDef    = gameDef;
     Bucketizer = bucketizer;
     Positions  = new ActionTreeNode[GameDef.MinPlayers];
     for (int p = 0; p < Positions.Length; ++p)
     {
         Positions[p]       = new ActionTreeNode();
         Positions[p].State = new GameState(gameDef, gameDef.MinPlayers);
     }
 }
Beispiel #14
0
        private void ReadInternalFDA(BinaryReader r)
        {
            Version = new BdsVersion();
            Version.Read(r);
            int serFmtVer = r.ReadInt32();

            _nodesByteSize = r.ReadInt64();
            _nodesCount    = r.ReadInt64();
            _nodeByteSize  = (int)(_nodesByteSize / _nodesCount);
            _fdaReader     = r;
            _depthFilePos  = r.BaseStream.Position;
            _nodesFilePos  = _depthFilePos + _nodesCount;
            AfterRead();
        }
        public void Test_FromAssembly()
        {
            Assembly   assembly = typeof(BdsVersion).Assembly;
            BdsVersion ver      = new BdsVersion(assembly);

            Assert.AreEqual(assembly.GetName().Version.Major, ver.Major);
            Assert.AreEqual(assembly.GetName().Version.Minor, ver.Minor);
            Assert.AreEqual(assembly.GetName().Version.Revision, ver.Revision);
            Assert.AreEqual(assembly.GetName().Version.Build, ver.Build);

            Assert.IsTrue(ver.Description.StartsWith("Various utilities"));
            Assert.IsTrue(ver.BuildInfo.Contains("POM:"));
            Assert.IsTrue(ver.ScmInfo.Contains("utils/"));
        }
Beispiel #16
0
        private void ReadInternal(BinaryReader r)
        {
            Version = new BdsVersion();
            Version.Read(r);
            int serFmtVer = r.ReadInt32();

            _nodesByteSize = r.ReadInt64();
            _nodesCount    = r.ReadInt64();
            _nodeByteSize  = (int)(_nodesByteSize / _nodesCount);
            Allocate(_nodesCount, _nodeByteSize);
            UnmanagedMemory.Read(r, _depthPtr, _nodesCount);
            UnmanagedMemory.Read(r, _nodesPtr, _nodesByteSize);
            ReadUserData(r);
            AfterRead();
        }
        public void Test_BinarySerialization()
        {
            Assembly   assembly = typeof(BdsVersion).Assembly;
            BdsVersion v1       = new BdsVersion(assembly);

            MemoryStream s = new MemoryStream();
            BinaryWriter w = new BinaryWriter(s);

            v1.Write(w);
            BdsVersion v2 = new BdsVersion();

            s.Seek(0, SeekOrigin.Begin);
            using (BinaryReader r = new BinaryReader(s))
            {
                v2.Read(r);
            }
            Assert.AreEqual(v1, v2);
        }
Beispiel #18
0
        static void WriteTable(List <Entry> table, string path)
        {
            BdsVersion fileVersion     = new BdsVersion(Version);
            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());

            fileVersion.ScmInfo = assemblyVersion.ScmInfo;

            using (BinaryWriter wr = new BinaryWriter(File.Open(path, FileMode.Create)))
            {
                fileVersion.Write(wr);
                wr.Write(table.Count);
                for (int i = 0; i < table.Count; ++i)
                {
                    wr.Write(table[i].key);
                    wr.Write(table[i].value);
                }
            }
        }
Beispiel #19
0
        static LutEvaluator7()
        {
            string    fileName  = LutPath;
            Exception exception = null;

            try
            {
                using (FileStream file = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (BinaryReader r = new BinaryReader(file))
                    {
                        BdsVersion version = new BdsVersion();
                        version.Read(r);
                        UInt32 formatId    = r.ReadUInt32();
                        UInt32 lutSize     = r.ReadUInt32();
                        UInt32 lutByteSize = lutSize * 4;
                        _lutPtr = UnmanagedMemory.AllocHGlobalExSmartPtr(lutByteSize);
                        pLut    = (UInt32 *)_lutPtr;
                        UnmanagedMemory.Read(r, _lutPtr, lutByteSize);
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                if (_lutPtr != null)
                {
                    _lutPtr.Dispose();
                    _lutPtr = null;
                }
                pLut = (UInt32 *)IntPtr.Zero.ToPointer();

                // If IO error occured leave the class uninitialized. This is a normal case for table generation.
                // If the application tries to use an uninitialized class, an exception will be thrown somewhere.
                // Otherwise rethrow the exception.
                if (!(exception is IOException))
                {
                    throw exception;
                }
            }
        }
Beispiel #20
0
        ///<summary>Create instance for file acesss.</summary>
        ///<param name="forceCreation">If true - always create a new index file, otherwise create only if no file exists.</param>
        public UFTreeChildrenIndex(UFTree ufTree, string fileName, bool forceCreation)
        {
            if (forceCreation || !File.Exists(fileName))
            {
                CreateIndex(ufTree);
                using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Create, FileAccess.Write)))
                {
                    BdsVersion v = new BdsVersion(Assembly.GetExecutingAssembly());
                    v.Description = "UFTreeChildrenIndex for : '" + ufTree.Version.Description + "'";
                    v.Write(bw);
                    bw.Write(SERIALIZATION_FORMAT_VERSION);
                    bw.Write(ufTree.NodesCount);
                    for (long i = 0; i < _childrenBeginIdx.Length; ++i)
                    {
                        bw.Write(_childrenBeginIdx[i]);
                    }
                    for (long i = 0; i < _childrenIdx.Length; ++i)
                    {
                        bw.Write(_childrenIdx[i]);
                    }
                }
            }
            _childrenBeginIdxReader =
                new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));
            BdsVersion v1 = new BdsVersion();

            v1.Read(_childrenBeginIdxReader);
            int serFmtVer = _childrenBeginIdxReader.ReadInt32();

            if (serFmtVer > SERIALIZATION_FORMAT_VERSION)
            {
                throw new ApplicationException(
                          string.Format("Unsupported serialization format '{0}', max supported: '{1}'", serFmtVer,
                                        SERIALIZATION_FORMAT_VERSION));
            }
            long nodesCount = _childrenBeginIdxReader.ReadInt64();

            _childrenBeginIdxFilePos = _childrenBeginIdxReader.BaseStream.Position;
            _childrenIdxFilePos      = _childrenBeginIdxFilePos + (nodesCount + 1) * 4;
            _childrenIdxReader       = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));

            _childrenBeginIdx = _childrenIdx = null;
        }
Beispiel #21
0
        static void WriteTable <T>(List <T> table, string path) where T : IPersistent
        {
            BdsVersion fileVersion     = new BdsVersion(Version);
            BdsVersion assemblyVersion = new BdsVersion(Assembly.GetExecutingAssembly());

            fileVersion.ScmInfo = assemblyVersion.ScmInfo;

            string dir = Path.GetDirectoryName(path);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (BinaryWriter wr = new BinaryWriter(File.Open(path, FileMode.Create)))
            {
                fileVersion.Write(wr);
                wr.Write(table.Count);
                for (int i = 0; i < table.Count; ++i)
                {
                    table[i].Write(wr);
                }
            }
        }
        public void Test_Replace()
        {
            BdsVersion ver1 = new BdsVersion
            {
                Major           = 10, Minor = 11, Revision = 12, Build = 13,
                BuildInfo       = "build info 1",
                ScmInfo         = "scm info 1",
                Description     = "description 1",
                UserDescription = "user description 1"
            };

            BdsVersion ver2 = new BdsVersion
            {
                Major           = 20,
                Minor           = 21,
                Revision        = 22,
                Build           = 23,
                BuildInfo       = "build info 2",
                ScmInfo         = "scm info 2",
                Description     = "description 2",
                UserDescription = "user description 2"
            };

            TestDataFile file1 = new TestDataFile {
                Version = ver1, Data = "data"
            };
            string fileName = Path.Combine(_outDir, "test-file.dat");

            file1.Write(fileName);
            BdsVersion.ReplaceInDataFile(fileName, (ref BdsVersion v) => v = ver2);
            TestDataFile file2 = new TestDataFile();

            file2.Read(fileName);
            Assert.AreNotEqual(file1.Version, file2.Version);
            Assert.AreEqual(ver2, file2.Version);
            Assert.AreEqual(file1.Data, file2.Data);
        }
Beispiel #23
0
 public ClusterTree()
 {
     Version = new BdsVersion(Assembly.GetExecutingAssembly());
 }
Beispiel #24
0
            /// <summary>
            /// Merging read a tree from a stream.
            /// </summary>
            public void Read(BinaryReader r)
            {
                Version = new BdsVersion();
                Version.Read(r);
                int serFmtVer = r.ReadInt32();

                if (serFmtVer > SERIALIZATION_FORMAT_VERSION)
                {
                    throw new ApplicationException(
                              String.Format("Unsupported serialization format version: file format: {0}, supported: {1}",
                                            serFmtVer, SERIALIZATION_FORMAT_VERSION));
                }
                int    playersCount = r.ReadInt32();
                int    roundsCount  = r.ReadInt32();
                UInt64 samplesCount = r.ReadUInt64();
                string sourceInfo   = r.ReadString();

                if (SamplesCount == 0)
                {
                    // This is a virgin tree - copy all the data.
                    PlayersCount = playersCount;
                    RoundsCount  = roundsCount;
                    SourceInfo   = sourceInfo;
                }
                else
                {
                    if (PlayersCount != playersCount || RoundsCount != roundsCount || SourceInfo != sourceInfo)
                    {
                        throw new ApplicationException(String.Format(
                                                           "Merge target (pc: {0}, rc: {1}, si: {2}) does not match the merge source (pc: {3}, rc: {4}, si: {5})",
                                                           PlayersCount, RoundsCount, SourceInfo, playersCount, roundsCount, sourceInfo));
                    }
                }
                UInt64 sumLeavesCount = SamplesCount;

                SamplesCount += samplesCount;

                Int64 leavesCount = r.ReadInt64();
                int   cardsCount  = RoundsCount * PlayersCount;

                for (Int64 l = 0; l < leavesCount; ++l)
                {
                    byte[] cards = r.ReadBytes(cardsCount);
                    UInt32 count, result;
                    if (serFmtVer == 1)
                    {
                        UInt64 tmp = r.ReadUInt64();
                        if (tmp > UInt32.MaxValue)
                        {
                            throw new ApplicationException("Overflow");
                        }
                        count = (UInt32)tmp;
                        tmp   = r.ReadUInt64();
                        if (tmp > UInt32.MaxValue)
                        {
                            throw new ApplicationException("Overflow");
                        }
                        result = (UInt32)tmp;
                    }
                    else
                    {
                        count  = r.ReadUInt32();
                        result = r.ReadUInt32();
                    }
                    LeafT[] leaves   = GetLeavesByCards(cards);
                    int     lastCard = cards[cards.Length - 1];
                    leaves[lastCard].IncrementCount(count);
                    leaves[lastCard].IncrementResult(result);
                    sumLeavesCount += count;
                }
                VerifyTotalCount(sumLeavesCount);
            }
Beispiel #25
0
 public Tree()
 {
     Root       = new Node(false, DEFAULT_INITIAL_CAPACITY);
     Version    = new BdsVersion();
     SourceInfo = "";
 }
Beispiel #26
0
 /// <summary>
 /// Allocates memory for the nodes and depths arrays. The memory is not initialized.
 /// </summary>
 protected UFTree(Int64 nodesCount, Int32 nodeByteSize)
 {
     Version       = new BdsVersion();
     _nodeByteSize = nodeByteSize;
     Allocate(nodesCount, nodeByteSize);
 }