Пример #1
0
        /// <summary>
        /// Gets the DBRecord for a particular ID.
        /// </summary>
        /// <param name="recordId">string ID of the record will be normalized internally</param>
        /// <returns>DBRecord corresponding to the string ID.</returns>
        public DBRecordCollection GetItem(string recordId)
        {
            DBRecordCollection result = null;

            if (string.IsNullOrEmpty(recordId))
            {
                return(result);
            }

            recordId = TQData.NormalizeRecordPath(recordId);
            DBRecordCollection databaseRecord;

            if (cache.ContainsKey(recordId))
            {
                databaseRecord = this.cache[recordId];
            }
            else
            {
                RecordInfo rawRecord;
                if (recordInfo.ContainsKey(recordId))
                {
                    rawRecord = this.recordInfo[recordId];
                }
                else
                {
                    // record not found
                    return(null);
                }

                databaseRecord = rawRecord.Decompress(this);
                this.cache.Add(recordId, databaseRecord);
            }

            return(databaseRecord);
        }
Пример #2
0
        /// <summary>
        /// Gets the DBRecord for a particular ID.
        /// </summary>
        /// <param name="recordId">string ID of the record will be normalized internally</param>
        /// <returns>DBRecord corresponding to the string ID.</returns>
        public DBRecordCollection GetItem(string recordId)
        {
            DBRecordCollection result = null;

            if (string.IsNullOrEmpty(recordId))
            {
                return(result);
            }

            recordId = TQData.NormalizeRecordPath(recordId);
            DBRecordCollection databaseRecord;

            try
            {
                databaseRecord = this.cache[recordId];
            }
            catch (KeyNotFoundException)
            {
                RecordInfo rawRecord;
                try
                {
                    rawRecord = this.recordInfo[recordId];
                }
                catch (KeyNotFoundException)
                {
                    // record not found
                    return(null);
                }

                databaseRecord = rawRecord.Decompress(this);
                this.cache.Add(recordId, databaseRecord);
            }

            return(databaseRecord);
        }
Пример #3
0
        /// <summary>
        /// Gets the DBRecord for a particular ID.
        /// </summary>
        /// <param name="recordId">string ID of the record will be normalized internally</param>
        /// <returns>DBRecord corresponding to the string ID.</returns>
        public DBRecordCollection GetItem(string recordId)
        {
            DBRecordCollection result = null;

            if (string.IsNullOrEmpty(recordId))
            {
                return(result);
            }

            recordId = TQData.NormalizeRecordPath(recordId);

            if (!cache.ContainsKey(recordId))
            {
                return(OnCachedItemNotFound(recordId));
            }
            return(this.cache[recordId]);
        }
