Beispiel #1
0
        /// <summary>
        /// Obtains an int value from an excel table using a StringId, column name, and row index.
        /// Returns 0x4C494146 on fail (will output as 'FAIL').
        /// </summary>
        /// <param name="stringId">An Excel Table StringId.</param>
        /// <param name="rowIndex">The row index to obtain the value from.</param>
        /// <param name="colName">The column name to check.</param>
        /// <returns></returns>
        public int GetExcelIntFromStringId(String stringId, int rowIndex, String colName)
        {
            if (DataFiles == null || String.IsNullOrEmpty(stringId) || DataFiles.ContainsKey(stringId) == false)
            {
                return(0x4C494146);
            }

            ExcelFile excelTable = DataFiles[stringId] as ExcelFile;

            if (excelTable == null)
            {
                return(0x4C494146);
            }
            if (rowIndex < 0 || rowIndex >= excelTable.Rows.Count)
            {
                return(0x4C494146);
            }

            ObjectDelegator excelDelegator = DataFileDelegators[stringId];
            Object          row            = excelTable.Rows[rowIndex];

            Object value = excelDelegator[colName](row);

            if (value is int)
            {
                return((int)value);
            }

            return((int)(short)value);
        }
Beispiel #2
0
        public Object GetRowFromValue(Xls.TableCodes tableCode, String colName, Object value)
        {
            if (value == null || String.IsNullOrEmpty(colName))
            {
                return(null);
            }

            ExcelFile excelTable = GetExcelTableFromCode(tableCode);

            if (excelTable == null)
            {
                return(null);
            }

            ObjectDelegator tableDelegate = DataFileDelegators[excelTable.StringId];

            ObjectDelegator.FieldDelegate fieldDelegate = tableDelegate.GetFieldDelegate(colName);
            if (fieldDelegate == null)
            {
                return(null);
            }

            foreach (Object row in excelTable.Rows)
            {
                if (value.Equals(fieldDelegate.GetValue(row)))
                {
                    return(row);
                }
            }

            return(null);
        }
Beispiel #3
0
        public int _GetExcelRowIndex(ExcelFile excelTable, String value)
        {
            FieldInfo field         = excelTable.Attributes.RowType.GetFields()[0];
            bool      isStringField = (field.FieldType == typeof(String));

            ObjectDelegator excelDelegator = DataFileDelegators[excelTable.StringId];

            ObjectDelegator.FieldGetValueDelegate getValue = excelDelegator[field.Name];

            int i = 0;

            foreach (Object row in excelTable.Rows)
            {
                if (isStringField)
                {
                    String val = (String)getValue(row);
                    if (val == value)
                    {
                        return(i);
                    }
                }
                else // string offset
                {
                    int    offset    = (int)getValue(row);
                    String stringVal = excelTable.ReadStringTable(offset);
                    if (stringVal == value)
                    {
                        return(i);
                    }
                }
                i++;
            }

            return(-1);
        }
Beispiel #4
0
        /// <summary>
        /// Checks if a data table has a specified column name.
        /// </summary>
        /// <param name="stringId">The StringId of the data table to check.</param>
        /// <param name="colName">The column name to check for.</param>
        /// <returns>True if the table has the column.</returns>
        public bool DataTableHasColumn(String stringId, String colName)
        {
            if (DataFiles == null || !DataFiles.ContainsKey(stringId))
            {
                return(false);
            }

            ObjectDelegator objectDelegator = DataFileDelegators[stringId];

            return(objectDelegator.ContainsGetFieldDelegate(colName));
        }
Beispiel #5
0
        /// <summary>
        /// Gets the RowIndex of a row containing a string, using the column name to search by.
        /// Returns -2 on error, -1 on not found
        /// </summary>
        /// <param name="stringId">An Excel Table StringId.</param>
        /// <param name="value">The value to search for.</param>
        /// <param name="colName">The column name to check.</param>
        /// <returns>Row index string found in column colName. (-2=error, -1=not found)</returns>
        public int GetExcelRowIndexFromStringId(String stringId, String value, String colName)
        {
            if (DataFiles == null || String.IsNullOrEmpty(stringId) || DataFiles.ContainsKey(stringId) == false)
            {
                return(-2);
            }

            ExcelFile excelTable = DataFiles[stringId] as ExcelFile;

            if (excelTable == null)
            {
                return(-2);
            }

            ObjectDelegator excelDelegator = DataFileDelegators[stringId];

            ObjectDelegator.FieldDelegate fieldDelegate = excelDelegator.GetFieldDelegate(colName);
            if (fieldDelegate == null)
            {
                return(-2);
            }

            bool isStringField = (fieldDelegate.FieldType == typeof(String));
            int  rowIndex      = -1;

            foreach (Object row in excelTable.Rows)
            {
                rowIndex++;

                if (isStringField)
                {
                    if ((String)fieldDelegate.GetValue(row) == value)
                    {
                        return(rowIndex);
                    }
                }
                else
                {
                    int offset = (int)fieldDelegate.GetValue(row);
                    if (excelTable.ReadStringTable(offset) == value)
                    {
                        return(rowIndex);
                    }
                }
            }

            return(-1);
        }
