public void TestOblivionSDP_File() { const string file = "oblivion-test-sdp.sdp"; using (var fs = File.Open(file, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) using (var bw = new BinaryWriter(fs, Encoding.UTF8, false)) { const uint count = 2; bw.Write(0x64); bw.Write(count); bw.Write(0x248); for (var i = 0; i < count; i++) { Span <char> name = new char[0x100]; name.Fill((char)0); var sName = $"MyShader{i}".AsSpan(); sName.CopyTo(name); bw.Write(name); bw.Write(0x20); Span <byte> data = new byte[0x20]; data.Fill(0); data[0] = (byte)i; bw.Write(data); } } var newData = new byte[0x10]; OblivionSDP.EditShaderPackage(file, "MyShader1", newData); using var stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read); using var br = new BinaryReader(stream, Encoding.UTF8, false); Assert.Equal((uint)0x64, br.ReadUInt32()); Assert.Equal((uint)2, br.ReadUInt32()); //new changed size Assert.Equal((uint)0x238, br.ReadUInt32()); //skip first shader and the name of the second one br.BaseStream.Seek(0x100 + 4 + 0x20 + 0x100, SeekOrigin.Current); //new length of the changed shader Assert.Equal((uint)newData.Length, br.ReadUInt32()); }
/// <summary> /// Executes the edit and changes a shader package. /// </summary> /// <param name="extractionFolder">Path to the extraction folder, this should be <see cref="ScriptReturnData.DataFolder"/></param>. /// <param name="shaderPath">Path to the shader file that should be edited.</param> /// <param name="outputPath">Optional output path if you don't want the shader to be overwritten.</param> /// <exception cref="ArgumentException">The file does not exist</exception> public void ExecuteEdit(string extractionFolder, string shaderPath, string?outputPath = null) { var filePath = File.GetFileInFolder(extractionFolder); if (!System.IO.File.Exists(filePath)) { throw new ArgumentException($"File does not exist: {filePath}"); } if (!System.IO.File.Exists(shaderPath)) { throw new ArgumentException($"File does not exist: {shaderPath}"); } var bytes = System.IO.File.ReadAllBytes(filePath); OblivionSDP.EditShaderPackage(shaderPath, Shader, bytes, outputPath); }
public void TestOblivionSDP_Stream() { using var ms = new MemoryStream(1024); using (var bw = new BinaryWriter(ms, Encoding.UTF8, true)) { const uint count = 2; bw.Write(0x64); bw.Write(count); bw.Write(0x248); for (var i = 0; i < count; i++) { Span <char> name = new char[0x100]; name.Fill((char)0); var sName = $"MyShader{i}".AsSpan(); sName.CopyTo(name); bw.Write(name); bw.Write(0x20); Span <byte> data = new byte[0x20]; data.Fill(0); data[0] = (byte)i; bw.Write(data); } ms.Position = 0; } var newData = new byte[0x10]; OblivionSDP.EditShaderPackage(ms, "MyShader1", newData, out var outputStream); using var br = new BinaryReader(outputStream, Encoding.UTF8, false); Assert.Equal((uint)0x64, br.ReadUInt32()); Assert.Equal((uint)2, br.ReadUInt32()); //new changed size Assert.Equal((uint)0x238, br.ReadUInt32()); //skip first shader and the name of the second one br.BaseStream.Seek(0x100 + 4 + 0x20 + 0x100, SeekOrigin.Current); //new length of the changed shader Assert.Equal((uint)newData.Length, br.ReadUInt32()); }