public static void SaveCache() { if (!CacheReady) { return; } try { TLBCache Cache = new TLBCache() { Signature = "TLBC", Original = Program.Cache.Keys.OfType <string>().ToArray(), Translations = Program.Cache.Values.OfType <string>().ToArray(), ManualString = ForceDialogues.Keys.OfType <string>().ToArray(), ManualChecked = ForceDialogues.Values.OfType <bool>().ToArray() }; if (File.Exists(CachePath)) { File.Delete(CachePath); } using (StructWriter Writer = new StructWriter(CachePath)) { Writer.WriteStruct(ref Cache); Writer.Close(); } } catch { } SaveTask(); }
public byte[] Export(string[] Strings) { uint EntryLen = (uint)Tools.GetStructLength(new Entry()); byte[] NewScript = new byte[this.Header.TextTableOffset]; Array.Copy(Script, NewScript, NewScript.Length); MemoryStream StringBuffer = new MemoryStream(); Entry[] EntryArr = Entries.ToArray(); StructWriter Writer = new StructWriter(StringBuffer, Encoding.Unicode); uint Length = 0; for (int i = 0; i < Strings.Length; i++) { dynamic Struct = EntryArr[i]; Struct.TextPos = Length; Struct.TextUniLength = (uint)Strings[i].Length; Overwrite(ref NewScript, Tools.BuildStruct(ref Struct), Header.ByteCodeLength + Header.ByteCodeStart + (EntryLen * (uint)i)); Length += Struct.TextUniLength + 1; Writer.Write(Strings[i], StringStyle.UCString); } Header.TextUniTableLength = (uint)StringBuffer.Length / 2; Header.TextTableLength = (uint)StringBuffer.Length; dynamic H = Header; Tools.BuildStruct(ref H).CopyTo(NewScript, 0); NewScript = NewScript.Concat(StringBuffer.ToArray()).ToArray(); Writer.Close(); StringBuffer?.Close(); return(NewScript); }
static public void Export(File[] Files, Stream Output, bool CloseStreams = true) { StructWriter Writer = new StructWriter(Output, Encoding: Encoding.Unicode); uint DataInfoLen = 0; foreach (File f in Files) { DataInfoLen += (uint)Tools.GetStructLength(f, Encoding.Unicode); } ArcHeader Header = new ArcHeader() { Count = (uint)Files.LongLength, BaseOffset = DataInfoLen }; Writer.WriteStruct(ref Header); uint Ptr = 0; for (uint i = 0; i < Files.LongLength; i++) { Files[i].Offset = Ptr; Files[i].Length = (uint)Files[i].Content.Length; Ptr += Files[i].Length; Writer.WriteStruct(ref Files[i]); } for (uint i = 0; i < Files.LongLength; i++) { int Readed = 0; uint TotalLen = 0; byte[] Buffer = new byte[1024 * 1024]; do { Readed = Files[i].Content.Read(Buffer, 0, Buffer.Length); Output.Write(Buffer, 0, Readed); TotalLen += (uint)Readed; } while (Readed > 0); Output.Flush(); //System.Diagnostics.Debug.Assert(TotalLen == Files[i].Length); if (CloseStreams) { Files[i].Content.Close(); } } if (CloseStreams) { Writer.Close(); } }
public static void Repack(File[] Files, Stream Output, bool CloseStreams = true) { if ((from x in Files where x.FileName == HeaderInfo select x).Count() != 1) { throw new Exception("Header Info Not Found"); } byte[] HInfo = new byte[0x7F8]; File tmp = (from x in Files where x.FileName == HeaderInfo select x).First(); tmp.Content.Read(HInfo, 0, HInfo.Length); tmp.Content.Close(); Files = (from x in Files where x.FileName != HeaderInfo select x).ToArray(); StructWriter Writer = new StructWriter(Output, Encoding: Encoding.ASCII); PACHeader Header = new PACHeader() { Signature = "PAC ", FileCount = (uint)Files.Length, Dummy = HInfo }; Writer.WriteStruct(ref Header); uint TotalHeaderLen = (uint)((Tools.GetStructLength(new File()) * Files.Length) + Tools.GetStructLength(Header)); for (uint i = 0, x = 0; i < Files.Length; i++) { tmp = new File() { FileName = Files[i].FileName, Length = (uint)Files[i].Content.Length, Offset = x + TotalHeaderLen }; Writer.WriteStruct(ref tmp); x += tmp.Length; } for (uint i = 0; i < Files.Length; i++) { Files[i].Content.CopyTo(Writer.BaseStream); if (CloseStreams) { Files[i].Content.Close(); } } Writer.Flush(); if (CloseStreams) { Writer.Close(); } }
public static byte[] BuildStruct <T>(ref T Struct, bool BigEndian = false, Encoding Encoding = null) { MemoryStream Stream = new MemoryStream(); StructWriter Writer = new StructWriter(Stream, BigEndian, Encoding); Writer.WriteStruct(ref Struct); byte[] Result = Stream.ToArray(); Writer.Close(); Stream?.Close(); return(Result); }
private void SaveSettings() { Settings.Signature = Signature; Settings.Version = SettingsVersion; Settings.Blacklist = Settings.Blacklist.Distinct().ToArray(); using (Stream WriterStream = new StreamWriter(SettingsPath).BaseStream) using (StructWriter Writer = new StructWriter(WriterStream)) { Writer.WriteStruct(ref Settings); Writer.Close(); } }
public byte[] Export(StringEntry[] Strings) { ScriptHeader NewHeader = new ScriptHeader(); Tools.CopyStruct(Header, ref NewHeader); MemoryStream UnkData = new MemoryStream(); MemoryStream OffsetData = new MemoryStream(); MemoryStream StringData = new MemoryStream(); MemoryStream Reader = new MemoryStream(Script); Reader.Seek(0x10, SeekOrigin.Begin); Algo.CopyStream(Reader, UnkData, NewHeader.UnkCount * 8); Reader.Close(); StructWriter OffsetWriter = new StructWriter(OffsetData, false, Encoding); StructWriter StringWriter = new StructWriter(StringData, false, Encoding); for (uint i = 0; i < EntryCount; i++) { OffsetWriter.Write((uint)StringWriter.BaseStream.Length); StringWriter.WriteStruct(ref Strings[i]); } OffsetWriter.Seek(0, SeekOrigin.Begin); StringWriter.Seek(0, SeekOrigin.Begin); NewHeader.ScriptLength = (uint)(OffsetWriter.BaseStream.Length + StringWriter.BaseStream.Length + UnkData.Length); NewHeader.OffsetTable = (uint)UnkData.Length; NewHeader.StringTable = (uint)(UnkData.Length + OffsetData.Length); byte[] Output = new byte[0x10 + UnkData.Length + OffsetData.Length + StringData.Length]; Tools.BuildStruct(ref NewHeader, false, Encoding).CopyTo(Output, 0); UnkData.ToArray().CopyTo(Output, 0x10); OffsetData.ToArray().CopyTo(Output, 0x10 + UnkData.Length); StringData.ToArray().CopyTo(Output, 0x10 + UnkData.Length + OffsetData.Length); UnkData.Close(); StringWriter.Close(); OffsetWriter.Close(); return(Compress(Output)); }
internal static void SaveTask() { try { if (File.Exists(TaskPath)) { File.Delete(TaskPath); } if (TaskInfo.LastTaskPos > 0) { using (StructWriter Writer = new StructWriter(TaskPath)) { Writer.WriteStruct(ref TaskInfo); Writer.Close(); } } } catch { } }
private void BuildStringData(string[] Strings, out byte[] StringTable, out int[] Offsets) { Offsets = new int[StrCount]; MemoryStream StrData = new MemoryStream(); StructWriter Writer = new StructWriter(StrData); for (int i = 0; i < StrCount; i++) { Offsets[i] = (int)Writer.BaseStream.Length; StrEntry Entry = new StrEntry(); Entry.Content = Strings[i]; Writer.WriteStruct(ref Entry); } StringTable = StrData.ToArray(); Writer.Close(); StrData?.Close(); }
public byte[] Export(string[] Strings) { MemoryStream Output = new MemoryStream(); Script.Seek(0, SeekOrigin.Begin); CopyStream(Script.BaseStream, Output, OffsetTablePos); MemoryStream StrBuffer = new MemoryStream(); StructWriter StrWorker = new StructWriter(StrBuffer, false, Encoding.Unicode); MemoryStream OffBuffer = new MemoryStream(); StructWriter OffWorker = new StructWriter(OffBuffer, false, Encoding.Unicode); OffWorker.Write((uint)Strings.LongLength); for (uint i = 0; i < Strings.LongLength; i++) { StrEntry Entry = new StrEntry() { Offset = (uint)StrBuffer.Length, Length = (uint)Strings[i].Length * 2 }; OffWorker.WriteStruct(ref Entry); StrWorker.Write(Strings[i], StringStyle.UCString); } OffWorker.Write((uint)StrBuffer.Length); StrBuffer.Position = 0; OffBuffer.Position = 0; CopyStream(OffBuffer, Output, OffBuffer.Length); CopyStream(StrBuffer, Output, StrBuffer.Length); StrWorker.Close(); OffWorker.Close(); return(Output.ToArray()); }
public byte[] Export(string[] Content) { MemoryStream Output = new MemoryStream(); StructWriter Writer = new StructWriter(Output, false, Encoding.UTF8); Writer.WriteStruct(ref Header); WriteNum(Writer, Langs.Length); foreach (string Lang in Langs) { WriteString(Writer, Lang); } WriteNum(Writer, (Content.Length / 2) / Langs.Length); foreach (string str in Content) { WriteString(Writer, str); } byte[] Result = Output.ToArray(); Writer.Close(); return(Result); }
private void BuildOffsetTable(int[] Offsets, out byte[] OffsetData) { MemoryStream OffData = new MemoryStream(); StructWriter Writer = new StructWriter(OffData); //Offset Count int OffsetSize = ForceMaxOffsetLength ? 4 : GetMinIntLen(StrCount); Writer.Write(ConvertSize(OffsetSize)); Writer.Write(CreateOffset(OffsetSize, StrCount)); //Offset's Size OffsetSize = ForceMaxOffsetLength ? 4 : GetMinIntLen(Offsets[StrCount - 1]); Writer.Write(ConvertSize(OffsetSize)); for (int i = 0; i < StrCount; i++) { Writer.Write(CreateOffset(OffsetSize, Offsets[i])); } OffsetData = OffData.ToArray(); Writer.Close(); OffData?.Close(); }
public byte[] Export(string[] Strs) { MemoryStream Out = new MemoryStream(); StructWriter Writer = new StructWriter(Out, false, Encoding.Unicode); Writer.Write((uint)Strs.LongLength / 2); for (uint i = 0; i < Strs.Length; i++) { string Str = Strs[i]; if (EscapeMap[i]) { Str += '\0'; } TMDEntry Entry = new TMDEntry() { String = Str }; Writer.WriteStruct(ref Entry); } Out.Position = 0; byte[] Output = Out.ToArray(); Writer.Close(); return(Output); }
internal static void SetupLauncher(string[] Args) { if (Texts.Count == 0) { Console.WriteLine("Write the Executable Name:"); Exe = Console.ReadLine(); Console.WriteLine("Write delay to find for new text in ms:"); Delay = int.Parse(Console.ReadLine()); Console.WriteLine("You Want Invalidate the game window when translate something? Y/N"); Console.WriteLine("(Can increase the CPU/GPU usage if the window change the text constantly.)"); Invalidate = Console.ReadKey().KeyChar.ToString().ToUpper()[0] == 'Y'; Console.WriteLine(); if (MTL) { Console.WriteLine("Translate From:"); SourceLang = Console.ReadLine(); Console.WriteLine("Translate To:"); TargetLang = Console.ReadLine(); Console.WriteLine("Executable Path:"); } } if (!MTL) { Console.WriteLine("Initializing Executable..."); ProgProc = new Process() { StartInfo = new ProcessStartInfo() { Arguments = ParseArguments(Args), FileName = AppDomain.CurrentDomain.BaseDirectory + Exe, WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory } }; Console.WriteLine("Initializing Hook..."); HookEnabler(); Console.WriteLine("Dumping Text..."); StreamWriter Writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ProgramText.txt", false, Encoding.Unicode); for (int i = 0; i < Texts.Count(); i++) { string line = Tls.Count != 0 && i < Tls.Count ? Tls[i] : Texts[i]; Encode(ref line, true); Writer.WriteLine(line); } Writer.Flush(); Writer.Close(); Console.WriteLine("Dumped, Translate all lines and press a key to save the translation."); Console.WriteLine("If have any string who you don't want translate, set it to :IGNORE:"); Console.ReadKey(); Console.WriteLine("Reading Translation..."); Tls = new List <string>(); TextReader Reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "ProgramText.txt", Encoding.Unicode); int ID = -1; while (Reader.Peek() != -1) { string Line = Reader.ReadLine(); Encode(ref Line, false); if (++ID >= Texts.Count) { continue; } if (string.IsNullOrWhiteSpace(Line) || Line.Trim().ToLower() == ":ignore:") { Texts.RemoveAt(ID--); continue; } Tls.Add(Line); } Reader.Close(); } Console.WriteLine("Generating Configuration..."); Config LauncherData = new Config() { Signature = "TLLD", Executable = Exe, MTL = (byte)(MTL ? 1 : 0), SourceLang = SourceLang, TargetLang = TargetLang, Strings = Texts.ToArray(), TLs = Tls.ToArray(), Delay = Delay, Invalidate = (byte)(Invalidate ? 1 : 0) }; Console.WriteLine("Saving Configuration..."); StructWriter Output = new StructWriter(Setup); Output.WriteStruct(ref LauncherData); Output.Close(); Console.WriteLine("Settings Saved, Press a Key to Exit."); Console.ReadKey(); }
/// <summary> /// Build the String.srl /// </summary> internal static void CompileStrMap() { var DBAr = new List <SRLDatabase2>(); var COri = new List <char>(); var CFak = new List <char>(); var UOri = new List <char>(); var UErr = new List <ushort>(); var ROri = new List <string>(); var RNew = new List <string>(); if (File.Exists(CharMapSrc)) { Log("Compiling Char Reloads..."); using (TextReader Reader = File.OpenText(CharMapSrc)) { while (Reader.Peek() >= 0) { string line = Reader.ReadLine(); if (line.Length == 3 && line[1] == '=') { char cOri = line[0], cFak = line[2]; COri.Add(cOri); CFak.Add(cFak); } if (line.Contains("0x") && line.Contains('=')) { string hex = line.Split('=')[1].Split('x')[1]; ushort Val = ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber); UOri.Add(line[0]); UErr.Add(Val); } } Reader.Close(); } } Log("Generating String Reload Database..."); //Splited String Dump string[] TLMaps = Directory.GetFiles(BaseDir, Path.GetFileName(string.Format(TLMapSrcMsk, "*"))); foreach (string TLMap in TLMaps) { var In = new List <string>(); var Out = new List <string>(); ReadDump(TLMap, ref In, ref Out, IgnoreMask: true); const string NamedLstPrefix = "Strings-"; string DBN = Path.GetFileNameWithoutExtension(TLMap); DBN = DBN.Substring(NamedLstPrefix.Length); DBAr.Add(new SRLDatabase2() { Original = In.ToArray(), Replace = Out.ToArray(), Name = DBN }); Log("{0} Found, Importing, Database ID: {1}...", false, Path.GetFileName(TLMap), DBAr.Count - 1); } if (File.Exists(TLMapSrc)) { var In = new List <string>(); var Out = new List <string>(); ReadDump(TLMapSrc, ref In, ref Out, IgnoreMask: true); DBAr.Add(new SRLDatabase2() { Original = In.ToArray(), Replace = Out.ToArray(), Name = Path.GetFileNameWithoutExtension(TLMapSrc) }); Log("{0} Found, Importing, Database ID: {1}...", false, Path.GetFileName(TLMapSrc), DBAr.Count - 1); } var DBS = DBAr.ToArray(); var Ilegals = CFak.ToArray(); SearchViolations(DBS, Ilegals); if (RemoveIlegals) { RemoveViolations(ref DBS, Ilegals); } Log("{0} Databases Generated.", true, DBAr.Count); if (File.Exists(ReplLst)) { Log("Compiling Replace List..."); ReadDump(ReplLst, ref ROri, ref RNew); } Log("Building String Reloads..."); SRLData3 Data = new SRLData3() { Signature = "SRL3", Version = 1, Databases = DBS, OriLetters = COri.ToArray(), MemoryLetters = Ilegals, UnkChars = UErr.ToArray(), UnkReps = UOri.ToArray(), RepOri = ROri.ToArray(), RepTrg = RNew.ToArray() }; List <IntroContainer> Container = new List <IntroContainer>(); while (File.Exists(string.Format(IntroMsk, Container.Count, "png"))) { string pTexture = string.Format(IntroMsk, Container.Count, "png"); string pSound = string.Format(IntroMsk, Container.Count, "wav"); IntroContainer cContainer = new IntroContainer() { Bitmap = File.ReadAllBytes(pTexture), Wav = new byte[0] }; if (File.Exists(pSound)) { cContainer.Wav = File.ReadAllBytes(pSound); } Container.Add(cContainer); } SRLIntro Intros = new SRLIntro() { Intros = Container.ToArray() }; Log("{0} Intro(s) Found", true, Intros.Intros.Length); if (File.Exists(TLMap)) { File.Delete(TLMap); } using (StructWriter Writer = new StructWriter(TLMap)){ Writer.WriteStruct(ref Data); Writer.WriteStruct(ref Intros); Writer.Close(); } Log("Builded Successfully."); }
internal static void InitializeHook(string[] Args) { Console.WriteLine("Initializing..."); Config Setup = new Config(); if (!System.IO.File.Exists(Common.Setup)) { Console.WriteLine("Config Data Not Found"); return; } using (StructReader Reader = new StructReader(Common.Setup)) { Reader.ReadStruct(ref Setup); Reader.Close(); } MTL = Setup.MTL > 0; From = Setup.SourceLang; To = Setup.TargetLang; Invalidate = Setup.Invalidate > 0; if (Setup.TLs.LongLength != Setup.Strings.LongLength) { throw new Exception("Bad Configuration, The String and Tl Length missmatch."); } for (long i = 0; i < Setup.Strings.LongLength; i++) { Database.Add(Setup.Strings[i], Setup.TLs[i]); } ProgProc = new Process() { StartInfo = new ProcessStartInfo() { Arguments = ParseArguments(Args), FileName = AppDomain.CurrentDomain.BaseDirectory + Setup.Executable, WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory } }; Console.WriteLine("Starting Process..."); ProgProc.Start(); Console.WriteLine("Wainting Main Window Open..."); while (ProgProc.MainWindowHandle == IntPtr.Zero) { System.Threading.Thread.Sleep(100); } Console.WriteLine("Initializing..."); while (!ProgProc.HasExited) { System.Threading.Thread.Sleep(Setup.Delay); var CB = new CallBack(Replace); EnumWindows(CB, 0); IntPtr Handler = GetMenu(ProgProc.MainWindowHandle); ReplaceMenu(Handler); } if (MTL) { Console.WriteLine("Updating Database..."); Setup.Strings = Database.Keys.ToArray(); Setup.TLs = Database.Values.ToArray(); using (StructWriter Writer = new StructWriter(Common.Setup)) { Writer.WriteStruct(ref Setup); Writer.Close(); } } }
public void Encode(Bitmap Texture, Stream Output, bool CloseStreams = true) { StructWriter Writer = new StructWriter(Output, (bool)IsBigEnddian); Writer.Write(0x2065727574786554); if (SteamVersion.Value) { Writer.Write(0x20202020); Writer.Write(0); //0xC, section length Writer.WriteRawType(Const.UINT64, SteamUnk); Writer.Write(0); //0x18, pnglen Writer.WriteRawType(Const.UINT16, (ushort)TexSize.Width); Writer.WriteRawType(Const.UINT16, (ushort)TexSize.Height); } else { Writer.Write(0); //0x8, blocklen Writer.WriteRawType(Const.UINT32, Flags); Writer.Write(0); //0x10, pnglen Writer.WriteRawType(Const.UINT32, (uint)TexSize.Width); Writer.WriteRawType(Const.UINT32, (uint)TexSize.Height); } if (Tiled) { Texture = Retile(Texture); } uint SectionLength; if (SteamVersion.Value) { byte[] Buffer = new byte[Texture.Width * Texture.Height * 4]; fixed(void *pBuff = &Buffer[0]) { int Stride = Texture.Width; uint *pPixel = (uint *)pBuff; unchecked { for (int i = 0; i < Buffer.Length / 4; i++) { int X = i % Stride; int Y = i / Stride; uint ARGB = (uint)Texture.GetPixel(X, Y).ToArgb(); //ARGB => ABGR *pPixel++ = (ARGB & 0xFF00FF00) | ((ARGB & 0x00FF0000) >> 8 * 2) | ((ARGB & 0x000000FF) << 8 * 2); } } } if (Compressed) { Buffer = LZSSCompress(Buffer); } Writer.Write(Buffer); SectionLength = (uint)Writer.BaseStream.Position; uint TexLen = SectionLength - 0x20; Writer.Seek(0xC, 0); Writer.WriteRawType(Const.UINT32, SectionLength); Writer.Seek(0x18, 0); Writer.WriteRawType(Const.UINT32, TexLen); } else { Texture.Save(Writer.BaseStream, ImageFormat.Png); SectionLength = (uint)Writer.BaseStream.Position; uint PngLen = SectionLength - 0x1C; Writer.Seek(0x8, 0); Writer.WriteRawType(Const.UINT32, SectionLength); Writer.Seek(0x10, 0); Writer.WriteRawType(Const.UINT32, PngLen); } Writer.Seek(SectionLength, 0); while (Writer.BaseStream.Position % (SteamVersion.Value ? 8 : 4) != 0) { Writer.Write((byte)0x0); } StructReader Reader = new StructReader(this.Texture, (bool)IsBigEnddian); Reader.Seek(SteamVersion.Value ? 0x0C : 0x08, 0); Reader.Seek(Reader.ReadRawType(Const.UINT32), 0); while (Reader.BaseStream.Position % (SteamVersion.Value ? 8 : 4) != 0) { Reader.ReadByte(); } Reader.BaseStream.CopyTo(Writer.BaseStream); Writer.Flush(); if (CloseStreams) { Writer.Close(); Output?.Close(); } }
public override void FileImport(string path, string outpath = null) { CZ0HeaderInfo cz0HeaderInfo = null; string infopath = path.Replace(".png", ".json"); if (File.Exists(infopath)) { cz0HeaderInfo = JsonConvert.DeserializeObject <CZ0HeaderInfo>(File.ReadAllText(infopath)); } CZOutputInfo czOutput = new CZOutputInfo(); Bitmap Picture = new Bitmap(new MemoryStream(File.ReadAllBytes(path))); StructWriter Writer = new StructWriter(File.Open(path + ".cz0", FileMode.Create)); CZ0Header header; if (cz0HeaderInfo == null) { header.Signature = "CZ0"; header.HeaderLength = 0x40; header.Width = (ushort)Picture.Width; header.Heigth = (ushort)Picture.Height; header.Colorbits = 32; } else { header = cz0HeaderInfo.cz0Header; } Writer.WriteStruct(ref header); Writer.Seek(header.HeaderLength, SeekOrigin.Begin); if (header.Colorbits == 32) { System.Diagnostics.Debug.WriteLine(32); for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { Writer.Write(Picture.GetPixel(x, y).R); Writer.Write(Picture.GetPixel(x, y).G); Writer.Write(Picture.GetPixel(x, y).B); Writer.Write(Picture.GetPixel(x, y).A); } } } else if (header.Colorbits == 24) { System.Diagnostics.Debug.WriteLine(24); for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { Writer.Write(Picture.GetPixel(x, y).R); Writer.Write(Picture.GetPixel(x, y).G); Writer.Write(Picture.GetPixel(x, y).B); } } } else if (header.Colorbits == 8) { System.Diagnostics.Debug.WriteLine(8); List <Pixel32_BGRA> list = new List <Pixel32_BGRA>(); for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { var color = Picture.GetPixel(x, y); var color2 = new Pixel32_BGRA(); color2.R = color.R; color2.G = color.G; color2.B = color.B; color2.A = color.A; if (!list.Contains(color2)) { list.Add(color2); } } } //颜色大于256 if (list.Count > 256) { Console.WriteLine("Over 256 Color!!"); //调用pngquant png 8bit缩减 if (File.Exists("pngquant.exe") || File.Exists("pngquant")) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var psi = new ProcessStartInfo("pngquant.exe") { RedirectStandardOutput = true }; psi.Arguments = " 256 " + path + " --ext tmp "; var proc = Process.Start(psi); if (proc == null) { Console.WriteLine("Can not exec."); } else { using (var sr = proc.StandardOutput) { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } if (!proc.HasExited) { proc.Kill(); } } } string pathtmp = path.Replace(".png", "tmp"); Picture = new Bitmap(new MemoryStream(File.ReadAllBytes(pathtmp))); File.Delete(pathtmp); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { //TODO } else { //TODO } list.Clear(); for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { var color = Picture.GetPixel(x, y); var color2 = new Pixel32_BGRA(); color2.R = color.R; color2.G = color.G; color2.B = color.B; color2.A = color.A; if (!list.Contains(color2)) { list.Add(color2); } } } } else { throw new Exception("Need pngquant !! Download From https://pngquant.org/"); } } var pixel = new Pixel32_BGRA(); for (int i = 0; i < list.Count; i++) { pixel = list[i]; Writer.WriteStruct(ref pixel); } for (int i = 0; i < 256 - list.Count; i++) { var pixelempty = new Pixel32_BGRA() { R = 0, G = 0, B = 0, A = 255 }; Writer.WriteStruct(ref pixelempty); } for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { var color = Picture.GetPixel(x, y); var color2 = new Pixel32_BGRA(); color2.R = color.R; color2.G = color.G; color2.B = color.B; color2.A = color.A; uint index = (uint)list.IndexOf(color2); Writer.Write((byte)index); } } } Writer.Close(); }
//作者:Wetor //时间:2019.1.18 public void PngToCZ1(string outfile) { Bitmap Picture = new Bitmap(File.Open(outfile, FileMode.Open)); StructWriter Writer = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create)); CZ1Header header; header.Signature = "CZ1"; header.HeaderLength = 0x10; header.Width = (ushort)Picture.Width; header.Heigth = (ushort)Picture.Height; header.Colorbits = 8; Writer.WriteStruct(ref header); Writer.Seek(header.HeaderLength, SeekOrigin.Begin); Pixel32_BGRA Pixel = new Pixel32_BGRA(); Pixel.R = 255; Pixel.G = 255; Pixel.B = 255; for (int k = 0; k < 256; k++) { Pixel.A = (byte)k; Writer.WriteStruct(ref Pixel); } byte[] bytes = new byte[Picture.Height * Picture.Width]; int i = 0; for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { bytes[i] = Picture.GetPixel(x, y).A; i++; } } int file_num = bytes.Length / 130554 + 1; //System.Diagnostics.Debug.WriteLine("{0} {1} {2}", file_num, listBytes.Count, 130554); List <int[]> out_list = new List <int[]>(); Writer.Write(file_num); for (int k = 0; k < file_num; k++) { List <int> listBytes = new List <int>(); StringBuilder decompressed = new StringBuilder(); byte[] tmp_bytes = new byte[130554]; if (k == file_num - 1) { Array.Copy(bytes, k * 130554, tmp_bytes, 0, bytes.Length - k * 130554); } else { Array.Copy(bytes, k * 130554, tmp_bytes, 0, 130554); } foreach (char kk in tmp_bytes) { decompressed.Append(kk); } listBytes = LzwUtil.Compress(decompressed.ToString()); out_list.Add(listBytes.ToArray()); Writer.Write(listBytes.Count); //string tmp_str; System.Diagnostics.Debug.WriteLine("{0}", k); /*if (k== file_num - 1) * { * tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, listBytes.Count - 130554 / 2 * k)); * tmp_list.Insert(0, 0); * tmp_str = Decompress(tmp_list); * } * else * { * tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, 130554 / 2)); * tmp_list.Insert(0, 0); * tmp_str = Decompress(tmp_list); * }*/ if (k == file_num - 1) { Writer.Write(bytes.Length - k * 130554); } else { Writer.Write(130554); } //Writer.Write(0xFFFD); } /*List<byte> output = new List<byte>(); * string str = Decompress(listBytes); * foreach (var c in str) * { * output.Add((byte)c); * } * // Writer.Write(output.ToArray()); * System.Diagnostics.Debug.WriteLine(output.Count);*/ for (int k = 0; k < out_list.Count; k++) { for (int kk = 0; kk < out_list[k].Length; kk++) { Writer.Write((UInt16)out_list[k][kk]); } } Writer.Close(); }
//作者:Wetor //时间:2019.1.18 public void PngToCZ1(string outfile) { CZOutputInfo czOutput = new CZOutputInfo(); string name = outfile.Replace(".png", "").Replace(".Png", "").Replace(".PNG", "") + ".json"; if (File.Exists(name)) { czOutput = JsonConvert.DeserializeObject <CZOutputInfo>(File.ReadAllText(name)); } Bitmap Picture = new Bitmap(File.Open(outfile, FileMode.Open)); StructWriter Writer = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create)); CZ1Header header; header.Signature = "CZ1"; header.HeaderLength = 0x10; header.Width = (ushort)Picture.Width; header.Heigth = (ushort)Picture.Height; header.Colorbits = 4; Writer.WriteStruct(ref header); Writer.Seek(header.HeaderLength, SeekOrigin.Begin); Pixel32_BGRA Pixel = new Pixel32_BGRA(); Pixel.R = 255; Pixel.G = 255; Pixel.B = 255; //FF FF FF 00 //FF FF FF 18 //FF FF FF 26 //FF FF FF 35 //FF FF FF 45 //FF FF FF 55 //FF FF FF 65 //FF FF FF 75 //FF FF FF 85 //FF FF FF 96 //FF FF FF A7 //FF FF FF B8 //FF FF FF C9 //FF FF FF DB //FF FF FF EC //FF FF FF FF Pixel.A = (byte)0x00; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x18; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x26; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x35; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x45; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x55; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x65; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x75; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x85; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x96; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xA7; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xB8; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xC9; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xDB; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xEC; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xFF; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Queue <byte> queue = new Queue <byte>(); int i = 0; for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { byte tmp = color256to16(Picture.GetPixel(x, y).A); //bytes11[i] = (byte)list.IndexOf(tmp); queue.Enqueue((byte)list.IndexOf(tmp)); i++; } } byte[] bytes2 = new byte[Picture.Height * Picture.Width]; for (int j = 0; j < bytes2.Length / 2; j++) { var low4bit = queue.Dequeue(); var high4bit = queue.Dequeue(); var b = (uint)(high4bit << 4) + (uint)low4bit; bytes2[j] = (byte)b; } var ie = bytes2.ToList().GetEnumerator(); List <List <int> > out_list = LzwUtil.Compress(ie, 0xFEFD); foreach (var item in out_list) { Console.WriteLine("add compressed count :" + item.Count); Console.WriteLine(item.Count); Console.WriteLine(LzwUtil.Decompress(item).Length); } //for (int k = 0; k < out_list.Count; k++) //{ // Writer.Write(out_list[k].Count); // Writer.Write(LzwUtil.Decompress(out_list[k]).Length); // List<byte> bytes = new List<byte>(); // foreach (var item in out_list[k]) // { // UInt16 bb = (UInt16)item; // bytes.Add(BitConverter.GetBytes(bb)[0]); // bytes.Add(BitConverter.GetBytes(bb)[1]); // } // //string tmp_str; // System.Diagnostics.Debug.WriteLine("{0}", k); //} int file_num = out_list.Count; Writer.Write(out_list.Count); for (int k = 0; k < out_list.Count; k++) { //if (k >= out_list.Count - 1) //{ // Writer.Write((UInt32)0); // Writer.Write((UInt32)0); //} //else //{ Writer.Write(out_list[k].Count); Writer.Write(LzwUtil.Decompress(out_list[k]).Length); //List<byte> bytes = new List<byte>(); //foreach (var item in out_list[k]) //{ // UInt16 bb = (UInt16)item; // bytes.Add(BitConverter.GetBytes(bb)[0]); // bytes.Add(BitConverter.GetBytes(bb)[1]); //} //string tmp_str; System.Diagnostics.Debug.WriteLine("{0}", k); //} } //Writer.Write(czOutput.filecount); //for (int k = 0; k < czOutput.filecount; k++) //{ // if (k >= out_list.Count - 1) // { // Writer.Write((UInt32)0); // Writer.Write((UInt32)0); // } // else // { // Writer.Write(out_list[k].Count); // Writer.Write(LzwUtil.Decompress(out_list[k]).Length); // //List<byte> bytes = new List<byte>(); // //foreach (var item in out_list[k]) // //{ // // UInt16 bb = (UInt16)item; // // bytes.Add(BitConverter.GetBytes(bb)[0]); // // bytes.Add(BitConverter.GetBytes(bb)[1]); // //} // //string tmp_str; // System.Diagnostics.Debug.WriteLine("{0}", k); // } //} uint totalsize_new = 0x10 + 4 * 16 + 4 + (uint)file_num * 4 * 2; uint totalsize_org = 0x10 + 4 * 16 + 4 + czOutput.filecount * 4 * 2; for (int k = 0; k < out_list.Count; k++) { for (int kk = 0; kk < out_list[k].Count; kk++) { Writer.Write((UInt16)out_list[k][kk]); totalsize_new += 2; } } totalsize_org += czOutput.TotalCompressedSize * 2; int diff = (int)(totalsize_org - totalsize_new); if (diff > 0) { diff = diff / 2; for (uint j = 0; j < diff; j++) { Writer.Write((UInt16)0); } } else { Console.WriteLine("超长"); } //int p = 0; //foreach (var blockinfo in czOutput.blockinfo) //{ // if (out_list.Count - 1 >= p) // { // for (int kk = 0; kk < out_list[p].Count; kk++) // { // Writer.Write((UInt16)out_list[p][kk]); // } // if (out_list[p].Count == blockinfo.CompressedSize) // { // Console.WriteLine("符合"); // } // else // { // Int64 compressedcount = (Int64)out_list[p].Count - (Int64)blockinfo.CompressedSize; // compressedcount = Math.Abs(compressedcount); // Console.WriteLine("不符合补0-" + compressedcount); // for (Int64 pp = 0; pp < compressedcount; pp++) // { // Writer.Write((UInt16)0); // } // } // } // else // { // Console.WriteLine("不符合补0-" + blockinfo.CompressedSize); // //整块补0 // for (Int64 pp = 0; pp < blockinfo.CompressedSize; pp++) // { // Writer.Write((UInt16)0); // } // } // p++; //} Writer.Close(); }
public static void Save(Stream Output, Entry[] Content, bool BigEndian, bool SteamVer, bool CloseStreams = true) { uint HeaderSize = (SteamVer ? ExNameSize : NameSize) + 4; StructWriter Writer = new StructWriter(Output, BigEndian); object Section = SteamVer ? (object)new SteamSection() : new Section(); ((dynamic)Section).Name = SteamVer ? "Filename " : "Filename"; ((dynamic)Section).Event = new FieldInvoke(SectionEvent); long BasePos = (uint)(Content.LongLength * 4); List <byte> OffsetBuffer = new List <byte>((int)BasePos); List <byte> NameBuffer = new List <byte>(); foreach (Entry Entry in Content) { OffsetBuffer.Add((uint)(NameBuffer.Count + BasePos), BigEndian); NameBuffer.AddRange(Encoding.UTF8.GetBytes(Entry.Filename + "\x0")); } ((dynamic)Section).Data = OffsetBuffer.Concat(NameBuffer).ToArray(); Writer.WriteStruct(SteamVer ? typeof(SteamSection) : typeof(Section), ref Section); while (Writer.BaseStream.Position % (SteamVer ? 8 : 4) != 0) { Writer.Write((byte)0x00); } long PackStart = Writer.BaseStream.Position; ((dynamic)Section).Name = SteamVer ? "Pack " : "Pack "; List <byte> OffsetTable = new List <byte>(); OffsetTable.Add(Content.Length, BigEndian); BasePos = PackStart + HeaderSize + 4 + (Content.Length * 8); while (BasePos % (SteamVer ? 8 : 0x100) != 0) { BasePos++; } foreach (Entry Entry in Content) { OffsetTable.Add((uint)BasePos, BigEndian); OffsetTable.Add((uint)Entry.Content.Length, BigEndian); BasePos += Entry.Content.Length; while (!SteamVer && BasePos % 0x100 != 0) { BasePos++; } } ((dynamic)Section).Data = OffsetTable.ToArray(); Writer.WriteStruct(SteamVer ? typeof(SteamSection) : typeof(Section), ref Section); while (Writer.BaseStream.Position % (SteamVer ? 8 : 0x100) != 0) { Writer.Write((byte)0); } Writer.Flush(); foreach (Entry Entry in Content) { Entry.Content.Position = 0; Entry.Content.CopyTo(Output); while (!SteamVer && Output.Position % 0x100 != 0) { Output.WriteByte(0); } } while (SteamVer && Output.Position % 0x10 != 0) { Output.WriteByte(0); } Writer.Close(); Output?.Close(); }
public static void Pack(File[] Files, Stream Output) { Header = new FPacHeader() { Signature = 0x43415046, Dummy1 = 1, Dummy2 = 0, FilesCount = (uint)Files.Length }; uint Tmp = 0; foreach (File File in Files) { if (Encoding.UTF8.GetByteCount(File.FileName) > Tmp) { Tmp = (uint)Encoding.UTF8.GetByteCount(File.FileName); } } while (Tmp % 4 != 0) { Tmp++; } int EntryLen = (int)Tools.GetStructLength(new Entry(), (int)Tmp); while (EntryLen % 16 != 0) { EntryLen++; } uint DataLength = ((uint)EntryLen * Header.FilesCount) + (uint)Tools.GetStructLength(Header); Header.BaseOffset = DataLength; foreach (File File in Files) { DataLength += (uint)File.Data.Length; } Header.PackgetSize = DataLength; Header.FilesNameLen = Tmp; StructWriter Writer = new StructWriter(Output, Encoding.UTF8); dynamic obj = Header; Writer.WriteStruct(ref obj); DataLength = Header.BaseOffset; Tmp = 0; foreach (File File in Files) { dynamic Entry = new Entry() { FileName = File.FileName, Offset = DataLength - Header.BaseOffset, Dummy = Dummy, FileIndex = Tmp++, FixPadding = Padding, Length = (uint)File.Data.Length }; DataLength += (uint)File.Data.Length; Writer.WriteStruct(ref Entry); } DataLength = Header.BaseOffset; foreach (File File in Files) { int Readed = 0; byte[] Buffer = new byte[1024 * 1024]; do { Readed = File.Data.Read(Buffer, 0, Buffer.Length); Writer.Write(Buffer, 0, Readed); } while (Readed > 0); File.Data.Close(); } Writer.Close(); Output?.Close(); }
public byte[] Export(string Content) { dynamic NewVLC = new VLContent() { Dummy = this.Content.Dummy, Content = Content }; if (NoUnk14 && (Encrypt || Compress)) { //The English Script of koichoco have a part of the header cutted off, so... //if you ignore this he corrupt when you encrypt or compress the script throw new Exception("This Script is Corrupted, You can't Export it with Compression or Encryption."); } SL2Header Header = new SL2Header(); Tools.CopyStruct(this.Header, ref Header); Header.IsCompressed = Header.IsEncrypted = false; Header.DecLen = 0; byte[] Output = Tools.BuildStruct(ref NewVLC, true, Encoding); if (Compress) { Output = CompressData(Output); Header.IsCompressed = true; Header.DecLen = (uint)Encoding.GetByteCount(Content) + 0x18;//??? } if (Encrypt) { Output = EncryptData(Output); Header.IsEncrypted = true; } Header.Length = (uint)(Output.LongLength + (NoUnk14 ? 0x4C : 0x50)); if (UpdateModifyDate) { Header.Day = (byte)DateTime.Now.Day; Header.Month = (byte)DateTime.Now.Month; Header.Year = (ushort)DateTime.Now.Year; } MemoryStream Data = new MemoryStream(); StructWriter Result = new StructWriter(Data, true, Encoding); Result.WriteStruct(ref Header); if (NoUnk14) { Result.Seek(-4, SeekOrigin.Current); } Result.Write(Output, 0, Output.Length); Output = Data.ToArray(); Result.Close(); Data.Close(); return(Output); }
//作者:Wetor //时间:2019.1.18 public void PngToCZ1(string outfile) { Bitmap Picture = new Bitmap(File.Open(outfile, FileMode.Open)); StructWriter Writer = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create)); CZ1Header header; header.Signature = "CZ1"; header.HeaderLength = 0x10; header.Width = (ushort)Picture.Width; header.Heigth = (ushort)Picture.Height; header.Colorbits = 4; Writer.WriteStruct(ref header); Writer.Seek(header.HeaderLength, SeekOrigin.Begin); Pixel32 Pixel = new Pixel32(); Pixel.R = 255; Pixel.G = 255; Pixel.B = 255; //FF FF FF 00 //FF FF FF 18 //FF FF FF 26 //FF FF FF 35 //FF FF FF 45 //FF FF FF 55 //FF FF FF 65 //FF FF FF 75 //FF FF FF 85 //FF FF FF 96 //FF FF FF A7 //FF FF FF B8 //FF FF FF C9 //FF FF FF DB //FF FF FF EC //FF FF FF FF Pixel.A = (byte)0x00; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x18; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x26; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x35; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x45; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x55; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x65; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x75; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x85; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0x96; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xA7; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xB8; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xC9; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xDB; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xEC; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Pixel.A = (byte)0xFF; list.Add(Pixel.A); Writer.WriteStruct(ref Pixel); Queue <byte> queue = new Queue <byte>(); int i = 0; for (int y = 0; y < Picture.Height; y++) { for (int x = 0; x < Picture.Width; x++) { byte tmp = color256to16(Picture.GetPixel(x, y).A); //bytes11[i] = (byte)list.IndexOf(tmp); queue.Enqueue((byte)list.IndexOf(tmp)); i++; } } byte[] bytes2 = new byte[Picture.Height * Picture.Width / 2]; for (int j = 0; j < bytes2.Length; j++) { var low4bit = queue.Dequeue(); var high4bit = queue.Dequeue(); var b = (uint)(high4bit << 4) + (uint)low4bit; bytes2[j] = (byte)b; } //foreach (var b in bytes2) //{ // int low4bit = b & 0x0F; // int high4bit = (b & 0xF0) >> 4; // queue.Enqueue(low4bit); // queue.Enqueue(high4bit); //} int file_num = bytes2.Length / 130554 + 1; //System.Diagnostics.Debug.WriteLine("{0} {1} {2}", file_num, listBytes.Count, 130554); List <int[]> out_list = new List <int[]>(); Writer.Write(file_num); for (int k = 0; k < file_num; k++) { List <int> listBytes = new List <int>(); StringBuilder decompressed = new StringBuilder(); byte[] tmp_bytes = new byte[130554]; if (k == file_num - 1) { Array.Copy(bytes2, k * 130554, tmp_bytes, 0, bytes2.Length - k * 130554); } else { Array.Copy(bytes2, k * 130554, tmp_bytes, 0, 130554); } foreach (char kk in tmp_bytes) { decompressed.Append(kk); } listBytes = LzwUtil.Compress(decompressed.ToString()); out_list.Add(listBytes.ToArray()); Writer.Write(listBytes.Count); //string tmp_str; System.Diagnostics.Debug.WriteLine("{0}", k); /*if (k== file_num - 1) * { * tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, listBytes.Count - 130554 / 2 * k)); * tmp_list.Insert(0, 0); * tmp_str = Decompress(tmp_list); * } * else * { * tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, 130554 / 2)); * tmp_list.Insert(0, 0); * tmp_str = Decompress(tmp_list); * }*/ if (k == file_num - 1) { Writer.Write(bytes2.Length - k * 130554); } else { Writer.Write(130554); } //Writer.Write(0xFFFD); } /*List<byte> output = new List<byte>(); * string str = Decompress(listBytes); * foreach (var c in str) * { * output.Add((byte)c); * } * // Writer.Write(output.ToArray()); * System.Diagnostics.Debug.WriteLine(output.Count);*/ for (int k = 0; k < out_list.Count; k++) { for (int kk = 0; kk < out_list[k].Length; kk++) { Writer.Write((UInt16)out_list[k][kk]); } } Writer.Close(); }