Esempio n. 1
0
        public static bool CookExcelFile(string excelPath)
        {
            byte[] excelBuffer = null;
            try
            {
                excelBuffer = File.ReadAllBytes(excelPath);
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
                Console.WriteLine(String.Format("Error reading file {0}", excelPath));
                return false;
            }

            ExcelFile excelFile = new ExcelFile(excelPath);
            try
            {
                excelFile.ParseCSV(excelBuffer, _fileManager);
            }
            catch (Exception e)
            {
                Console.WriteLine("Critical Error:\n" + e);
                return false;
            }
            if (excelFile.HasIntegrity == false)
            {
                Console.WriteLine(String.Format("Failed to parse excel file {0}", excelPath));
                return false;
            }

            Console.WriteLine(String.Format("Cooking {0}", Path.GetFileNameWithoutExtension(excelPath)));
            excelBuffer = excelFile.ToByteArray();
            if (excelBuffer == null)
            {
                Console.WriteLine(String.Format("Failed to serialize excel file {0}", excelFile.StringId));
                return false;
            }

            String writeToPath = excelPath + ".cooked";
            try
            {
                File.WriteAllBytes(writeToPath, excelBuffer);
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
                Console.WriteLine(String.Format("Failed to write cooked file {0} ", writeToPath));
                return false;
            }

            return true;
        }
Esempio n. 2
0
        /// <summary>
        /// This function is to test excel CSV cooking.
        /// </summary>
        public static void DoCookTest()
        {
            FileManager fileManager = new FileManager(Config.HglDir);
            fileManager.LoadTableFiles();
            fileManager.ExtractAllExcel();
            ExcelScript.GlobalDebug(true);

            foreach (PackFileEntry fileEntry in fileManager.FileEntries.Values)
            {
                if (!fileEntry.Name.EndsWith(ExcelFile.Extension)) continue;

                byte[] fileBytes = fileManager.GetFileBytes(fileEntry, true);
                Debug.Assert(fileBytes != null);
                String filePath = Path.Combine(Config.HglDir, fileEntry.Path);

                ExcelFile excelFile = new ExcelFile(fileBytes, fileEntry.Path);
                if (excelFile.Attributes.IsEmpty) continue;

                Console.WriteLine("Cooking file: " + fileEntry.Path);

                //if (!fileEntry.RelativeFullPathWithoutPatch.Contains("display_item")) continue;

                byte[] csvBytes = excelFile.ExportCSV(fileManager);
                File.WriteAllBytes(filePath.Replace(ExcelFile.Extension, ExcelFile.ExtensionDeserialised), csvBytes);
                ExcelFile excelFileCSV = new ExcelFile(fileEntry.Path);
                excelFileCSV.ParseCSV(csvBytes, fileManager);

                //byte[] recookedBytes = excelFileCSV.ToByteArray();
                byte[] csvBytes2 = excelFileCSV.ExportCSV(fileManager);

                //if (!csvBytes.SequenceEqual(csvBytes2))
                //{
                //    int b5p = 0;
                //}

                //if (excelFile.StringId == "GLOBAL_STRING")
                //{
                //    ExcelFile temp = new ExcelFile(recookedBytes, fileEntry.RelativeFullPathWithoutPatch);
                //    int bp1 = 0;
                //}

                //File.WriteAllBytes(filePath, recookedBytes);
            }

            //int bp = 0;
        }