Beispiel #6
0
        public int GetExcelRowIndexFromStringId(String stringId, int value, String colName)
        {
            if (DataFiles == null || String.IsNullOrEmpty(stringId) || DataFiles.ContainsKey(stringId) == false)
            {
                return(-1);
            }

            ExcelFile       excelFile      = (ExcelFile)DataFiles[stringId];
            ObjectDelegator excelDelegator = DataFileDelegators[stringId];

            ObjectDelegator.FieldGetValueDelegate getValue = excelDelegator[colName];

            int  rowIndex = -1;
            bool foundRow = false;

            foreach (Object row in excelFile.Rows)
            {
                rowIndex++;

                int    intVal;
                Object val = getValue(row);
                if (val is short)
                {
                    intVal = (int)(short)val;
                }
                else
                {
                    intVal = (int)val;
                }

                if (intVal != value)
                {
                    continue;
                }

                foundRow = true;
                break;
            }

            return((foundRow) ? rowIndex : -1);
        }
Beispiel #7
0
        /// <summary>
        /// Obtains a String value from an excel table using a StringId, column name, and row index.
        /// Returns null on fail
        /// </summary>
        /// <param name="stringId">An Excel Table StringId.</param>
        /// <param name="rowIndex">The row index to obtain the value from.</param>
        /// <param name="colName">The column name to check.</param>
        /// <returns></returns>
        public String GetExcelStringFromStringId(String stringId, int rowIndex, String colName)
        {
            if (DataFiles == null || String.IsNullOrEmpty(stringId) || DataFiles.ContainsKey(stringId) == false)
            {
                return(null);
            }

            ExcelFile excelTable = DataFiles[stringId] as ExcelFile;

            if (excelTable == null)
            {
                return(null);
            }
            if (rowIndex < 0 || rowIndex >= excelTable.Rows.Count)
            {
                return(null);
            }

            ObjectDelegator excelDelegator = DataFileDelegators[stringId];

            ObjectDelegator.FieldDelegate fieldDelegate = excelDelegator.GetFieldDelegate(colName);
            if (fieldDelegate == null)
            {
                return(null);
            }

            bool   isStringField = (fieldDelegate.FieldType == typeof(String));
            Object row           = excelTable.Rows[rowIndex];

            if (isStringField)
            {
                return((String)fieldDelegate.GetValue(row));
            }

            int    offset    = (int)fieldDelegate.GetValue(row);
            String stringVal = excelTable.ReadStringTable(offset);

            return(stringVal);
        }
Beispiel #8
0
        public StringsFile(byte[] buffer, String filePath)
        {
            IsStringsFile = true;

            FilePath = filePath;
            StringId = _GetStringId(filePath);
            if (StringId == null)
            {
                throw new Exceptions.DataFileStringIdNotFound(filePath);
            }
            Attributes = DataFileMap[StringId];

            // create field delegators
            FieldInfo[] dataFileFields = Attributes.RowType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            dataFileFields = dataFileFields.OrderBy(f => f.MetadataToken).ToArray(); // order by defined order - GetFields does not guarantee ordering
            Delegator      = new ObjectDelegator(dataFileFields);

            Rows = new List <Object>();

            int  peek  = FileTools.ByteArrayToInt32(buffer, 0);
            bool isCSV = (peek != Token.Header);

            HasIntegrity = ((isCSV)) ? ParseCSV(buffer) : ParseData(buffer);
        }
Beispiel #9
0
        private String _GetExcelStringFromExcelFile(ExcelFile excelTable, int rowIndex, int colIndex)
        {
            if (excelTable == null || rowIndex >= excelTable.Rows.Count)
            {
                return(null);
            }

            ObjectDelegator tableDelegator = DataFileDelegators[excelTable.StringId];

            ObjectDelegator.FieldDelegate fieldDelegate = tableDelegator.GetPublicFieldDelegate(colIndex);

            bool   isStringField = (fieldDelegate.FieldType == typeof(String));
            Object row           = excelTable.Rows[rowIndex];

            if (isStringField)
            {
                return((String)fieldDelegate.GetValue(row));
            }

            int    offset    = (int)fieldDelegate.GetValue(row);
            String stringVal = excelTable.ReadStringTable(offset);

            return(stringVal);
        }