Пример #4
0
            /// <summary>
            /// Decompresses an individual record.
            /// </summary>
            /// <param name="arzFile">ARZ file which we are decompressing.</param>
            /// <returns>decompressed DBRecord.</returns>
            public DBRecordCollection Decompress(ArzFile arzFile)
            {
                // record variables have this format:
                // 0x00 int16 specifies data type:
                //      0x0000 = int - data will be an int32
                //      0x0001 = float - data will be a Single
                //      0x0002 = string - data will be an int32 that is index into string table
                //      0x0003 = bool - data will be an int32
                // 0x02 int16 specifies number of values (usually 1, but sometimes more (for arrays)
                // 0x04 int32 key string ID (the id into the string table for this variable name
                // 0x08 data value
                byte[] data = this.DecompressBytes(arzFile);

                int numberOfDWords = data.Length / 4;

                if (data.Length % 4 != 0)
                {
                    // Turn on debugging so we can log the exception.
                    if (!TQDebug.DebugEnabled)
                    {
                        TQDebug.DebugEnabled = true;
                    }

                    TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error in ARZFile - {0}", arzFile.fileName));
                    TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, data Length = {1} which is not a multiple of 4", this.ID, (int)data.Length));
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, data Length = {1} which is not a multiple of 4", this.ID, (int)data.Length));
                }

                DBRecordCollection record = new DBRecordCollection(this.ID, this.RecordType);

                // Create a memory stream to read the binary data
                using (BinaryReader inReader = new BinaryReader(new MemoryStream(data, false)))
                {
                    int i = 0;
                    while (i < numberOfDWords)
                    {
                        short  dataType     = inReader.ReadInt16();
                        short  valCount     = inReader.ReadInt16();
                        int    variableID   = inReader.ReadInt32();
                        string variableName = arzFile.Getstring(variableID);

                        if (variableName == null)
                        {
                            // Turn on debugging so we can log the exception.
                            if (!TQDebug.DebugEnabled)
                            {
                                TQDebug.DebugEnabled = true;
                            }

                            TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error in ARZFile - {0}", arzFile.fileName));
                            TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, variable is NULL", this.ID));
                            throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, variable is NULL", this.ID));
                        }

                        if (dataType < 0 || dataType > 3)
                        {
                            // Turn on debugging so we can log the exception.
                            if (!TQDebug.DebugEnabled)
                            {
                                TQDebug.DebugEnabled = true;
                            }

                            TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error in ARZFile - {0}", arzFile.fileName));
                            TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, variable {1}, bad dataType {2}", this.ID, variableName, dataType));
                            throw new ArgumentOutOfRangeException(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, variable {1}, bad dataType {2}", this.ID, variableName, dataType));
                        }

                        Variable v = new Variable(variableName, (VariableDataType)dataType, valCount);

                        if (valCount < 1)
                        {
                            // Turn on debugging so we can log the exception.
                            if (!TQDebug.DebugEnabled)
                            {
                                TQDebug.DebugEnabled = true;
                            }

                            TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error in ARZFile - {0}", arzFile.fileName));
                            TQDebug.DebugWriteLine(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, variable {1}, bad valCount {2}", this.ID, variableName, valCount));
                            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Error while parsing arz record {0}, variable {1}, bad valCount {2}", this.ID, variableName, valCount));
                        }

                        // increment our dword count
                        i += 2 + valCount;

                        for (int j = 0; j < valCount; ++j)
                        {
                            switch (v.DataType)
                            {
                            case VariableDataType.Integer:
                            case VariableDataType.Boolean:
                            {
                                int val = inReader.ReadInt32();
                                v[j] = val;
                                break;
                            }

                            case VariableDataType.Float:
                            {
                                float val = inReader.ReadSingle();
                                v[j] = val;
                                break;
                            }

                            case VariableDataType.StringVar:
                            {
                                int    id  = inReader.ReadInt32();
                                string val = arzFile.Getstring(id);
                                if (val == null)
                                {
                                    val = string.Empty;
                                }
                                else
                                {
                                    val = val.Trim();
                                }

                                v[j] = val;
                                break;
                            }

                            default:
                            {
                                int val = inReader.ReadInt32();
                                v[j] = val;
                                break;
                            }
                            }
                        }

                        record.Set(v);
                    }
                }

                return(record);
            }
Пример #5
0
        /// <summary>
        /// Builds the table from the database using the passed table Id.
        /// </summary>
        private void LoadTable()
        {
            // Get the data
            DBRecordCollection record = Database.DB.GetRecordFromFile(this.tableId);

            if (record == null)
            {
                return;
            }

            // Go through and get all of the randomizerWeight randomizerName pairs that have a weight > 0.
            Dictionary <int, float>  weights = new Dictionary <int, float>();
            Dictionary <int, string> names   = new Dictionary <int, string>();

            foreach (Variable variable in record)
            {
                string upperCase = variable.Name.ToUpperInvariant();
                if (upperCase.StartsWith("RANDOMIZERWEIGHT", StringComparison.Ordinal))
                {
                    string numPart = upperCase.Substring(16);
                    int    num;
                    if (int.TryParse(numPart, out num))
                    {
                        // Make sure the value is an integer or float
                        float value = -1.0F;

                        if (variable.DataType == VariableDataType.Integer)
                        {
                            value = (float)variable.GetInt32(0);
                        }
                        else if (variable.DataType == VariableDataType.Float)
                        {
                            value = variable.GetSingle(0);
                        }

                        if (value > 0)
                        {
                            weights.Add(num, value);
                        }
                    }
                }
                else if (upperCase.StartsWith("RANDOMIZERNAME", StringComparison.Ordinal))
                {
                    string numPart = upperCase.Substring(14);
                    int    num;
                    if (int.TryParse(numPart, out num))
                    {
                        // now get the value
                        string value = variable.GetString(0);
                        if (!string.IsNullOrEmpty(value))
                        {
                            names.Add(num, value);
                        }
                    }
                }
            }

            // Now do an INNER JOIN on the 2 dictionaries to find valid pairs.
            IEnumerable <KeyValuePair <string, float> > buildTableQuery = from weight in weights
                                                                          join name in names on weight.Key equals name.Key
                                                                          select new KeyValuePair <string, float>(name.Value, weight.Value);

            // Iterate the query to build the new unweighted table.
            foreach (KeyValuePair <string, float> kvp in buildTableQuery)
            {
                // Check for a double entry in the table.
                if (this.data.ContainsKey(kvp.Key))
                {
                    // for a double entry just add the chance.
                    this.data[kvp.Key] += kvp.Value;
                }
                else
                {
                    this.data.Add(kvp.Key, kvp.Value);
                }
            }

            // Calculate the total weight.
            this.totalWeight = buildTableQuery.Sum(kvp => kvp.Value);
        }
Пример #6
0
 public Info(DBRecordCollection record)
 {
     this.record = record;
     this.AssignVariableNames();
 }