Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cmkushnir committed Oct 12, 2022
1 parent 86803bf commit a9451f3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 91 deletions.
9 changes: 4 additions & 5 deletions Common/NMS/PAK/Data/PAK.MBIN.Data.cs
Expand Up @@ -19,16 +19,15 @@
//=============================================================================

using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;

//=============================================================================

namespace cmk.NMS.PAK.MBIN
{
// Replaces libMBIN.MBINFile
public class Data
// Replaces libMBIN.MBINFile
public class Data
: cmk.NMS.PAK.Item.Data
{
// log_mbin_guid_mismatch command-line setting:
Expand Down Expand Up @@ -227,10 +226,10 @@ public override bool Save( Log LOG = null )
var is_edited = raw_bytes.Length != mod_bytes.Length ?
true : PInvoke.memcmp(raw_bytes, mod_bytes, mod_bytes.Length) != 0
;
if( !is_edited ) return true; // nothing to update this Save

// once file data flagged as IsEdited, keep flagged as IsEdited
if( is_edited ) IsEdited = true;
if( !is_edited ) return true; // nothing to update this Save
IsEdited = true;

lock( this ) {
var new_raw = Mbinc?.NMSTemplateToRaw(mod_object, LOG);
Expand Down
112 changes: 103 additions & 9 deletions Common/NMS/PAK/Data/PAK.SPV.Data.cs
Expand Up @@ -28,7 +28,7 @@

namespace cmk.NMS.PAK.SPV
{
public class Data
public class Data
: cmk.NMS.PAK.TXT.Data
{
static Data()
Expand Down Expand Up @@ -63,9 +63,33 @@ public Data( string PATH, Stream RAW = null, Log LOG = null )
/// Use spirv-cross.exe to decompile.
/// </summary>
protected override string RawToText( Log LOG = null )
=> RawToTextSpirvCross(Raw, LOG).Replace("_RESERVED_IDENTIFIER_FIXUP", "");

//...........................................................

protected override bool TextToRaw( string TEXT, Log LOG = null )
{
try {
var stream = TextToRawVeldrid(TEXT, LOG);
lock( Raw ) {
Raw.Position = 0;
Raw.SetLength(0);
stream.CopyTo(Raw);
}
return true;
}
catch( Exception EX ) {
LOG.AddFailure(EX);
return false;
}
}

//...........................................................

public string RawToTextSpirvCross( Stream RAW, Log LOG = null )
{
lock( Raw ) try {
Raw.Position = 0;
lock( RAW ) try {
RAW.Position = 0;
using( Process process = new() ) {
process.StartInfo.FileName = "spirv-cross.exe";
process.StartInfo.Arguments = "- -V"; // '-' use stdin, '-V' output vulkan glsl
Expand All @@ -75,7 +99,7 @@ protected override string RawToText( Log LOG = null )
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.Start();
Raw.CopyTo(process.StandardInput.BaseStream);
RAW.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close(); // required
return process.StandardOutput.ReadToEnd();
}
Expand All @@ -88,20 +112,90 @@ protected override string RawToText( Log LOG = null )

//...........................................................

protected override bool TextToRaw( string TEXT, Log LOG = null )
protected readonly Veldrid.SPIRV.CrossCompileOptions m_Veldrid_Cross_Options = new(){
NormalizeResourceNames = true
};

public string RawToTextVeldridCompute( Stream RAW, Log LOG = null )
{
lock( Raw ) try {
Raw.Position = 0;
throw new NotImplementedException();
try {
byte[] bytes = null;
lock( RAW ) {
RAW.Position = 0;
bytes = RAW.ToArray();
}
var result = Veldrid.SPIRV.SpirvCompilation.CompileCompute(
bytes, Veldrid.SPIRV.CrossCompileTarget.GLSL, m_Veldrid_Cross_Options
);
return result.ComputeShader;
}
catch( Exception EX ) {
LOG.AddFailure(EX);
return false;
return null;
}
}

//...........................................................

protected readonly Veldrid.SPIRV.GlslCompileOptions m_Veldrid_Glsl_Options = new(
true // preserve debug info
//, param MacroDefinition[]
);

public MemoryStream TextToRawVeldrid( string TEXT, Log LOG = null )
{
try {
Veldrid.ShaderStages stages = Veldrid.ShaderStages.None;
if( Path.Name.Contains("_COMP_") ) stages = Veldrid.ShaderStages.Compute;
else if( Path.Name.Contains("_FRAG_") ) stages = Veldrid.ShaderStages.Fragment;
else if( Path.Name.Contains("_VERT_") ) stages = Veldrid.ShaderStages.Vertex;

var spirv = Veldrid.SPIRV.SpirvCompilation.CompileGlslToSpirv(
TEXT, string.Empty, stages, m_Veldrid_Glsl_Options
);

var bytes = spirv.SpirvBytes;
return new MemoryStream(bytes);
}
catch( Exception EX ) {
LOG.AddFailure(EX);
return null;
}
}

//...........................................................

public string Main( Log LOG = null )
{
var text = RawToTextSpirvCross(Raw, LOG);

var start = text.IndexOf("void main()");
var end = text.LastIndexOf('}') + 1;
if( start < 0 || end < start ) return "";

return text.Substring(start, end - start);
}

//...........................................................

public string Main( string MAIN, bool UPDATE = true, Log LOG = null )
{
var text = RawToTextSpirvCross(Raw, LOG);

var start = text.IndexOf("void main()");
var end = text.LastIndexOf('}') + 1;
if( start < 0 || end < start ) return "";

text = text.Remove(start, end - start);
text = text.Insert(start, MAIN);

if( UPDATE ) Text = text;

return text;
}

//...........................................................

protected override void SaveFilePrepare( SaveFileDialog DIALOG, Log LOG = null )
{
base.SaveFilePrepare(DIALOG, LOG);
Expand Down
38 changes: 2 additions & 36 deletions Common/NMS/PAK/PAK.File.Loader.cs
Expand Up @@ -25,7 +25,6 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using cmk.NMS.PAK.Item;

//=============================================================================
Expand Down Expand Up @@ -143,6 +142,8 @@ public string SubPath

protected bool Load( Log LOG = null, CancellationToken CANCEL = default )
{
if( Path.Name == "x" ) {
}
try {
using( var pak = WaitOpenSharedReadOnly(default, CANCEL) ) {
if( pak == null ) {
Expand Down Expand Up @@ -591,41 +592,6 @@ public AS_T ExtractData<AS_T>( string PATH, bool NORMALIZE = false, Log LOG = nu

//...........................................................

/// <summary>
/// Extract DDS item and convert to a BitmapSource.
/// Discards PAK.DDS.Data wrapper after conversion.
/// </summary>
public BitmapSource ExtractDdsBitmapSource( NMS.PAK.Item.Info INFO, int HEIGHT = 32, Log LOG = null, CancellationToken CANCEL = default )
{
if( INFO.Path.Extension.ToUpper() != ".DDS" ) {
LOG.AddFailure($"{Path.NameExt} {INFO.Path} - Not a *.DDS file");
return null;
}

var data = ExtractData<NMS.PAK.DDS.Data>(INFO, LOG, CANCEL);
if( data == null ) return null;

var bitmap = data.Dds?.GetBitmap(HEIGHT < 16 ? 256 : HEIGHT, true);
if( bitmap == null ) {
LOG.AddFailure($"{Path.NameExt} {data.Path} - Unable to convert dds to bitmap");
return null;
}

return bitmap;
}

public BitmapSource ExtractDdsBitmapSource( string PATH, bool NORMALIZE = false, int HEIGHT = 32, Log LOG = null, CancellationToken CANCEL = default )
{
var info = FindInfo(PATH, NORMALIZE);
if( info == null ) {
LOG.AddFailure($"{Path.NameExt} {PATH} - Unable to find info");
return null;
}
return ExtractDdsBitmapSource(info, HEIGHT, LOG, CANCEL);
}

//...........................................................

/// <summary>
/// Extract MBIN or MBIN.PC item then decompile NMSTemplate based object.
/// Discards PAK.MBIN.Data wrapper after decompiling.
Expand Down
29 changes: 4 additions & 25 deletions Common/NMS/PAK/PAK.Files.cs
Expand Up @@ -23,16 +23,15 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

//=============================================================================

namespace cmk.NMS.PAK
{
/// <summary>
/// Manage a collection (directory) of .pak files.
/// </summary>
public class Files
/// <summary>
/// Manage a collection (directory) of .pak files.
/// </summary>
public class Files
: cmk.NMS.PAK.Item.ICollection
{
/// <summary>
Expand Down Expand Up @@ -282,26 +281,6 @@ public AS_T ExtractData<AS_T>( string PATH, bool NORMALIZE = false, Log LOG = nu

//...........................................................

/// <summary>
/// Extract DDS item and convert to a BitmapSource.
/// Discards PAK.DDS.Data wrapper after conversion.
/// </summary>
public BitmapSource ExtractDdsBitmapSource( string PATH, bool NORMALIZE = false, int HEIGHT = 32, Log LOG = null, CancellationToken CANCEL = default )
{
Lock.AcquireRead();
try {
var info = FindInfo(PATH, NORMALIZE);
if( info == null ) {
LOG.AddFailure($"{PATH} - unable to find info in {SubPath}*.pak");
return null;
}
return info.ExtractDdsBitmapSource(HEIGHT, LOG);
}
finally { Lock.ReleaseRead(); }
}

//...........................................................

/// <summary>
/// Extract MBIN or MBIN.PC item then decompile NMSTemplate based object.
/// Discards PAK.MBIN.Data wrapper after decompiling.
Expand Down
4 changes: 2 additions & 2 deletions Common/NMS/PAK/PAK.Item.Data.cs
Expand Up @@ -29,7 +29,7 @@

namespace cmk.NMS.PAK.Item
{
public interface IViewer
public interface IViewer
{
ImageButton ViewerButton { get; }
}
Expand Down Expand Up @@ -278,7 +278,7 @@ public void SaveFileDialog( Log LOG = null )
),
};
SaveFilePrepare(dialog, LOG);
if( dialog.ShowDialog() == true ) {
if( dialog.ShowDialog(System.Windows.Application.Current.MainWindow) == true ) {
try {
Resource.SaveDirectory = dialog.FileName;
SaveFileTo(dialog.FileName, LOG);
Expand Down
11 changes: 0 additions & 11 deletions Common/NMS/PAK/PAK.Item.Info.cs
Expand Up @@ -21,7 +21,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Media.Imaging;

//=============================================================================

Expand Down Expand Up @@ -115,16 +114,6 @@ public AS_T ExtractData<AS_T>( Log LOG = null )

//...........................................................

/// <summary>
/// Get this DDS Item Data from File and convert to a BitmapSource.
/// </summary>
public BitmapSource ExtractDdsBitmapSource( int HEIGHT = 32, Log LOG = null )
{
return File?.ExtractDdsBitmapSource(this, HEIGHT, LOG);
}

//...........................................................

/// <summary>
/// Get this MBIN or MBIN.PC Item Data from File and convert to NMSTemplate based DOM.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions Common/NMS/PAK/PAK.Item.Interfaces.cs
Expand Up @@ -22,7 +22,6 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Media.Imaging;

//=============================================================================

Expand Down Expand Up @@ -114,8 +113,6 @@ protected bool IsWordBorder( string TEXT, int OFFSET )
public AS_T ExtractData<AS_T>( string PATH, bool NORMALIZE = false, Log LOG = null, CancellationToken CANCEL = default )
where AS_T : NMS.PAK.Item.Data;

public BitmapSource ExtractDdsBitmapSource( string PATH, bool NORMALIZE = false, int HEIGHT = 32, Log LOG = null, CancellationToken CANCEL = default );

public AS_T ExtractMbin<AS_T>( string PATH, bool NORMALIZE = false, Log LOG = null, CancellationToken CANCEL = default )
where AS_T : class; // libMBIN.NMSTemplate

Expand Down

0 comments on commit a9451f3

Please sign in to comment.