Beispiel #10
0
        private DataTable _LoadExcelTable(ExcelFile excelFile, bool doRelations, bool force)
        {
            String    tableName = excelFile.StringId;
            DataTable dataTable = XlsDataSet.Tables[tableName];

            if (dataTable != null && !force)
            {
                return(dataTable);
            }
            if (dataTable != null)
            {
                XlsDataSet.Relations.Clear();
                XlsDataSet.Tables.Remove(tableName);
            }
            dataTable           = XlsDataSet.Tables.Add(tableName);
            dataTable.TableName = tableName;
            dataTable.ExtendedProperties.Add("FileHeader", excelFile._excelFileHeader.DeepClone());

            Type dataType = excelFile.Attributes.RowType;
            List <OutputAttribute> outputAttributes = new List <OutputAttribute>();

            #region Generate Columns
            DataColumn indexColumn = dataTable.Columns.Add("Index");
            indexColumn.AutoIncrement = true;
            indexColumn.Unique        = true;
            dataTable.PrimaryKey      = new[] { indexColumn };
            outputAttributes.Add(null);

            FieldInfo[] fieldInfos = dataType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                OutputAttribute excelAttribute = ExcelFile.GetExcelAttribute(fieldInfo);

                // The only private field we add is the TableHeader
                if (fieldInfo.IsPrivate)
                {
                    if (fieldInfo.FieldType != typeof(ExcelFile.RowHeader))
                    {
                        continue;
                    }

                    outputAttributes.Add(null);
                    dataTable.Columns.Add(fieldInfo.Name, typeof(String));
                    continue;
                }

                Type fieldType = fieldInfo.FieldType;
                bool isArray   = false;
                bool isEnum    = false;
                if (fieldInfo.FieldType.BaseType == typeof(Array))
                {
                    fieldType = typeof(String);
                    isArray   = true;
                }
                else if (fieldInfo.FieldType.BaseType == typeof(Enum) && excelAttribute == null)
                {
                    fieldType = fieldInfo.FieldType;
                    isEnum    = true;
                }

                DataColumn dataColumn = dataTable.Columns.Add(fieldInfo.Name, fieldType);
                if (isArray)
                {
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsArray, true);
                }
                else if (isEnum)
                {
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsEnum, true);
                }

                if (excelAttribute == null)
                {
                    outputAttributes.Add(null);
                    continue;
                }

                outputAttributes.Add(excelAttribute);

                if (excelAttribute.IsStringOffset)
                {
                    dataColumn.DataType = typeof(String);
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsStringOffset, true);
                    dataColumn.DefaultValue = String.Empty;
                }

                if (excelAttribute.IsScript)
                {
                    dataColumn.DataType = typeof(String);
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsScript, true);
                    dataColumn.DefaultValue = String.Empty;
                }

                if (excelAttribute.IsSecondaryString)
                {
                    dataColumn.DataType = typeof(String);
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsSecondaryString, true);
                    dataColumn.DefaultValue = String.Empty;
                }

                if (excelAttribute.IsStringIndex)
                {
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsStringIndex, true);

                    // Add new column for the string
                    DataColumn dataColumnString = dataTable.Columns.Add(fieldInfo.Name + "_string", typeof(String));
                    dataColumnString.DefaultValue = String.Empty;
                    outputAttributes.Add(null);
                    dataColumnString.ExtendedProperties.Add(ColumnKeys.IsRelationGenerated, true);
                }

                if (excelAttribute.IsTableIndex)
                {
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsTableIndex, true);

                    // Add new column for the string
                    DataColumn dataColumnString = dataTable.Columns.Add(fieldInfo.Name + "_string", typeof(String));
                    dataColumnString.DefaultValue = String.Empty;
                    outputAttributes.Add(null);
                    dataColumnString.ExtendedProperties.Add(ColumnKeys.IsRelationGenerated, true);
                }

                if (excelAttribute.IsBitmask)
                {
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsBitmask, true);
                }

                if (excelAttribute.IsBool)
                {
                    dataColumn.ExtendedProperties.Add(ColumnKeys.IsBool, true);
                }
            }

            if (excelFile.Attributes.HasStats) // items, missiles, monsters, objects, players
            {
                DataColumn extendedDataColumn = dataTable.Columns.Add("Stats");
                extendedDataColumn.DataType = typeof(String);
                extendedDataColumn.ExtendedProperties.Add(ExcelFile.ColumnTypeKeys.IsStats, true);
                outputAttributes.Add(null);
            }
            #endregion

            #region Generate Rows
            int             row             = 1;
            object[]        baseRow         = new object[outputAttributes.Count];
            ObjectDelegator objectDelegator = new ObjectDelegator(fieldInfos);
            foreach (Object tableRow in excelFile.Rows)
            {
                int col = 1;
                foreach (FieldInfo fieldInfo in fieldInfos)
                {
                    Object value = objectDelegator[fieldInfo.Name](tableRow);

                    if (fieldInfo.IsPrivate)
                    {
                        if (fieldInfo.FieldType != typeof(ExcelFile.RowHeader))
                        {
                            continue;
                        }
                        baseRow[col++] = FileTools.ObjectToStringGeneric(value, ",");
                        continue;
                    }

                    OutputAttribute excelOutputAttribute = outputAttributes[col];

                    if (excelOutputAttribute == null)
                    {
                        if (value.GetType().BaseType == typeof(Array))
                        {
                            value = ((Array)value).ToString(",");
                        }

                        baseRow[col++] = value;
                        continue;
                    }

                    if (excelOutputAttribute.IsStringOffset)
                    {
                        int valueInt = (int)value;
                        baseRow[col++] = (valueInt != -1) ? excelFile.ReadStringTable(valueInt) : String.Empty;
                        continue;
                    }

                    if (excelOutputAttribute.IsSecondaryString)
                    {
                        int valueInt = (int)value;
                        baseRow[col++] = (valueInt != -1) ? excelFile.ReadSecondaryStringTable(valueInt) : String.Empty;
                        continue;
                    }

                    if (excelOutputAttribute.IsScript)
                    {
                        int scriptOffset = (int)value;
                        if (scriptOffset == 0)
                        {
                            baseRow[col++] = String.Empty;
                            continue;
                        }

                        String script;
                        if (scriptOffset == 9649 && excelFile.StringId == "SKILLS") // todo: not sure what's with this script...
                        {
                            /* Compiled Bytes:
                             * 26,30,700,6,26,1,399,358,669,616562688,711,26,62,3,17,669,641728512,26,8,711,26,62,3,17,358,669,322961408,26,5,700,6,26,1,399,358,388,0
                             *
                             * Ending Stack (FIFO):
                             * SetStat669('sfx_attack_pct', 'all', 30 * ($sklvl - 1))
                             * SetStat669('sfx_duration_pct', 'all', get_skill_level(@unit, 'Shield_Mastery'))
                             * SetStat669('damage_percent_skill', 8 * get_skill_level(@unit, 'Shield_Mastery'))
                             * SetStat669('damage_percent_skill', 8 * get_skill_level(@unit, 'Shield_Mastery')) + 5 * ($sklvl - 1)
                             *
                             * The last SetStat has strange overhang - decompiling wrong?
                             * Or is it "supposed" to be there?
                             * i.e. It's actually decompiling correctly, but because I've assumed such scripts to be wrong (as the end +5... segment is useless) we get the Stack exception
                             */
                            int[] scriptCode = excelFile.ReadScriptTable(scriptOffset);
                            script = scriptCode != null?FileTools.ArrayToStringGeneric(scriptCode, ",") : "ScriptError";

                            baseRow[col++] = script;
                            continue;
                        }

                        //if (fieldInfo.Name == "props1" && row == 45)
                        //{
                        //    int bp = 0;
                        //}

                        ExcelScript excelScript = new ExcelScript(this);

                        try
                        {
                            if (ExcelScript.DebugEnabled)
                            {
                                int[] scriptCode = excelFile.ReadScriptTable(scriptOffset);
                                script = scriptCode != null?FileTools.ArrayToStringGeneric(scriptCode, ",") : String.Empty;

                                script = excelScript.Decompile(excelFile.ScriptBuffer, scriptOffset, script, excelFile.StringId, row, col, fieldInfo.Name);
                            }
                            else
                            {
                                script = excelScript.Decompile(excelFile.ScriptBuffer, scriptOffset);
                            }

                            //if (script.StartsWith("GetStat666('skill_points_bonus_total', '') > -1;"))
                            //{
                            //    int bp = 0;
                            //}
                        }
                        catch (Exception)
                        {
                            int[] scriptCode = excelFile.ReadScriptTable(scriptOffset);
                            script = scriptCode != null?FileTools.ArrayToStringGeneric(scriptCode, ",") : "ScriptError";
                        }

                        baseRow[col++] = script;
                        continue;
                    }

                    if (excelOutputAttribute.IsTableIndex || excelOutputAttribute.IsStringIndex)
                    {
                        if (value.GetType().BaseType == typeof(Array))
                        {
                            value = ((Array)value).ToString(",");
                        }
                        else
                        {
                            value = (int)value;
                        }

                        baseRow[col++] = value;
                        col++; // for _strings relational column
                        continue;
                    }

                    // Else its something else, ie bitmask, bool, table/string index
                    baseRow[col++] = value;
                }

                // stats, only a component of the UnitData row type
                if (excelFile.Attributes.HasStats)
                {
                    baseRow[col++] = FileTools.ArrayToStringGeneric(excelFile.ReadStats(row - 1), ",");
                }

                dataTable.Rows.Add(baseRow);
                row++;
            }

            #endregion

            // Generate Relationships as required
            if (doRelations)
            {
                _GenerateRelations(excelFile);
            }

            return(dataTable);
        }