|
| 1 | +using System.IO; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.IO.Compression; |
| 5 | +using System.Collections; |
| 6 | + |
| 7 | +namespace tModUnpacker |
| 8 | +{ |
| 9 | + struct tModFileInfo |
| 10 | + { |
| 11 | + public long filestart; |
| 12 | + public long filesize; |
| 13 | + } |
| 14 | + |
| 15 | + struct tModFile |
| 16 | + { |
| 17 | + public string path; |
| 18 | + public long size; |
| 19 | + public byte[] data; |
| 20 | + } |
| 21 | + |
| 22 | + struct tModInfo |
| 23 | + { |
| 24 | + public string modloaderversion; |
| 25 | + public string modversion; |
| 26 | + public string modname; |
| 27 | + public byte[] modhash; |
| 28 | + public byte[] modsignature; |
| 29 | + public int filecount; |
| 30 | + } |
| 31 | + |
| 32 | + class tMod : IEnumerable<tModFile>, IEnumerable |
| 33 | + { |
| 34 | + public tModInfo info; |
| 35 | + |
| 36 | + public IDictionary<string, tModFileInfo> files = new Dictionary<string, tModFileInfo>(); |
| 37 | + |
| 38 | + private string path; |
| 39 | + |
| 40 | + private FileStream tempfile; |
| 41 | + private string tempfilepath = Path.GetTempFileName(); |
| 42 | + |
| 43 | + internal tMod(string path) |
| 44 | + { |
| 45 | + this.tempfile = new FileStream( |
| 46 | + this.tempfilepath, |
| 47 | + FileMode.OpenOrCreate, |
| 48 | + FileAccess.ReadWrite, |
| 49 | + FileShare.Read, |
| 50 | + 4096, |
| 51 | + FileOptions.DeleteOnClose | FileOptions.RandomAccess |
| 52 | + ); |
| 53 | + this.path = path; |
| 54 | + } |
| 55 | + |
| 56 | + ~tMod() |
| 57 | + { |
| 58 | + if (this.tempfile != null) |
| 59 | + this.tempfile.Close(); |
| 60 | + } |
| 61 | + |
| 62 | + public bool HasFile(string path) |
| 63 | + { |
| 64 | + return this.files.ContainsKey(path.Replace("\\", "/")); |
| 65 | + } |
| 66 | + |
| 67 | + public byte[] GetFile(string path) |
| 68 | + { |
| 69 | + path = path.Replace("\\", "/"); |
| 70 | + if (HasFile(path)) |
| 71 | + { |
| 72 | + tModFileInfo file = this.files[path]; |
| 73 | + this.tempfile.Seek(file.filestart, SeekOrigin.Begin); |
| 74 | + byte[] data = new byte[file.filesize]; |
| 75 | + this.tempfile.Read(data, 0, (int)file.filesize); |
| 76 | + |
| 77 | + return data; |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + public void WriteFile(string path, byte[] data) |
| 83 | + { |
| 84 | + string dirpath = Path.GetDirectoryName(path); |
| 85 | + if (!Directory.Exists(dirpath)) |
| 86 | + { |
| 87 | + Directory.CreateDirectory(dirpath); |
| 88 | + } |
| 89 | + File.WriteAllBytes(path, data); |
| 90 | + } |
| 91 | + |
| 92 | + public bool ReadMod() |
| 93 | + { |
| 94 | + tModInfo info = new tModInfo(); |
| 95 | + using (FileStream fileStream = File.OpenRead(this.path)) |
| 96 | + { |
| 97 | + BinaryReader binaryReader = new BinaryReader(fileStream); |
| 98 | + if (Encoding.ASCII.GetString(binaryReader.ReadBytes(4)) != "TMOD") |
| 99 | + return false; |
| 100 | + |
| 101 | + info.modloaderversion = binaryReader.ReadString(); |
| 102 | + info.modhash = binaryReader.ReadBytes(20); |
| 103 | + info.modsignature = binaryReader.ReadBytes(256); |
| 104 | + |
| 105 | + fileStream.Seek(4, SeekOrigin.Current); |
| 106 | + |
| 107 | + DeflateStream inflateStream = new DeflateStream(fileStream, CompressionMode.Decompress); |
| 108 | + inflateStream.CopyTo(this.tempfile); |
| 109 | + inflateStream.Close(); |
| 110 | + this.tempfile.Seek(0, SeekOrigin.Begin); |
| 111 | + BinaryReader tempFileBinaryReader = new BinaryReader(this.tempfile); |
| 112 | + |
| 113 | + info.modname = tempFileBinaryReader.ReadString(); |
| 114 | + info.modversion = tempFileBinaryReader.ReadString(); |
| 115 | + info.filecount = tempFileBinaryReader.ReadInt32(); |
| 116 | + for (int index = 0; index < info.filecount; index++) |
| 117 | + { |
| 118 | + tModFileInfo file = new tModFileInfo(); |
| 119 | + string path = tempFileBinaryReader.ReadString().Replace("\\", "/"); |
| 120 | + file.filesize = tempFileBinaryReader.ReadInt32(); |
| 121 | + file.filestart = this.tempfile.Position; |
| 122 | + this.tempfile.Seek(file.filesize, SeekOrigin.Current); |
| 123 | + this.files.Add(path, file); |
| 124 | + } |
| 125 | + this.info = info; |
| 126 | + return true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public bool DumpFile(string outputpath, string filename) |
| 131 | + { |
| 132 | + string path = Path.Combine(outputpath, this.info.modname, filename); |
| 133 | + byte[] data = this.GetFile(filename); |
| 134 | + |
| 135 | + if (data == null) |
| 136 | + return false; |
| 137 | + |
| 138 | + WriteFile(path, data); |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + public void DumpFiles(string outputpath) |
| 143 | + { |
| 144 | + foreach (string fileName in this.files.Keys) |
| 145 | + { |
| 146 | + DumpFile(outputpath, fileName); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public void DumpFiles(string outputpath, System.Action<string> func) |
| 151 | + { |
| 152 | + foreach (string fileName in this.files.Keys) |
| 153 | + { |
| 154 | + func(fileName); |
| 155 | + DumpFile(outputpath, fileName); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public void DumpFiles(string outputpath, System.Func<string, bool> func) |
| 160 | + { |
| 161 | + foreach (string fileName in this.files.Keys) |
| 162 | + { |
| 163 | + if (!func(fileName)) |
| 164 | + continue; |
| 165 | + DumpFile(outputpath, fileName); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public IEnumerator<tModFile> GetEnumerator() |
| 170 | + { |
| 171 | + foreach (string path in this.files.Keys) |
| 172 | + { |
| 173 | + tModFile file = new tModFile(); |
| 174 | + file.path = path; |
| 175 | + file.data = this.GetFile(path); |
| 176 | + file.size = file.data != null ? file.data.Length : 0; |
| 177 | + yield return file; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + IEnumerator IEnumerable.GetEnumerator() |
| 182 | + { |
| 183 | + return this.GetEnumerator(); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments