Пример #1
0
        public bool BuildCPK(string buildFile)
        {
            if (!DeserializeFromDisk(buildFile))
            {
                return(false);
            }

            List <CPKEmbeddedFileMeta> changedFiles = new List <CPKEmbeddedFileMeta>();

            foreach (var file in files.Values)
            {
                string filePath = Path.Combine(ProjectFolder.GetRootDir(), file.filePath);

                if (!File.Exists(filePath))
                {
                    string errorMessage = string.Format("File {0} did not exist.", filePath);
                    MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                FileStream   fs = new FileStream(filePath, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);

                byte[] fileAsBytes = br.ReadBytes((int)br.BaseStream.Length);

                br.Close();
                fs.Close();

                string checksum = Checksums.GetMD5(fileAsBytes);


                if (checksum != file.checksumValue) // file has been changed
                {
                    changedFiles.Add(file);
                }
            }

            string originalCPKPath = Path.Combine(ProjectFolder.GetRootDir(), originalFileLocation);
            string targetCPKPath   = Path.Combine(ProjectFolder.GetRootDir(), targetFileLocation);

            if (DebugSettings.REFRESH_REPACKED_FILES_ON_BUILD)
            {
                if (File.Exists(targetCPKPath)) // refresh files in repacked files folder just in case since 1) we will be using the repacked folder as our base to merge new changes into 2) a rebuild implies a refresh. perhaps some distinction between "build" and "clean build" would be a good idea later, which case it will most likely derive from this.
                {
                    File.Delete(targetCPKPath);
                }
                else
                {
                    DirectoryGuard.CheckDirectory(targetCPKPath);
                }
                File.Copy(originalCPKPath, targetCPKPath);
            }

            if (changedFiles.Count > 0)
            {
                var batchReplaceArg = new Dictionary <string, string>();

                foreach (var file in changedFiles)
                {
                    string fileName = file.fileName;

                    if (!fileName.Contains("/")) // done to match the format used by the batch replacer
                    {
                        fileName = "/" + fileName;
                    }

                    batchReplaceArg[fileName] = Path.Combine(ProjectFolder.GetRootDir(), file.filePath);
                }

                ReplaceCPKFiles(originalCPKPath, targetCPKPath, batchReplaceArg);
            }


            return(true);
        }