Пример #1
0
        public static void exportCache(CacheCounter counter)
        {
            if (!EditMode)
            {
                throw new InvalidOperationException("Cannot export the cache unless the game is in edit mode");
            }

            int fileCount = 0;

            foreach (Type type in cache.Keys)
            {
                fileCount += cache[type].Count;
            }

            counter.totalObjects       = fileCount;
            counter.consumedObjects    = 0;
            counter.operationsFinished = false;

            Log.info("Starting data cache export.");

            foreach (Type type in cache.Keys)
            {
                counter.currentOperation = "Saving " + type.Name + " data...";

                foreach (string name in cache[type].Keys)
                {
                    string path = typeDirectories[type] + name + typeExtensions[type];
                    exportData(path, type, name);

                    if (Log.Debugging)
                    {
                        Log.info("Exported " + name + " of type" + type);
                    }

                    counter.consumedObjects++;
                }
            }

            counter.currentOperation   = "Saving complete!";
            counter.operationsFinished = true;
            Log.info("Data cache export complete.");
        }
Пример #2
0
        public static void importCache(CacheCounter counter)
        {
            SortedDictionary <Type, string[]> files = new SortedDictionary <Type, string[]>(new TypeComparer());

            counter.currentOperation   = "Discovering data files...";
            counter.totalObjects       = typeDirectories.Count;
            counter.consumedObjects    = 0;
            counter.operationsFinished = false;

            int totalFiles = 0;

            Log.info("Starting import of data cache.");

            if (Log.Debugging)
            {
                Log.info("Searching datafile directories.");
            }

            foreach (Type type in typeDirectories.Keys)
            {
                string   directory = typeDirectories[type];
                string   extension = typeExtensions[type];
                string[] fileNames = Directory.GetFiles(directory, "*" + extension);

                Array.Sort(fileNames);
                files.Add(type, fileNames);

                counter.consumedObjects++;
                totalFiles += fileNames.Length;

                if (Log.Debugging)
                {
                    Log.info("Found " + fileNames.Length + " files of type " + type + " at " + directory);
                }

                System.Threading.Thread.Sleep(1000);
            }

            counter.totalObjects    = totalFiles;
            counter.consumedObjects = 0;

            if (Log.Debugging)
            {
                Log.info("Reading in file streams.");
            }

            //Make this multithreaded if speed becomes an issue (KISS for now)
            using (MD5 md5 = MD5.Create())
            {
                byte[][] rawChecksum = new byte[totalFiles][];

                foreach (Type type in files.Keys)
                {
                    counter.currentOperation = "Loading " + type.Name + " data...";
                    string[] fileNames = files[type];

                    foreach (string file in fileNames)
                    {
                        string name = Path.GetFileNameWithoutExtension(file);
                        importData(file, type, name);

                        //if (!EditMode) //Diasable check for debugging purposes
                        {
                            rawChecksum[counter.consumedObjects] = md5.ComputeHash(cache[type][name]);
                        }

                        counter.consumedObjects++;

                        if (Log.Debugging)
                        {
                            Log.info("Imported " + name + " of type " + type);
                        }
                    }
                }

                //if (!EditMode) // Disable check for debugging purposes
                {
                    byte[] rawFilesum   = rawChecksum.SelectMany(x => x).ToArray();
                    byte[] byteChecksum = md5.TransformFinalBlock(rawFilesum, 0, rawFilesum.Length);
                    string checksum     = BitConverter.ToString(byteChecksum).Replace("-", "");

                    if (!EditMode) // Enable/Disable check for debugging purposes
                    {
                        if (!checksum.Equals(Game.versionChecksum))
                        {
                            throw new DataException("Checksum mismatch.  Data files have been corrupted.");
                        }
                    }

                    if (Log.Debugging)
                    {
                        Log.info(checksum);
                    }

                    Log.info("Data cache import checksum validated.");
                }
            }

            counter.currentOperation   = "Loading complete!";
            counter.operationsFinished = true;
            Log.info("Data cache import complete.");
        }