public static void AnalyzeXML(IToolInstance tool, string version, SwfFile swf) { var doc = new XDocument(); doc.Add(new XElement("XML")); foreach (var tag in swf.Tags) { // DefineBinaryData if (tag.Type != 87) { continue; } // <?xml if (tag.Content[6] != 0x3c || tag.Content[7] != 0x3f || tag.Content[8] != 0x78 || tag.Content[9] != 0x6d || tag.Content[10] != 0x6c) { continue; } string xmlString = Encoding.UTF8.GetString(tag.Content, 6, tag.Content.Length - 6); try { XDocument xml = XDocument.Parse(xmlString); doc.Root.Add(xml.Root.Elements()); } catch { } } doc.Root.Add(new XAttribute("version", version)); tool.SaveXmlData(new XmlData(doc)); }
public void CompressLzmaTest() { var source = new byte[] { 0x46, 0x57, 0x53, 0x14, 0x5A, 0xB3, 0xB2, 0x00, 0x78, 0x00, 0x07, 0xD0, 0x00, 0x00, 0x18, 0x38, 0x00, 0x00, 0x3C, 0x01, 0x00, 0x44, 0x11, 0x19, 0x00, 0x00, 0x00, 0x7F, 0x13, 0x80, 0x02, 0x00 }; var target = new MemoryStream(); SwfFile.Compress(new MemoryStream(source), target, SwfFormat.ZWS); var res = target.ToArray(); Assert.AreEqual(0x4C, res[8]); Assert.AreEqual(0x5A, res[9]); Assert.AreEqual(0x49, res[10]); Assert.AreEqual(0x50, res[11]); Assert.AreEqual(0x5D, res[12]); Assert.AreEqual(0x00, res[13]); Assert.AreEqual(0x00, res[14]); Assert.AreEqual(0x20, res[15]); Assert.AreEqual(0x00, res[16]); Assert.AreEqual(0x00, res[17]); }
/// <summary> /// Converts to XML. /// </summary> /// <param name="file">The file.</param> /// <returns>Convert SwfFile to xml.</returns> public XDocument ConvertToXml(SwfFile file) { _formatterFactory = new TagFormatterFactory(file.FileInfo.Version); var doc = new XDocument(GetRoot(file)); doc.Declaration = new XDeclaration("1.0", "utf-8", "yes"); return doc; }
public void ReadWriteTest() { var file = new SwfFile { FileInfo = { Format = SwfFormat.FWS, Version = 8 }, Header = { FrameSize = new SwfRect(0, 0, 100, 100), FrameRate = 20.0, FrameCount = 1 }, Tags = { new FileAttributesTag { UseNetwork = true }, new SetBackgroundColorTag { Color = new SwfRGB(10, 224, 224) }, new PlaceObject3Tag { BackgroundColor = new SwfRGBA(255, 255, 255, 1), HasVisible = true }, new ShowFrameTag(), new EndTag() } }; var mem = new MemoryStream(); file.WriteTo(mem); mem.Seek(0, SeekOrigin.Begin); var other = SwfFile.ReadFrom(mem); Assert.AreEqual(file.Tags.Count, other.Tags.Count); }
public void ReadMatrixTest() { var file = new SwfFile(); file.FileInfo.Version = 10; var tags = GetTagBinariesFromSwfResource("Matrix-compiled.swf") .Where(item => item.Type == SwfTagType.PlaceObject2); var tagData = tags.First(); var ser = new SwfTagDeserializer(file); var tag = ser.ReadTag <PlaceObject2Tag>(tagData); Assert.AreEqual(20.5, tag.Matrix.ScaleX); Assert.AreEqual(17.25, tag.Matrix.ScaleY); tagData = tags.Skip(1).First(); tag = ser.ReadTag <PlaceObject2Tag>(tagData); Assert.AreEqual(0.5, tag.Matrix.ScaleX); Assert.AreEqual(1.25, tag.Matrix.ScaleY); tagData = tags.Skip(2).First(); tag = ser.ReadTag <PlaceObject2Tag>(tagData); Assert.AreEqual(0.5, tag.Matrix.ScaleX); Assert.AreEqual(-1.25, tag.Matrix.ScaleY); }
public void DefineBitsJPEG2Test() { var file = new SwfFile(); file.FileInfo.Version = 10; var tag = new DefineBitsJPEG2Tag(); tag.CharacterID = 1; tag.ImageData = GetEmbeddedResourceData("DefineBitsJPEG2.jpg"); var visitor = new SwfTagSerializer(file); var res = visitor.GetTagData(tag); var mem = new MemoryStream(); var writer = new SwfStreamWriter(mem); writer.WriteTagData(res); var etalon = GetTagFullBinariesFromSwfResource("DefineBitsJPEG2.swf") .FirstOrDefault(item => item.Type == SwfTagType.DefineBitsJPEG2); if (etalon.Binary == null) { throw new InvalidOperationException("Couldn't find etalon tag"); } AssertExt.AreEqual(etalon.Binary, mem.ToArray(), "Checking DefineBitsJPEG2"); }
protected void Bin2BinTest <T>(string resourceName, Action <SwfFile> appendTagsCallback) where T : SwfTagBase, new() { var stream = GetType().Assembly.GetManifestResourceStream(resourceName); var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); var file = new SwfFile { FileInfo = { Version = 10 } }; appendTagsCallback(file); var tag = DeserializeTag <T>(file, resourceName); var newBytes = SerializeTag(file, tag); int min = bytes.Length < newBytes.Length ? bytes.Length : newBytes.Length; for (var i = 0; i < min; i++) { Assert.AreEqual(bytes[i], newBytes[i], string.Format("Checking resource {0}, index: {1}, original: {2}, actual: {3}", resourceName, i, bytes[i], newBytes[0])); } Assert.AreEqual(bytes.Length, newBytes.Length, "Checking length"); }
protected T ReadTag <T>(string resourceName, string tagHash) where T : SwfTagBase { using (var stream = OpenEmbeddedResource(resourceName)) { var file = new SwfFile(); var reader = new SwfStreamReader(stream); file.FileInfo = reader.ReadSwfFileInfo(); reader = GetSwfStreamReader(file.FileInfo, stream); file.Header = reader.ReadSwfHeader(); while (!reader.IsEOF) { var tagData = reader.ReadTagData(); var hash = GetTagHash(tagData); if (tagHash == hash) { var tagReader = new SwfTagDeserializer(file); //using (var dump = File.Open(@"D:\temp\1.bin", FileMode.Create)) { // dump.Write(tagData.Data, 0, tagData.Data.Length); // dump.Flush(); //} return(tagReader.ReadTag <T>(tagData)); } } } return(null); }
protected IEnumerable <TagBinaryInfo> GetTagFullBinariesFromSwfResource(string resourceName) { var file = new SwfFile(); var stream = OpenEmbeddedResource(resourceName); SwfStreamReader reader = new SwfStreamReader(stream); file.FileInfo = reader.ReadSwfFileInfo(); stream = DecompressIfNeeded(file.FileInfo, stream); stream.Seek(8, SeekOrigin.Begin); reader = new SwfStreamReader(stream); file.Header = reader.ReadSwfHeader(); while (stream.Position < stream.Length) { var position = stream.Position; ushort typeAndSize = reader.ReadUInt16(); SwfTagType type = (SwfTagType)(typeAndSize >> 6); int shortSize = typeAndSize & 0x3f; int size = shortSize < 0x3f ? shortSize : reader.ReadInt32(); var length = stream.Position - position + size; stream.Seek(position, SeekOrigin.Begin); yield return(new TagBinaryInfo { Type = type, Binary = reader.ReadBytes((int)length) }); } }
protected IEnumerable<TagBinaryInfo> GetTagFullBinariesFromSwfResource(string resourceName) { var file = new SwfFile(); var stream = OpenEmbeddedResource(resourceName); SwfStreamReader reader = new SwfStreamReader(stream); file.FileInfo = reader.ReadSwfFileInfo(); stream = DecompressIfNeeded(file.FileInfo, stream); stream.Seek(8, SeekOrigin.Begin); reader = new SwfStreamReader(stream); file.Header = reader.ReadSwfHeader(); while (stream.Position < stream.Length) { var position = stream.Position; ushort typeAndSize = reader.ReadUInt16(); SwfTagType type = (SwfTagType)(typeAndSize >> 6); int shortSize = typeAndSize & 0x3f; int size = shortSize < 0x3f ? shortSize : reader.ReadInt32(); var length = stream.Position - position + size; stream.Seek(position, SeekOrigin.Begin); yield return new TagBinaryInfo { Type = type, Binary = reader.ReadBytes((int)length) }; } }
public SwfFile ReadFromXml(XDocument doc) { var root = doc.Root; var file = new SwfFile(); SwfFileInfo fileInfo; if (root == null || root.Name.LocalName != "swf") { throw new FormatException("Expected swf as root"); } fileInfo.Version = root.RequiredByteAttribute("version"); fileInfo.Format = root.RequiredBoolAttribute("compressed") ? "CWS" : "FWS"; fileInfo.FileLength = 0; file.FileInfo = fileInfo; var hdr = root.RequiredElement("Header"); file.Header.FrameRate = hdr.RequiredDoubleAttribute("framerate"); file.Header.FrameCount = hdr.RequiredUShortAttribute("frames"); file.Header.FrameSize = XRect.FromXml(hdr.RequiredElement("size").Element("Rectangle")); var formatterFactory = new TagFormatterFactory(fileInfo.Version); var tags = hdr.RequiredElement("tags"); foreach (var xTag in tags.Elements()) { var tag = SwfTagNameMapping.CreateTagByXmlName(xTag.Name.LocalName); var formatter = formatterFactory.GetFormatter(tag); tag = formatter.ParseTo(xTag, tag); file.Tags.Add(tag); } return file; }
/// <summary> /// Converts to XML. /// </summary> /// <param name="file">The file.</param> /// <returns>Convert SwfFile to xml.</returns> public XDocument ConvertToXml(SwfFile file) { _formatterFactory = new TagFormatterFactory(file.FileInfo.Version); var doc = new XDocument(GetRoot(file)); doc.Declaration = new XDeclaration("1.0", "utf-8", "yes"); return(doc); }
private static bool IsCompressed(SwfFile file) { if (file.FileInfo.Format == SwfFormat.Unknown) { throw new InvalidOperationException(); } return(file.FileInfo.Format != SwfFormat.FWS); }
private XElement GetRoot(SwfFile file) { return(new XElement("swf", new XAttribute("version", file.FileInfo.Version), new XAttribute("format", file.FileInfo.Format.ToString()), GetSwfHeaderXml(file) )); }
protected T DeserializeTag <T>(SwfFile file, string resourceName) where T : SwfTagBase, new() { using (var stream = GetType().Assembly.GetManifestResourceStream(resourceName)) { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); var tag = DeserializeTag <T>(file, bytes); return(tag); } }
protected string GetTagHash(SwfTagBase tag) { var file = new SwfFile(); file.FileInfo.Version = 10; var ser = new SwfTagSerializer(file); var tagData = ser.GetTagData(tag); return(GetTagHash(tagData)); }
private XElement GetSwfHeaderXml(SwfFile file) { var header = file.Header; return(new XElement("Header", new XAttribute("framerate", header.FrameRate), new XAttribute("frames", header.FrameCount), new XElement("size", XRect.ToXml(header.FrameSize)), GetTagsXml(file.Tags))); }
private static bool IsCompressed(SwfFile file) { switch (file.FileInfo.Format) { case "CWS": return true; case "FWS": return false; default: throw new InvalidOperationException(); } }
protected T DeserializeTag <T>(SwfFile file, byte[] bytes) where T : SwfTagBase, new() { var tag = new T(); var tagData = new SwfTagData { Type = tag.TagType, Data = bytes }; //TODO: There is no need in second instance of tag return((T) new SwfTagDeserializer(file).ReadTag(tagData)); }
public void SwfToXmlTest() { var source = GetType().Assembly.GetManifestResourceStream("SwfLib.SwfMill.Tests.FlashTest.swf"); var file = SwfFile.ReadFrom(source); var doc = new SwfMillFacade().ConvertToXml(file); doc.Declaration = new XDeclaration("1", "utf-8", "yes"); var res = doc.ToString(); using (var writer = new StreamWriter(@"d:\Sergey\test.xml", false, Encoding.UTF8)) { doc.Save(writer); } }
public Stream Cycle(string path) { var facade = new SwfMillFacade(); using (var stream = File.Open(path, FileMode.Open)) { var xml = facade.ConvertToXml(SwfFile.ReadFrom(stream)); var newFile = facade.ReadFromXml(xml); var mem = new MemoryStream(); newFile.WriteTo(mem); mem.Seek(0, SeekOrigin.Begin); return(mem); } }
protected IEnumerable <SwfTagData> IterateTags(Stream stream) { var file = new SwfFile(); var reader = new SwfStreamReader(stream); file.FileInfo = reader.ReadSwfFileInfo(); reader = GetSwfStreamReader(file.FileInfo, stream); file.Header = reader.ReadSwfHeader(); while (!reader.IsEOF) { var tagData = reader.ReadTagData(); yield return(tagData); } }
public void ReadWriteTest() { var file = new SwfFile { FileInfo = { Format = "FWS", Version = 8 }, Header = { FrameSize = new SwfRect(0, 0, 100, 100), FrameRate = 20.0, FrameCount = 1 }, Tags = { new FileAttributesTag { UseNetwork = true }, new SetBackgroundColorTag { Color = new SwfRGB(10, 224, 224) }, new ShowFrameTag(), new EndTag() } }; var mem = new MemoryStream(); file.WriteTo(mem); mem.Seek(0, SeekOrigin.Begin); var other = SwfFile.ReadFrom(mem); Assert.AreEqual(file.Tags.Count, other.Tags.Count); }
public SwfFile ReadFromXml(XDocument doc) { var root = doc.Root; var file = new SwfFile(); SwfFileInfo fileInfo; if (root == null || root.Name.LocalName != "swf") { throw new FormatException("Expected swf as root"); } fileInfo.Version = root.RequiredByteAttribute("version"); if (!Enum.TryParse(root.RequiredAttribute("format"), false, out fileInfo.Format)) { throw new FormatException("Unable to parse file format"); } if (fileInfo.Format == SwfFormat.Unknown) { throw new FormatException("Unsupported file format"); } fileInfo.FileLength = 0; file.FileInfo = fileInfo; var hdr = root.RequiredElement("Header"); file.Header.FrameRate = hdr.RequiredDoubleAttribute("framerate"); file.Header.FrameCount = hdr.RequiredUShortAttribute("frames"); file.Header.FrameSize = XRect.FromXml(hdr.RequiredElement("size").Element("Rectangle")); var formatterFactory = new TagFormatterFactory(fileInfo.Version); var tags = hdr.RequiredElement("tags"); foreach (var xTag in tags.Elements()) { var tag = SwfTagNameMapping.CreateTagByXmlName(xTag.Name.LocalName); var formatter = formatterFactory.GetFormatter(tag); tag = formatter.ParseTo(xTag, tag); file.Tags.Add(tag); } return(file); }
public void CompressZlibTest() { var source = new byte[] { 0x46, 0x57, 0x53, 0x09, 0x20, 0x00, 0x00, 0x00, 0x78, 0x00, 0x07, 0x08, 0x00, 0x00, 0x11, 0xf8, 0x00, 0x00, 0x14, 0x01, 0x00, 0x44, 0x11, 0x08, 0x00, 0x00, 0x00, 0x43, 0x02, 0xff, 0x00, 0x00 }; var target = new MemoryStream(); SwfFile.Compress(new MemoryStream(source), target, SwfFormat.CWS); var res = target.ToArray(); Assert.AreEqual(0x09, res[3]); Assert.AreEqual(0x20, res[4]); Assert.AreEqual(0x00, res[5]); Assert.AreEqual(0x00, res[6]); Assert.AreEqual(0x00, res[7]); }
public void DefineBitsJPEG2Test() { var file = new SwfFile(); file.FileInfo.Version = 10; var tag = new DefineBitsJPEG2Tag(); tag.CharacterID = 1; tag.ImageData = GetEmbeddedResourceData("DefineBitsJPEG2.jpg"); var visitor = new SwfTagSerializer(file); var res = visitor.GetTagData(tag); var mem = new MemoryStream(); var writer = new SwfStreamWriter(mem); writer.WriteTagData(res); var etalon = GetTagFullBinariesFromSwfResource("DefineBitsJPEG2.swf") .FirstOrDefault(item => item.Type == SwfTagType.DefineBitsJPEG2); if (etalon.Binary == null) throw new InvalidOperationException("Couldn't find etalon tag"); AssertExt.AreEqual(etalon.Binary, mem.ToArray(), "Checking DefineBitsJPEG2"); }
static void SwfToXml(string[] args) { if (args.Length < 2) { Console.WriteLine("Source file wasn't specified"); ShowUsage(); return; } if (args.Length < 3) { Console.WriteLine("target file wasn't specified"); ShowUsage(); return; } using (var file = File.Open(args[1], FileMode.Open, FileAccess.Read)) { var swf = SwfFile.ReadFrom(file); var facade = new SwfMillFacade(); var doc = facade.ConvertToXml(swf); doc.Save(args[2]); } }
void ReadFile() { var swf = SwfFile.ReadFrom(m_reader.BaseStream); var tag = swf.Tags.FirstOrDefault(x => x is SymbolClassTag) as SymbolClassTag; if (tag == null) { return; } FrameRate = (uint)swf.Header.FrameRate; //Version = swf.FileInfo.Version; Version = 0; //It seem all swl have 0 for Version foreach (var reference in tag.References) { Classes.Add(reference.SymbolName); } m_reader.Seek(0, SeekOrigin.Begin); SwfData = m_reader.ReadBytes((int)m_reader.BytesAvailable); }
protected SwfTagData ReadTagData(string resourceName, string tagHash) { using (var stream = OpenEmbeddedResource(resourceName)) { var file = new SwfFile(); var reader = new SwfStreamReader(stream); file.FileInfo = reader.ReadSwfFileInfo(); reader = GetSwfStreamReader(file.FileInfo, stream); file.Header = reader.ReadSwfHeader(); while (!reader.IsEOF) { var tagData = reader.ReadTagData(); var hash = GetTagHash(tagData); if (tagHash == hash) { return(tagData); } } } return(null); }
public void ReadMatrixTest() { var file = new SwfFile(); file.FileInfo.Version = 10; var tags = GetTagBinariesFromSwfResource("Matrix-compiled.swf") .Where(item => item.Type == SwfTagType.PlaceObject2); var tagData = tags.First(); var ser = new SwfTagDeserializer(file); var tag = ser.ReadTag<PlaceObject2Tag>(tagData); Assert.AreEqual(20.5, tag.Matrix.ScaleX); Assert.AreEqual(17.25, tag.Matrix.ScaleY); tagData = tags.Skip(1).First(); tag = ser.ReadTag<PlaceObject2Tag>(tagData); Assert.AreEqual(0.5, tag.Matrix.ScaleX); Assert.AreEqual(1.25, tag.Matrix.ScaleY); tagData = tags.Skip(2).First(); tag = ser.ReadTag<PlaceObject2Tag>(tagData); Assert.AreEqual(0.5, tag.Matrix.ScaleX); Assert.AreEqual(-1.25, tag.Matrix.ScaleY); }
/// <summary> /// Decompresses the specified source. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> public void Decompress(Stream source, Stream target) { SwfFile.Decompress(source, target); }
private XElement GetSwfHeaderXml(SwfFile file) { var header = file.Header; return new XElement("Header", new XAttribute("framerate", header.FrameRate), new XAttribute("frames", header.FrameCount), new XElement("size", XRect.ToXml(header.FrameSize)), GetTagsXml(file.Tags)); }
/// <summary> /// Gets all the mods and patches them. /// </summary> private void worker_DoWork(object sender, DoWorkEventArgs e) { _errorLevel = ErrorLevel.NoError; //Gets the list of mods ItemCollection modCollection = null; string lolLocation = null; Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => { modCollection = ModsListBox.Items; lolLocation = LocationTextbox.Text; })); SetStatusLabelAsync("Gathering mods..."); //Gets the list of mods that have been checked. List <LessMod> modsToPatch = new List <LessMod>(); foreach (var x in modCollection) { CheckBox box = (CheckBox)x; bool isBoxChecked = false; Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => { isBoxChecked = box.IsChecked ?? false; })); if (isBoxChecked) { modsToPatch.Add(_lessMods[box]); } } Dictionary <string, SwfFile> swfs = new Dictionary <string, SwfFile>(); timer = Stopwatch.StartNew(); foreach (var lessMod in modsToPatch) { Debug.Assert(lessMod.Patches.Length > 0); SetStatusLabelAsync("Patching mod: " + lessMod.Name); foreach (var patch in lessMod.Patches) { if (!swfs.ContainsKey(patch.Swf)) { string fullPath = Path.Combine(lolLocation, patch.Swf); //Backup the SWF string CurrentLocation = ""; string[] FileLocation = patch.Swf.Split('/', '\\'); foreach (string s in FileLocation.Take(FileLocation.Length - 1)) { CurrentLocation = Path.Combine(CurrentLocation, s); if (!Directory.Exists(Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, CurrentLocation))) { Directory.CreateDirectory(Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, CurrentLocation)); } } if (!File.Exists(Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, patch.Swf))) { File.Copy(Path.Combine(lolLocation, patch.Swf), Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, patch.Swf)); } swfs.Add(patch.Swf, SwfFile.ReadFile(fullPath)); } SwfFile swf = swfs[patch.Swf]; List <DoAbcTag> tags = swf.GetDoAbcTags(); bool classFound = false; foreach (var tag in tags) { //check if this tag contains our script ScriptInfo si = tag.GetScriptByClassName(patch.Class); //check next tag if it doesn't if (si == null) { continue; } ClassInfo cls = si.GetClassByClassName(patch.Class); classFound = true; Assembler asm; switch (patch.Action) { case "replace_trait": //replace trait (method) asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); TraitInfo newTrait = asm.Assemble() as TraitInfo; int traitIndex = cls.Instance.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); bool classTrait = false; if (traitIndex < 0) { traitIndex = cls.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); classTrait = true; } if (traitIndex < 0) { throw new TraitNotFoundException(String.Format("Can't find trait \"{0}\" in class \"{1}\"", newTrait.Name.Name, patch.Class)); } if (classTrait) { cls.Traits[traitIndex] = newTrait; } else { cls.Instance.Traits[traitIndex] = newTrait; } break; case "replace_cinit": //replace class constructor asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); cls.ClassInit = asm.Assemble() as MethodInfo; break; case "replace_iinit": //replace instance constructor asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); cls.Instance.InstanceInit = asm.Assemble() as MethodInfo; break; case "add_class_trait": //add new class trait (method) asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); newTrait = asm.Assemble() as TraitInfo; traitIndex = cls.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); if (traitIndex < 0) { cls.Traits.Add(newTrait); } else { cls.Traits[traitIndex] = newTrait; } break; case "add_instance_trait": //add new instance trait (method) asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); newTrait = asm.Assemble() as TraitInfo; traitIndex = cls.Instance.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); if (traitIndex < 0) { cls.Instance.Traits.Add(newTrait); } else { cls.Instance.Traits[traitIndex] = newTrait; } break; case "remove_class_trait": throw new NotImplementedException(); case "remove_instance_trait": throw new NotImplementedException(); default: throw new NotSupportedException("Unknown Action \"" + patch.Action + "\" in mod " + lessMod.Name); } } if (!classFound) { _errorLevel = ErrorLevel.UnableToPatch; throw new ClassNotFoundException(string.Format("Class {0} not found in file {1}", patch.Class, patch.Swf)); } } } //return; foreach (var patchedSwf in swfs) { try { SetStatusLabelAsync("Applying mods: " + patchedSwf.Key); string swfLoc = Path.Combine(lolLocation, patchedSwf.Key); SwfFile.WriteFile(patchedSwf.Value, swfLoc); } catch { _errorLevel = ErrorLevel.GoodJobYourInstallationIsProbablyCorruptedNow; if (Debugger.IsAttached) { throw; } } } timer.Stop(); }
/// <summary> /// Compresses the specified source. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> public void Compress(Stream source, Stream target, SwfFormat format) { SwfFile.Compress(source, target, format); }
private XElement GetRoot(SwfFile file) { return new XElement("swf", new XAttribute("version", file.FileInfo.Version), new XAttribute("compressed", IsCompressed(file) ? "1" : "0"), GetSwfHeaderXml(file) ); }
protected IEnumerable<SwfTagData> IterateTags(Stream stream) { var file = new SwfFile(); var reader = new SwfStreamReader(stream); file.FileInfo = reader.ReadSwfFileInfo(); reader = GetSwfStreamReader(file.FileInfo, stream); file.Header = reader.ReadSwfHeader(); while (!reader.IsEOF) { var tagData = reader.ReadTagData(); yield return tagData; } }
/// <summary> /// /// </summary> /// <param name="inputTag"></param> /// <param name="sourceFile"></param> /// <param name="input"></param> public void Read(Tag inputTag, SwfFile sourceFile, Stream input) { _tag = inputTag; _SourceFileReference = sourceFile; _dataStream = inputTag.ReadContent(input); String s = String.Format("0x{0:X08}: reading tag content for {1}", _tag.OffsetData, (inputTag.IsTagTypeKnown ? inputTag.TagTypeName : inputTag.TagType.ToString())); // Log.Debug(this, s); Parse(); }
/// <summary> /// Gets all the mods and patches them. /// </summary> private void worker_DoWork(object sender, DoWorkEventArgs e) { _errorLevel = ErrorLevel.NoError; //Gets the list of mods ItemCollection modCollection = null; Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => { modCollection = ModsListBox.Items; })); SetStatusLabelAsync("Gathering mods..."); //Gets the list of mods that have been checked. List <LessMod> modsToPatch = new List <LessMod>(); foreach (var x in modCollection) { CheckBox box = (CheckBox)x; bool isBoxChecked = false; Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => { isBoxChecked = box.IsChecked ?? false; })); if (isBoxChecked) { modsToPatch.Add(_lessMods[box]); } } string lolLocation = null; bool overwrite = true; Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => { lolLocation = LocationTextbox.Text; })); if (IsGarena) { MessageBox.Show("Garena detected! Please note that you must uninstall LESs before patching your LoL with Garena." + Environment.NewLine + "Otherwise, your Garena LoL patcher will complain hard and we are not responsible for it ;)."); Uri lolRootLocation = new Uri(lolLocation); lolRootLocation = new Uri(lolRootLocation.LocalPath.Replace(lolRootLocation.Segments.Last(), String.Empty)); // Get LoL latest patched date String versionLocation = Path.Combine(lolRootLocation.LocalPath, "lol.version"); if (File.Exists(versionLocation)) { // Store the date in another file. It will be used in LESs removing. File.Copy(versionLocation, Path.Combine(lolRootLocation.LocalPath, "LESs_recent.version"), true); } if (Directory.Exists(Path.Combine(lolLocation, "LESsBackup"))) { MessageBoxResult diagRst = MessageBox.Show("We found that you already have backup files. Overwriting it may result in you losing your original files." + Environment.NewLine + "Would you like to overwrite your old files?", "You already have backup files", MessageBoxButton.YesNo); if (diagRst == MessageBoxResult.No) { overwrite = false; } } } Dictionary <string, SwfFile> swfs = new Dictionary <string, SwfFile>(); Stahpwatch = Stopwatch.StartNew(); foreach (var lessMod in modsToPatch) { Debug.Assert(lessMod.Patches.Length > 0); SetStatusLabelAsync("Patching mod: " + lessMod.Name); foreach (var patch in lessMod.Patches) { if (!swfs.ContainsKey(patch.Swf)) { string fullPath = Path.Combine(lolLocation, patch.Swf); //Backup the SWF string CurrentLocation = ""; string[] FileLocation = patch.Swf.Split('/'); foreach (string s in FileLocation.Take(FileLocation.Length - 1)) { CurrentLocation = Path.Combine(CurrentLocation, s); if (IsGarena) { if (!Directory.Exists(Path.Combine(lolLocation, "LESsBackup", CurrentLocation))) { Directory.CreateDirectory(Path.Combine(lolLocation, "LESsBackup", CurrentLocation)); } if (!File.Exists(Path.Combine(lolLocation, "LESsBackup", patch.Swf))) { if (overwrite) { File.Copy(Path.Combine(lolLocation, patch.Swf), Path.Combine(lolLocation, "LESsBackup", patch.Swf)); } } } else { if (!Directory.Exists(Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, CurrentLocation))) { Directory.CreateDirectory(Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, CurrentLocation)); } if (!File.Exists(Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, patch.Swf))) { File.Copy(Path.Combine(lolLocation, patch.Swf), Path.Combine(lolLocation, "LESsBackup", INTENDED_VERSION, patch.Swf)); } } } swfs.Add(patch.Swf, SwfFile.ReadFile(fullPath)); } SwfFile swf = swfs[patch.Swf]; List <DoAbcTag> tags = swf.GetDoAbcTags(); bool classFound = false; foreach (var tag in tags) { //check if this tag contains our script ScriptInfo si = tag.GetScriptByClassName(patch.Class); //check next tag if it doesn't if (si == null) { continue; } ClassInfo cls = si.GetClassByClassName(patch.Class); classFound = true; Assembler asm; switch (patch.Action) { case "replace_trait": //replace trait (method) asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); TraitInfo newTrait = asm.Assemble() as TraitInfo; int traitIndex = cls.Instance.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); bool classTrait = false; if (traitIndex < 0) { traitIndex = cls.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); classTrait = true; } if (traitIndex < 0) { throw new TraitNotFoundException(String.Format("Can't find trait \"{0}\" in class \"{1}\"", newTrait.Name.Name, patch.Class)); } if (classTrait) { cls.Traits[traitIndex] = newTrait; } else { cls.Instance.Traits[traitIndex] = newTrait; } break; case "replace_cinit": //replace class constructor asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); cls.ClassInit = asm.Assemble() as MethodInfo; break; case "replace_iinit": //replace instance constructor asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); cls.Instance.InstanceInit = asm.Assemble() as MethodInfo; break; case "add_class_trait": //add new class trait (method) asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); newTrait = asm.Assemble() as TraitInfo; traitIndex = cls.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); if (traitIndex < 0) { cls.Traits.Add(newTrait); } else { cls.Traits[traitIndex] = newTrait; } break; case "add_instance_trait": //add new instance trait (method) asm = new Assembler(File.ReadAllText(Path.Combine(lessMod.Directory, patch.Code))); newTrait = asm.Assemble() as TraitInfo; traitIndex = cls.Instance.GetTraitIndexByTypeAndName(newTrait.Type, newTrait.Name.Name); if (traitIndex < 0) { cls.Instance.Traits.Add(newTrait); } else { cls.Instance.Traits[traitIndex] = newTrait; } break; case "remove_class_trait": throw new NotImplementedException(); case "remove_instance_trait": throw new NotImplementedException(); default: throw new NotSupportedException("Unknown Action \"" + patch.Action + "\" in mod " + lessMod.Name); } } if (!classFound) { _errorLevel = ErrorLevel.UnableToPatch; throw new ClassNotFoundException(string.Format("Class {0} not found in file {1}", patch.Class, patch.Swf)); } } } //return; foreach (var patchedSwf in swfs) { try { SetStatusLabelAsync("Applying mods: " + patchedSwf.Key); string swfLoc = Path.Combine(lolLocation, patchedSwf.Key); SwfFile.WriteFile(patchedSwf.Value, swfLoc); } catch { _errorLevel = ErrorLevel.GoodJobYourInstallationIsProbablyCorruptedNow; if (Debugger.IsAttached) { throw; } } } Stahpwatch.Stop(); }
public static void Process() { const string championsSquare = @"League of Legends\Champions\Square\"; const string championsPortrait = @"League of Legends\Champions\Portrait\"; const string championsLandscape = @"League of Legends\Champions\Landscape\"; const string abilities = @"League of Legends\Abilities\"; const string items = @"League of Legends\Items\"; const string spells = @"League of Legends\Spells\"; const string masteries = @"League of Legends\Masteries\"; const string runes = @"League of Legends\Runes\"; const string wards = @"League of Legends\Wards\"; string[] directories = { championsSquare, championsPortrait, championsLandscape, abilities, items, spells, masteries, runes, wards }; Helper.BuildDirectoryTree(directories); // Get the path of the source var ini = new INIFile(Globals.Paths.ConfigurationFile); var sourcePath = ini.INIReadValue("Game Paths", "League of Legends"); // Get the source string[] neededSwFs = { "ImagePack_spells.swf", "ImagePack_masteryIcons.swf", "ImagePack_items.swf" }; foreach ( var neededSwf in Directory.GetFiles(sourcePath, "ImagePack_*.swf", SearchOption.AllDirectories) .Where(f => neededSwFs.Contains(Path.GetFileName(f), StringComparer.OrdinalIgnoreCase)) .ToList()) { File.Copy(neededSwf, Path.Combine(Globals.Paths.Assets, "Source", "League of Legends", Path.GetFileName(neededSwf)), true); Console.WriteLine("Copying {0}", neededSwf); } // Extract the SWFs foreach ( var swfFile in Directory.GetFiles(Path.Combine(Globals.Paths.Assets, "Source", "League of Legends"), "*.swf", SearchOption.AllDirectories).ToList()) { string outputPath = null; switch (Path.GetFileName(swfFile)) { case "ImagePack_items.swf": outputPath = items; break; case "ImagePack_spells.swf": outputPath = spells; break; case "ImagePack_masteryIcons.swf": outputPath = masteries; break; default: break; } var swf = new SwfFile(swfFile); if (outputPath != null) { swf.ExtractImages(Path.Combine(Globals.Paths.Assets, outputPath, "Source")); } } // Copy the rest of the source assets // Copy jobs take the form { output path = string, { string start path, bool recursion flag, string search pattern, string exclude pattern } } const string sourceReleases = @"RADS\projects\lol_air_client\releases"; var sourceVersion = Directory.GetDirectories(Path.Combine(sourcePath, sourceReleases))[0]; var sourceAssets = Path.Combine(sourcePath, sourceReleases, sourceVersion, @"deploy\assets"); var copyJobs = new List <CopyJob> { new CopyJob(championsPortrait, Path.Combine(sourceAssets, @"images\champions"), false, "*_0.jpg", "*_S*_*.jpg"), new CopyJob(championsLandscape, Path.Combine(sourceAssets, @"images\champions"), false, "*_Splash_0.jpg", null), new CopyJob(championsSquare, Path.Combine(sourceAssets, @"images\champions"), false, "*_Square_0.png", null), new CopyJob(abilities, Path.Combine(sourceAssets, @"images\abilities"), false, "*.png", null), new CopyJob(runes, Path.Combine(sourceAssets, @"images\runes"), true, "*.png", null), new CopyJob(wards, Path.Combine(sourceAssets, @"images\misc\wards"), false, "wardImage_*.png", null) }; Helper.BatchFileCopy(copyJobs); // Rename all the things Helper.BatchFileRename("League of Legends"); // Scale all the things // Scaling jobs take the form { string start path, string search pattern, string exclude pattern } var scalingJobs = new List <ScalingJob> { new ScalingJob(championsLandscape, "*.jpg"), new ScalingJob(championsPortrait, "*.jpg"), new ScalingJob(championsSquare, "*.png"), new ScalingJob(abilities, "*.png"), new ScalingJob(items, "*.png"), new ScalingJob(spells, "*.png"), new ScalingJob(masteries, "*.png"), new ScalingJob(runes, "*.png"), new ScalingJob(wards, "*.png") }; Helper.BatchIMScale(scalingJobs); }
public void Patch(long ts, ref byte[] swf, bool isLoader) { var swfFile = new SwfFile(swf); string version = null; foreach (var tag in swfFile.Tags) { if (tag.Type == 82) { byte[] cnt = tag.Content; uint pos = 4; while (cnt[pos] != 0) { pos++; } ABCFile file = new ABCReader(cnt, ++pos).abc; if (isLoader) { for (int i = 0; i < file.strings.Length; i++) { if (file.strings[i] == "www.realmofthemadgod.com") { file.strings[i] = targetHostWithPort; } else if (file.strings[i] == "realmofthemadgodhrd.appspot.com") { file.strings[i] = targetHostWithPort; } else if (file.strings[i] == "rotmg_loader_port") { // When use AGCLoader directly, rotmg_loader_port not set in flashvars, // so port number must be appended. // However when use homepage, rotmg_loader_port is set, // so port number must not be appended. // To simpify the cases, patch the reading of that variable. file.strings[i] = "x"; } } } else { int stringRef = 0; for (int i = 0; i < file.strings.Length; i++) { if (file.strings[i] == "www.realmofthemadgod.com") { file.strings[i] = targetHost; stringRef = i; } else if (file.strings[i] == "realmofthemadgodhrd.appspot.com") { file.strings[i] = targetHostWithPort; } else if (file.strings[i] == "xlate.kabam.com") { // Not necessary, but prevent the background 'Play at' text from appearing. file.strings[i] = targetHost; } else if (file.strings[i] == "https://") { file.strings[i] = "http://"; } } for (int i = 0; i < file.classes.Length; i++) { for (int j = 0; j < file.classes[i].traits.Length; j++) { var trait = file.classes[i].traits[j]; if (!(trait.kind == TraitKind.Const && trait.Slot.vkind == ASType.Utf8 && trait.Slot.vindex == stringRef)) { continue; } // patch string decrypter var cinit = file.classes[i].cinit; for (int k = 0; k < file.bodies.Length; k++) { var body = file.bodies[k]; if (body.method != cinit) { continue; } for (int l = 0; l < body.instructions.Length; l++) // ugh. ijkl, so many loops... { var instr = body.instructions[l]; if (instr.opcode != Opcode.OP_initproperty || instr.arguments[0].index != trait.name) { continue; } instr.opcode = Opcode.OP_pop; instr.arguments = new ABCFile.Instruction.Argument[0]; var newInstrs = new List <ABCFile.Instruction>(body.instructions); newInstrs[l] = instr; newInstrs.Insert(l, instr); // fix branches for (int m = 0; m < newInstrs.Count; m++) { var opcodeInfo = OpcodeInfo.opcodeDict[newInstrs[m].opcode]; for (int n = 0; n < opcodeInfo.argumentTypes.Length; n++) // ...and a few more =P { switch (opcodeInfo.argumentTypes[n]) { case OpcodeArgumentType.JumpTarget: case OpcodeArgumentType.SwitchDefaultTarget: if (newInstrs[m].arguments[n].jumpTarget.index > l) { newInstrs[m].arguments[n].jumpTarget.index++; } break; case OpcodeArgumentType.SwitchTargets: for (int o = 0; o < newInstrs[m].arguments[n].switchTargets.Length; o++) { if (newInstrs[m].arguments[n].switchTargets[o].index > l) { newInstrs[m].arguments[n].switchTargets[o].index++; } } break; default: break; } } } body.instructions = newInstrs.ToArray(); break; } file.bodies[k] = body; break; } break; } } version = SWFAnalyzer.AnalyzePackets(tool, ts, file); } var newAbc = new ABCWriter(file).buf; var newTag = new byte[newAbc.Length + pos]; Buffer.BlockCopy(cnt, 0, newTag, 0, (int)pos); Buffer.BlockCopy(newAbc, 0, newTag, (int)pos, newAbc.Length); tag.Content = newTag; } } if (version != null) { SWFAnalyzer.AnalyzeXML(tool, version, swfFile); } var newSwf = swfFile.Write(); swf = newSwf; }
protected SwfFile ReadSwfFile(string resourceName) { using (var stream = OpenEmbeddedResource(resourceName)) { return(SwfFile.ReadFrom(stream)); } }