|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/EchoTools/evrFileTools/pkg/manifest" |
| 11 | +) |
| 12 | + |
| 13 | +func runPatch() error { |
| 14 | + // 1. Read manifest from dataDir/manifests/packageName. |
| 15 | + manifestPath := filepath.Join(dataDir, "manifests", packageName) |
| 16 | + m, err := manifest.ReadFile(manifestPath) |
| 17 | + if err != nil { |
| 18 | + return fmt.Errorf("read manifest: %w", err) |
| 19 | + } |
| 20 | + fmt.Printf("Manifest loaded: %d files in %d packages\n", m.FileCount(), m.PackageCount()) |
| 21 | + |
| 22 | + // 2. Read replacement file from patchInput. |
| 23 | + data, err := os.ReadFile(patchInput) |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("read patch input file %s: %w", patchInput, err) |
| 26 | + } |
| 27 | + fmt.Printf("Replacement file read: %d bytes\n", len(data)) |
| 28 | + |
| 29 | + // 3. Parse patchType and patchFile hex strings. |
| 30 | + typeSymbol, err := parseHexSymbol(patchType) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("parse -patch-type %q: %w", patchType, err) |
| 33 | + } |
| 34 | + fileSymbol, err := parseHexSymbol(patchFile) |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("parse -patch-file %q: %w", patchFile, err) |
| 37 | + } |
| 38 | + |
| 39 | + // 4. Copy original package files to outputDir/packages/. |
| 40 | + srcPkgDir := filepath.Join(dataDir, "packages") |
| 41 | + dstPkgDir := filepath.Join(outputDir, "packages") |
| 42 | + if err := os.MkdirAll(dstPkgDir, 0755); err != nil { |
| 43 | + return fmt.Errorf("create output packages dir: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + if err := copyPackageFiles(srcPkgDir, dstPkgDir, packageName, m.PackageCount()); err != nil { |
| 47 | + return fmt.Errorf("copy package files: %w", err) |
| 48 | + } |
| 49 | + fmt.Printf("Copied %d package file(s) to %s\n", m.PackageCount(), dstPkgDir) |
| 50 | + |
| 51 | + // 5. Call manifest.PatchFile with the copied package base path. |
| 52 | + pkgBasePath := filepath.Join(dstPkgDir, packageName) |
| 53 | + updated, err := manifest.PatchFile(m, pkgBasePath, typeSymbol, fileSymbol, data) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("patch file: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + // 6. Write updated manifest to outputDir/manifests/packageName. |
| 59 | + manifestsDir := filepath.Join(outputDir, "manifests") |
| 60 | + if err := os.MkdirAll(manifestsDir, 0755); err != nil { |
| 61 | + return fmt.Errorf("create output manifests dir: %w", err) |
| 62 | + } |
| 63 | + outManifestPath := filepath.Join(manifestsDir, packageName) |
| 64 | + if err := manifest.WriteFile(outManifestPath, updated); err != nil { |
| 65 | + return fmt.Errorf("write updated manifest: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + fmt.Printf("Patch complete. Updated manifest written to %s\n", outManifestPath) |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// parseHexSymbol parses a hex string (with or without 0x prefix) into int64. |
| 73 | +func parseHexSymbol(s string) (int64, error) { |
| 74 | + // Strip optional 0x prefix. |
| 75 | + trimmed := s |
| 76 | + if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { |
| 77 | + trimmed = s[2:] |
| 78 | + } |
| 79 | + v, err := strconv.ParseUint(trimmed, 16, 64) |
| 80 | + if err != nil { |
| 81 | + return 0, fmt.Errorf("invalid hex value %q: %w", s, err) |
| 82 | + } |
| 83 | + return int64(v), nil |
| 84 | +} |
| 85 | + |
| 86 | +// copyPackageFiles copies all package files (<name>_0, <name>_1, ...) from src to dst dir. |
| 87 | +func copyPackageFiles(srcDir, dstDir, name string, count int) error { |
| 88 | + for i := 0; i < count; i++ { |
| 89 | + filename := fmt.Sprintf("%s_%d", name, i) |
| 90 | + src := filepath.Join(srcDir, filename) |
| 91 | + dst := filepath.Join(dstDir, filename) |
| 92 | + if err := copyFile(src, dst); err != nil { |
| 93 | + return fmt.Errorf("copy %s: %w", filename, err) |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// copyFile copies a single file from src to dst, creating dst if necessary. |
| 100 | +func copyFile(src, dst string) error { |
| 101 | + in, err := os.Open(src) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + defer in.Close() |
| 106 | + |
| 107 | + out, err := os.Create(dst) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + defer out.Close() |
| 112 | + |
| 113 | + if _, err := io.Copy(out, in); err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + return out.Sync() |
| 117 | +} |
0 commit comments