Esempio n. 3
0
        /// <summary>
        /// Function to test all excel files cooking/recooking/etc to/from byte arrays and csv types.
        /// </summary>
        public static void TestExcelCooking(bool doTCv4 = false)
        {
            /*																0
            0   byte[] fileBytes = fileManager.GetFileBytes(excelFile.FilePath, true);	Bytes													0-gen bytes

                                                                            0      1           2        3            4
            foreach (DataFile dataFile in fileManager.DataFiles.Values)				Bytes->Xls												1-gen Xls file
            1   byte[] dataFileBytes = dataFile.ToByteArray();							Bytes->Xls->Bytes										1-gen bytes
            fromBytesExcel.ParseData(dataFileBytes);								Bytes->Xls->Bytes->Xls									2-gen Xls file
            1a  byte[] bytesXlsBytesXlsBytes = fromBytesExcel.ToByteArray();			Bytes->Xls->Bytes->Xls->Bytes							2-gen bytes
            1ba byte[] bytesXlsCsv = dataFile.ExportCSV(fileManager);					Bytes->Xls->CSV											1-gen CSV-bytes
            bytesXlsCsvXls.ParseCSV(bytesXlsCsv, fileManager);						Bytes->Xls->CSV  ->Xls									2-gen Xls file
            1bb byte[] bytesXlsCsvXlsBytes = bytesXlsCsvXls.ToByteArray();				Bytes->Xls->CSV  ->Xls->Bytes							2-gen CSV-bytes
            1b  byte[] csvBytes = fromBytesExcel.ExportCSV(fileManager);				Bytes->Xls->Bytes->Xls->CSV								2-gen CSV file
            csvExcel.ParseCSV(csvBytes, fileManager);								Bytes->Xls->Bytes->Xls->CSV->Xls						3-gen Xls file
            1c  byte[] bytesXlsBytesXlsCsvXlsBytes = csvExcel.ToByteArray();			Bytes->Xls->Bytes->Xls->CSV->Xls->Bytes					3-gen bytes
            finalExcel.ParseData(recookedExcelBytes);								Bytes->Xls->Bytes->Xls->CSV->Xls->Bytes->Xls			4-gen Xls file
            1d  byte[] csvCheck = finalExcel.ExportCSV(fileManager);					Bytes->Xls->Bytes->Xls->CSV->Xls->Bytes->Xls->CSV		4-gen CSV file
            1e  byte[] finalCheck = finalExcel.ToByteArray();							Bytes->Xls->Bytes->Xls->CSV->Xls->Bytes->Xls->Bytes		4-gen bytes
             */

            String root = @"C:\excel_debug";
            FileManager.ClientVersions clientVersion = FileManager.ClientVersions.SinglePlayer;
            if (doTCv4)
            {
                root = Path.Combine(root, "tcv4");
                clientVersion = FileManager.ClientVersions.TestCenter;
            }
            root += @"\"; // lazy
            Directory.CreateDirectory(root);

            FileManager fileManager = new FileManager(Config.HglDir, clientVersion);
            fileManager.BeginAllDatReadAccess();
            fileManager.LoadTableFiles();
            ExcelFile.EnableDebug = true;

            int checkedCount = 0;
            List<String> excelFailed = new List<String>();
            foreach (DataFile bytesXls in fileManager.DataFiles.Values)
            {
                ExcelFile excelFile = bytesXls as ExcelFile;
                if (excelFile == null) continue;

                const String debugStr = "SKILLS";
                //if (excelFile.StringId != debugStr) continue;
                //if (excelFile.StringId == debugStr)
                //{
                //    int bp = 0;
                //}

                checkedCount++;
                Debug.Write(String.Format("Checking {0}... ", bytesXls.StringId));

                byte[] bytes = fileManager.GetFileBytes(excelFile.FilePath, true);
                try
                {
                    byte[] bytesXlsBytes = bytesXls.ToByteArray();
                    if (bytesXls.StringId == "SOUNDS" && false)
                    {
                        byte[] csvBytesSounds = excelFile.ExportCSV(fileManager);
                        ExcelFile soundsCSV = new ExcelFile(excelFile.FilePath);
                        soundsCSV.ParseCSV(csvBytesSounds, fileManager);
                        byte[] soundsBytes = soundsCSV.ToByteArray();
                        //byte[] soundsBytesFromCSV = soundsCSV.ExportCSV();
                        //ExcelFile soundsCSVFromBytesFromCSV = new ExcelFile(soundsBytesFromCSV, fileEntry.RelativeFullPathWithoutPatch);

                        // some brute force ftw
                        byte[][] bytesArrays = new[] { bytes, soundsBytes };
                        for (int z = 0; z < bytesArrays.Length; z++)
                        {
                            byte[] bytesBrute = bytesArrays[z];

                            int offset = 0x20;
                            int stringsBytesCount = FileTools.ByteArrayToInt32(bytesBrute, ref offset);

                            StringWriter stringWriterByteStrings = new StringWriter();
                            stringWriterByteStrings.WriteLine(stringsBytesCount + " bytes");
                            List<String> strings = new List<String>();
                            List<int> offsets = new List<int>();

                            while (offset < stringsBytesCount + 0x20)
                            {
                                String str = FileTools.ByteArrayToStringASCII(bytesBrute, offset);
                                strings.Add(str);
                                offsets.Add(offset);

                                offset += str.Length + 1;
                            }

                            String[] sortedStrings = strings.ToArray();
                            int[] sortedOffsets = offsets.ToArray();
                            Array.Sort(sortedStrings, sortedOffsets);
                            stringWriterByteStrings.WriteLine(strings.Count + " strings");
                            for (int i = 0; i < strings.Count; i++)
                            {
                                stringWriterByteStrings.WriteLine(sortedStrings[i] + "\t\t\t" + sortedOffsets[i]);
                            }

                            File.WriteAllText(@"C:\excel_debug\strings" + z + ".txt", stringWriterByteStrings.ToString());
                        }
                    }

                    Debug.Write("ToByteArray... ");
                    if (bytes.Length != bytesXlsBytes.Length && !doTCv4) // some TCv4 tables don't have their sort columns yet
                    {
                        Debug.WriteLine("ToByteArray() dataFileBytes has differing length: " + bytesXls.StringId);
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    ExcelFile bytesXlsBytesXls = new ExcelFile(excelFile.FilePath, fileManager.ClientVersion);
                    bytesXlsBytesXls.ParseData(bytesXlsBytes);
                    Debug.Write("new ExcelFile... ");
                    if (!bytesXlsBytesXls.HasIntegrity)
                    {
                        Debug.WriteLine("fromBytesExcel = new Excel from ToByteArray() failed!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    // more checks
                    Debug.Write("ToByteArray->ToByteArray... ");
                    byte[] bytesXlsBytesXlsBytes = bytesXlsBytesXls.ToByteArray(); // bytesXlsBytesXlsBytes
                    if (bytes.Length != bytesXlsBytesXlsBytes.Length && !doTCv4) // some TCv4 tables don't have their sort columns yet
                    {
                        Debug.WriteLine("ToByteArray() dataFileBytesFromToByteArray has differing length!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1a.toByteArrayFromByteArray", bytesXlsBytesXlsBytes);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    // check generated sort index arrays);
                    Debug.Write("IndexSortArrays... ");
                    if (excelFile.IndexSortArray != null)
                    {
                        if (bytesXlsBytesXls.IndexSortArray == null || excelFile.IndexSortArray.Count != bytesXlsBytesXls.IndexSortArray.Count)
                        {
                            Debug.WriteLine("fromBytesExcel has not-matching IndexSortArray count!");
                            excelFailed.Add(bytesXls.StringId);
                            continue;
                        }

                        bool hasError = false;
                        for (int i = 0; i < excelFile.IndexSortArray.Count; i++)
                        {
                            if (excelFile.IndexSortArray[i].SequenceEqual(bytesXlsBytesXls.IndexSortArray[i])) continue;

                            Debug.WriteLine(String.Format("IndexSortArray[{0}] NOT EQUAL to original!", i));
                            hasError = true;
                        }

                        if (hasError)
                        {
                            File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                            excelFailed.Add(bytesXls.StringId);
                            continue;
                        }
                    }

                    // some csv stuff
                    Debug.Write("BaseXls->BaseCSV==");
                    byte[] bytesXlsCsv = bytesXls.ExportCSV(fileManager); // Bytes->Xls->CSV
                    Debug.Write("ExportCSV... ");
                    byte[] bytesXlsBytesXlsCsv = bytesXlsBytesXls.ExportCSV(fileManager);
                    if (!bytesXlsCsv.SequenceEqual(bytesXlsBytesXlsCsv))
                    {
                        Debug.WriteLine("FALSE!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    ExcelFile bytesXlsCsvXls = new ExcelFile(excelFile.FilePath, fileManager.ClientVersion);
                    bytesXlsCsvXls.ParseCSV(bytesXlsCsv, fileManager);
                    byte[] bytesXlsCsvXlsBytes = bytesXlsCsvXls.ToByteArray(); // used later as well
                    if (excelFile.ScriptBuffer == null && // can only compare here for no-script tables - can't compare to pure-base xls-bytes as xls->csv->xls rearranges script code
                        !excelFile.HasStringBuffer) // xls->csv->xls also rearranges the strings buffer
                    {
                        Debug.Write("BytesXlsBytesXlsBytes==BytesXlsCsvXlsBytes... ");
                        if (!bytesXlsBytesXlsBytes.SequenceEqual(bytesXlsCsvXlsBytes))
                        {
                            Debug.WriteLine("FALSE!");
                            File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                            File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                            File.WriteAllBytes(root + bytesXls.StringId + "1bb.bytesXlsCsvXlsBytes", bytesXlsCsvXlsBytes);
                            excelFailed.Add(bytesXls.StringId);
                            continue;
                        }

                        Debug.Write("BytesXlsBytes==BytesXlsCsvXlsBytes... ");
                        if (!bytesXlsBytes.SequenceEqual(bytesXlsCsvXlsBytes))
                        {
                            Debug.WriteLine("FALSE!");
                            File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                            File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                            File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                            File.WriteAllBytes(root + bytesXls.StringId + "1bb.bytesXlsCsvXlsBytes", bytesXlsCsvXlsBytes);
                            excelFailed.Add(bytesXls.StringId);
                            continue;
                        }
                    }

                    Debug.Write("BytesXlsBytesXlsCsv -> new ExcelFile");
                    ExcelFile bytesXlsBytesXlsCsvXls = new ExcelFile(excelFile.FilePath, fileManager.ClientVersion);
                    bytesXlsBytesXlsCsvXls.ParseCSV(bytesXlsBytesXlsCsv, fileManager);
                    Debug.Write("... ");
                    if (!bytesXlsBytesXlsCsvXls.HasIntegrity)
                    {
                        Debug.WriteLine("Failed!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    byte[] bytesXlsBytesXlsCsvXlsBytes = bytesXlsBytesXlsCsvXls.ToByteArray();
                    Debug.Write("BytesXlsCsvXlsBytes==BytesXlsBytesXlsCsvXlsBytes... ");
                    if (!bytesXlsCsvXlsBytes.SequenceEqual(bytesXlsBytesXlsCsvXlsBytes))
                    {
                        Debug.WriteLine("FALSE!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1bb.bytesXlsCsvXlsBytes", bytesXlsCsvXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1c.bytesXlsBytesXlsCsvXlsBytes", bytesXlsBytesXlsCsvXlsBytes);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    if (!fileManager.IsVersionTestCenter)
                    {
                        Debug.Write("StructureId... ");
                        UInt32 structureId = BitConverter.ToUInt32(bytes, 4);
                        UInt32 fromCSVStructureId = BitConverter.ToUInt32(bytesXlsBytesXlsCsvXlsBytes, 4);
                        if (structureId != fromCSVStructureId)
                        {
                            Debug.WriteLine("Structure Id value do not match: " + structureId + " != " + fromCSVStructureId);
                            excelFailed.Add(bytesXls.StringId);
                            continue;
                        }
                    }

                    Debug.Write("Almost Done... ");
                    ExcelFile bytesXlsBytesXlsCsvXlsBytesXls = new ExcelFile(excelFile.FilePath, fileManager.ClientVersion);
                    bytesXlsBytesXlsCsvXlsBytesXls.ParseData(bytesXlsBytesXlsCsvXlsBytes);
                    byte[] bytesXlsBytesXlsCsvXlsBytesXlsCsv = bytesXlsBytesXlsCsvXlsBytesXls.ExportCSV(fileManager);

                    int recookedLength = bytesXlsBytesXlsCsvXlsBytes.Length;
                    if (excelFile.StringId == "SKILLS") recookedLength += 12; // 12 bytes in int ptr data not used/referenced at all and are removed/lost in bytes -> csv -> bytes
                    if (bytes.Length != recookedLength && !doTCv4) // some TCv4 tables don't have their sort columns yet
                    {
                        Debug.WriteLine("Recooked Excel file has differing length!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1c.bytesXlsBytesXlsCsvXlsBytes", bytesXlsBytesXlsCsvXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1d.bytesXlsBytesXlsCsvXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsvXlsBytesXlsCsv);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    Debug.Assert(bytesXlsBytesXlsCsvXlsBytesXls.HasIntegrity);
                    byte[] bytesXlsBytesXlsCsvXlsBytesXlsBytes = bytesXlsBytesXlsCsvXlsBytesXls.ToByteArray();
                    if (excelFile.StringId == "SKILLS") Debug.Assert(bytesXlsBytesXlsCsvXlsBytesXlsBytes.Length + 12 == bytesXlsBytes.Length);
                    else Debug.Assert(bytesXlsBytesXlsCsvXlsBytesXlsBytes.Length == bytesXlsBytes.Length || doTCv4);

                    if (!bytesXlsBytesXlsCsv.SequenceEqual(bytesXlsBytesXlsCsvXlsBytesXlsCsv))
                    {
                        Debug.WriteLine("csvBytes.SequenceEqual failed!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsBytesXlsBytes", bytesXlsBytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1ba.bytesXlsCsv.csv", bytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1c.bytesXlsBytesXlsCsvXlsBytes", bytesXlsBytesXlsCsvXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1d.bytesXlsBytesXlsCsvXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsvXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + "1e.bytesXlsBytesXlsCsvXlsBytesXlsBytes", bytesXlsBytesXlsCsvXlsBytesXlsBytes);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    Debug.Write("\nPerforming deep scan: ");
                    ObjectDelegator objectDelegator = fileManager.DataFileDelegators[excelFile.StringId];
                    int lastPercent = 0;
                    int col = 0;
                    bool failed = false;
                    foreach (FieldDelegate fieldDelegate in objectDelegator)
                    {
                        int percent = col * 100 / objectDelegator.FieldCount - 1;
                        int dotCount = percent - lastPercent;
                        for (int i = 0; i < dotCount; i++) Debug.Write(".");

                        lastPercent = percent;

                        ExcelFile.OutputAttribute excelAttribute = ExcelFile.GetExcelAttribute(fieldDelegate.Info);
                        bool isArray = (fieldDelegate.FieldType.BaseType == typeof(Array));

                        for (int row = 0; row < excelFile.Rows.Count; row++)
                        {
                            Object obj1 = fieldDelegate.GetValue(excelFile.Rows[row]);
                            Object obj2 = fieldDelegate.GetValue(bytesXlsBytesXlsCsvXlsBytesXls.Rows[row]);

                            if (isArray)
                            {
                                //if (excelFile.StringId == "TREASURE")
                                //{
                                //    int bp = 0;
                                //}
                                IEnumerator arrayEnumator1 = ((Array)obj1).GetEnumerator();
                                IEnumerator arrayEnumator2 = ((Array)obj2).GetEnumerator();
                                int index = -1;
                                while (arrayEnumator1.MoveNext() && arrayEnumator2.MoveNext())
                                {
                                    index++;
                                    Object elementVal1 = arrayEnumator1.Current;
                                    Object elementVal2 = arrayEnumator2.Current;

                                    if (elementVal1.Equals(elementVal2)) continue;

                                    Debug.WriteLine(String.Format("Array Element '{0}' != '{1}' on col({2}) = '{3}', row({4}), index({5})", elementVal1, elementVal2, col, fieldDelegate.Name, row, index));
                                    failed = true;
                                    break;
                                }
                                if (failed) break;

                                continue;
                            }

                            if (obj1.Equals(obj2)) continue;

                            if (excelAttribute != null)
                            {
                                if (excelAttribute.IsStringOffset)
                                {
                                    int offset1 = (int)obj1;
                                    int offset2 = (int)obj2;

                                    String str1 = excelFile.ReadStringTable(offset1);
                                    String str2 = bytesXlsBytesXlsCsvXlsBytesXls.ReadStringTable(offset2);

                                    if (str1 == str2) continue;

                                    obj1 = str1;
                                    obj2 = str2;
                                }

                                if (excelAttribute.IsScript)
                                {
                                    int offset1 = (int)obj1;
                                    int offset2 = (int)obj2;

                                    Int32[] script1 = excelFile.ReadScriptTable(offset1);
                                    Int32[] script2 = bytesXlsBytesXlsCsvXlsBytesXls.ReadScriptTable(offset2);

                                    if (script1.SequenceEqual(script2)) continue;

                                    obj1 = script1.ToString(",");
                                    obj2 = script2.ToString(",");
                                }
                            }

                            String str = obj1 as String;
                            if ((str != null && str.StartsWith("+N%-N% base damage as [elem]")) ||  // "+N%-N% base damage as [elem]…" != "+N%-N% base damage as [elem]?" on col(2) = 'exampleDescription', row(132) // the '?' at end
                                fieldDelegate.Name == "bonusStartingTreasure")                      // refernces multiple different blank rows
                                continue;

                            Debug.WriteLine(String.Format("'{0}' != '{1}' on col({2}) = '{3}', row({4})", obj1, obj2, col, fieldDelegate.Name, row));
                            failed = true;
                            break;
                        }
                        if (failed) break;

                        col++;
                    }

                    if (failed)
                    {
                        File.WriteAllBytes(root + bytesXls.StringId + ".bytesXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsv);
                        File.WriteAllBytes(root + bytesXls.StringId + ".bytesXlsBytesXlsCsvXlsBytesXlsCsv.csv", bytesXlsBytesXlsCsvXlsBytesXlsCsv);
                        excelFailed.Add(bytesXls.StringId);
                        continue;
                    }

                    Debug.WriteLine("OK\n");
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Excel file Exception: " + bytesXls.StringId + "\n" + e);
                }
            }

            Debug.WriteLine(checkedCount + " excel files checked.");

            if (excelFailed.Count <= 0) return;
            Debug.WriteLine(excelFailed.Count + " failed excel checks! (" + ((float)excelFailed.Count / checkedCount * 100) + "%)");
            foreach (String str in excelFailed) Debug.Write(str + ", ");
        }
Esempio n. 4
0
        /// <summary>
        /// Function to test saving/importing/exported/etc relating to DataTables (i.e. actions from WITHIN Reanimator etc.)
        /// </summary>
        public static void TestDataTableExportAndImport(bool doTCv4 = false)
        {
            /*
            bytesXls						Bytes->Xls
            bytesXlsBytes					Bytes->Xls->Bytes
            bytesXlsCsv						Bytes->Xls->CSV
            bytesXlsCsvXls					Bytes->Xls->CSV->Xls
            bytesXlsCsvXlsBytes				Bytes->Xls->CSV->Xls->Bytes					== bytesXlsTableXlsBytes as single byte is stripped ->CSV

            bytesXlsTable					Bytes->Xls->Table
            bytesXlsTableXls				Bytes->Xls->Table->Xls
            bytesXlsTableXlsBytes			Bytes->Xls->Table->Xls->Bytes				!= bytesXlsBytes because of single script byte thingy lost to table
            bytesXlsTableXlsCsv				Bytes->Xls->Table->Xls->CSV
            bytesXlsTableXlsCsvXls			Bytes->Xls->Table->Xls->CSV->Xls
            bytesXlsTableXlsCsvXlsBytes		Bytes->Xls->Table->Xls->CSV->Xls->Bytes

            byte[] bytesXlsBytes				= bytesXls.ToByteArray();
            byte[] bytesXlsTableXlsCsvXlsBytes	= bytesXlsTableXlsCsvXls.ToByteArray();
             */

            String root = @"C:\excel_datatable_debug";
            FileManager.ClientVersions clientVersion = FileManager.ClientVersions.SinglePlayer;
            if (doTCv4)
            {
                root = Path.Combine(root, "tcv4");
                clientVersion = FileManager.ClientVersions.TestCenter;
            }
            root += @"\"; // lazy
            Directory.CreateDirectory(root);

            FileManager fileManager = new FileManager(Config.HglDir, clientVersion);
            fileManager.BeginAllDatReadAccess();
            fileManager.LoadTableFiles();

            List<String> sequenceFailed = new List<String>();
            List<String> sequenceChecked = new List<String>();
            foreach (KeyValuePair<String, DataFile> keyValuePair in fileManager.DataFiles)
            {
                String stringId = keyValuePair.Key;
                DataFile dataFile = keyValuePair.Value;
                if (dataFile.IsStringsFile) continue;

                //if (stringId != "WARDROBE_LAYER") continue;
                //if (stringId == "WARDROBE_LAYER")
                //{
                //    int bp = 0;
                //}

                Debug.Write("Checking " + stringId + "... ");
                ExcelFile bytesXls = (ExcelFile)dataFile;
                byte[] bytes = fileManager.GetFileBytes(bytesXls.FilePath, true);
                sequenceChecked.Add(stringId);
                sequenceFailed.Add(stringId);

                if (stringId == "PROPERTIES") continue; // can't do this atm - need to export Properties script table to DataTable etc.

                Debug.Write("DataTable... ");
                DataTable bytesXlsTable = fileManager.GetDataTable(stringId);
                if (bytesXlsTable == null)
                {
                    Debug.WriteLine("FAILED!");
                    continue;
                }

                // parse as DataTable
                Debug.Write("new Excel... ");
                DataFile bytesXlsTableXls = new ExcelFile(bytesXls.FilePath, fileManager.ClientVersion);
                if (!bytesXlsTableXls.ParseDataTable(bytesXlsTable, fileManager))
                {
                    Debug.WriteLine("FAILED!");
                    continue;
                }

                // check just data table import/export
                byte[] bytesXlsBytes = bytesXls.ToByteArray();
                byte[] bytesXlsTableXlsBytes = bytesXlsTableXls.ToByteArray();
                if (bytesXls.ScriptBuffer == null && // can't check this as ScriptBuffer[0] is NOT always 0x00 (even though it's unused and means no script normally when pointed to)
                    !bytesXls.HasStringBuffer) // as above - gets re-arranged etc (only consistant comparint CSV->XLS->DATA
                {
                    Debug.Write("bytesXlsBytes==bytesXlsTableXlsBytes... ");
                    if (!bytesXlsBytes.SequenceEqual(bytesXlsTableXlsBytes))
                    {
                        Debug.WriteLine("FALSE!");
                        File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                        File.WriteAllBytes(root + bytesXls.StringId + "2.bytesXlsTableXlsBytes", bytesXlsTableXlsBytes);
                        continue;
                    }
                }

                // export CSV
                Debug.Write("ExportCSV... ");
                byte[] bytesXlsTableXlsCsv = bytesXlsTableXls.ExportCSV(fileManager);
                if (bytesXlsTableXlsCsv == null)
                {
                    Debug.WriteLine("FAILED!");
                    continue;
                }

                Debug.Write("bytesXlsCsv==bytesXlsTableXlsCsv... ");
                byte[] bytesXlsCsv = bytesXls.ExportCSV(fileManager);
                if (!bytesXlsCsv.SequenceEqual(bytesXlsTableXlsCsv))
                {
                    Debug.WriteLine("FALSE!");
                    File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsCsv.csv", bytesXlsCsv);
                    File.WriteAllBytes(root + bytesXls.StringId + "2.bytesXlsTableXlsBytes", bytesXlsTableXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "3.bytesXlsTableXlsCsv.csv", bytesXlsTableXlsCsv);
                    continue;
                }

                Debug.Write("bytesXlsCsvXlsBytes==bytesXlsTableXlsBytes... ");
                DataFile bytesXlsCsvXls = new ExcelFile(bytesXls.FilePath, fileManager.ClientVersion);
                bytesXlsCsvXls.ParseCSV(bytesXlsCsv, fileManager);
                byte[] bytesXlsCsvXlsBytes = bytesXlsCsvXls.ToByteArray();
                if (!bytesXlsCsvXlsBytes.SequenceEqual(bytesXlsTableXlsBytes) &&
                    stringId != "ITEMDISPLAY" && stringId != "LEVEL") // ITEMDISPLAY has weird single non-standard ASCII char that is lost (from using MS Work on their part lol)
                {                                                       // and LEVEL references blank TREASURE rows and hence rowId references differ on recooks

                    Debug.WriteLine("FALSE!");
                    File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "1a.bytesXlsCsv.csv", bytesXlsCsv);
                    File.WriteAllBytes(root + bytesXls.StringId + "1b.bytesXlsCsvXlsBytes", bytesXlsCsvXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "2.bytesXlsTableXlsBytes", bytesXlsTableXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "3.bytesXlsTableXlsCsv.csv", bytesXlsTableXlsCsv);
                    continue;
                }

                // import CSV
                Debug.Write("ParseCSV... ");
                DataFile bytesXlsTableXlsCsvXls = new ExcelFile(bytesXls.FilePath, fileManager.ClientVersion);
                if (!bytesXlsTableXlsCsvXls.ParseCSV(bytesXlsTableXlsCsv, fileManager))
                {
                    Debug.WriteLine("FAILED!");
                    continue;
                }

                // export imported as cooked and do byte compare
                Debug.Write("ToByteArray... ");
                byte[] bytesXlsTableXlsCsvXlsBytes = bytesXlsTableXlsCsvXls.ToByteArray();
                Debug.Write("bytesXlsCsvXlsBytes==bytesXlsTableXlsCsvXlsBytes... ");
                if (!bytesXlsCsvXlsBytes.SequenceEqual(bytesXlsTableXlsCsvXlsBytes))
                {
                    Debug.WriteLine("FALSE!");
                    File.WriteAllBytes(root + bytesXls.StringId + "0.bytes", bytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "1.bytesXlsBytes", bytesXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "2.bytesXlsTableXlsBytes", bytesXlsTableXlsBytes);
                    File.WriteAllBytes(root + bytesXls.StringId + "3.bytesXlsTableXlsCsv", bytesXlsTableXlsCsv);
                    File.WriteAllBytes(root + bytesXls.StringId + "4.bytesXlsTableXlsCsvXlsBytes", bytesXlsTableXlsCsvXlsBytes);
                    continue;
                }

                // yay
                Debug.WriteLine("OK!");
                sequenceFailed.RemoveAt(sequenceFailed.Count - 1);
            }

            Debug.WriteLine("Totals: {0} sequence checks, {1} fails. ({2}% failed!)", sequenceChecked.Count, sequenceFailed.Count, (float)sequenceFailed.Count * 100f / sequenceChecked.Count);
            Debug.Write("Failed Tables: ");
            foreach (String stringIdFailed in sequenceFailed)
            {
                Debug.Write(stringIdFailed + ", ");
            }
        }
Esempio n. 5
0
        private static void _CookExcelFiles(IEnumerable<String> excelFilesToCook)
        {
            Dictionary<String, ExcelFile> excelFiles = new Dictionary<String, ExcelFile>();

            Console.WriteLine("Reading Excel CSV content...");
            foreach (String excelPath in excelFilesToCook)
            {
                Console.Write(Path.GetFileName(excelPath) + "... ");

                byte[] fileBytes;
                try
                {
                    fileBytes = File.ReadAllBytes(excelPath);
                }
                catch (Exception e)
                {
                    ExceptionLogger.LogException(e);

                    Console.WriteLine("\nFailed to read file contents!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }

                ExcelFile excelFile = new ExcelFile(excelPath);
                try
                {
                    excelFile.LoadCSV(fileBytes);
                }
                catch (Exception e)
                {
                    ExceptionLogger.LogException(e);

                    Console.WriteLine("\nFailed to load CSV contents!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }

                excelFiles.Add(excelFile.StringId, excelFile);
            }

            if (excelFiles.Count == 0) return;

            Console.WriteLine("\nProcessing Excel CSV content...");
            foreach (ExcelFile excelFile in excelFiles.Values)
            {
                Console.WriteLine("Cooking " + Path.GetFileName(excelFile.FilePath));

                try
                {
                    excelFile.ParseCSV(_fileManager, excelFiles);
                }
                catch (Exception e)
                {
                    ExceptionLogger.LogException(e);

                    Console.WriteLine("Failed to parse CSV data!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }

                if (excelFile.HasIntegrity == false)
                {
                    Console.WriteLine("Failed to parse CSV data!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }

                byte[] cookedFileBytes;
                try
                {
                    cookedFileBytes = excelFile.ToByteArray();
                }
                catch (Exception e)
                {
                    ExceptionLogger.LogException(e);

                    Console.WriteLine("Failed to serialise CSV data!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }

                if (cookedFileBytes == null)
                {
                    Console.WriteLine("Failed to serialise CSV data!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }

                String savePath = excelFile.FilePath.Replace(ExcelFile.ExtensionDeserialised, ExcelFile.Extension);
                try
                {
                    File.WriteAllBytes(savePath, cookedFileBytes);
                }
                catch (Exception e)
                {
                    ExceptionLogger.LogException(e);

                    Console.WriteLine("Failed to write cooked excel file!\nIgnore and Continue? [Y/N]: ");
                    char c = (char)Console.Read();
                    if (c == 'y' || c == 'Y') continue;

                    return;
                }
            }
        }