mirror of
https://github.com/aelurum/AssetStudio.git
synced 2025-07-18 03:24:15 -04:00
clean up code
This commit is contained in:
@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Unity_Studio
|
||||
{
|
||||
@ -20,8 +21,6 @@ namespace Unity_Studio
|
||||
public string[] buildType;
|
||||
public int platform = 100663296;
|
||||
public string platformStr = "";
|
||||
//public EndianType endianType = EndianType.BigEndian;
|
||||
//public List<AssetPreloadData> preloadTable = new List<AssetPreloadData>();
|
||||
public Dictionary<long, AssetPreloadData> preloadTable = new Dictionary<long, AssetPreloadData>();
|
||||
public Dictionary<long, GameObject> GameObjectList = new Dictionary<long, GameObject>();
|
||||
public Dictionary<long, Transform> TransformList = new Dictionary<long, Transform>();
|
||||
@ -216,7 +215,6 @@ namespace Unity_Studio
|
||||
byte[] b32 = BitConverter.GetBytes(platform);
|
||||
Array.Reverse(b32);
|
||||
platform = BitConverter.ToInt32(b32, 0);
|
||||
//endianType = EndianType.LittleEndian;
|
||||
a_Stream.endian = EndianType.LittleEndian;
|
||||
}
|
||||
|
||||
@ -344,10 +342,6 @@ namespace Unity_Studio
|
||||
//this is a single byte, not an int32
|
||||
//the next entry is aligned after this
|
||||
//but not the last!
|
||||
if (unknownByte != 0)
|
||||
{
|
||||
//bool investigate = true;
|
||||
}
|
||||
}
|
||||
|
||||
string typeString;
|
||||
@ -424,7 +418,6 @@ namespace Unity_Studio
|
||||
{
|
||||
string varType = a_Stream.ReadStringToNull();
|
||||
string varName = a_Stream.ReadStringToNull();
|
||||
//a_Stream.Position += 20;
|
||||
int size = a_Stream.ReadInt32();
|
||||
int index = a_Stream.ReadInt32();
|
||||
int isArray = a_Stream.ReadInt32();
|
||||
@ -432,7 +425,6 @@ namespace Unity_Studio
|
||||
int flag = a_Stream.ReadInt32();
|
||||
int childrenCount = a_Stream.ReadInt32();
|
||||
|
||||
//Debug.WriteLine(baseFormat + " " + baseName + " " + childrenCount);
|
||||
cb.Add(new ClassMember()
|
||||
{
|
||||
Level = level - 1,
|
||||
@ -461,7 +453,7 @@ namespace Unity_Studio
|
||||
}
|
||||
classIDs.Add(new[] { type1, classID });
|
||||
classID = type1;
|
||||
/*TODO 替换?
|
||||
/*TODO 用来替换下方的代码
|
||||
if(classID == 114)
|
||||
{
|
||||
a_Stream.Position += 16;
|
||||
@ -530,9 +522,6 @@ namespace Unity_Studio
|
||||
Flag = flag
|
||||
});
|
||||
}
|
||||
|
||||
//for (int t = 0; t < level; t++) { Debug.Write("\t"); }
|
||||
//Debug.WriteLine(varTypeStr + " " + varNameStr + " " + size);
|
||||
}
|
||||
a_Stream.Position += stringSize;
|
||||
|
||||
|
@ -31,9 +31,7 @@ namespace Unity_Studio
|
||||
int compressedSize = lz4Stream.ReadInt32();
|
||||
int something = lz4Stream.ReadInt32(); //1
|
||||
|
||||
byte[] lz4buffer = new byte[compressedSize];
|
||||
lz4Stream.Read(lz4buffer, 0, compressedSize);
|
||||
|
||||
var lz4buffer = lz4Stream.ReadBytes(compressedSize);
|
||||
using (var inputStream = new MemoryStream(lz4buffer))
|
||||
{
|
||||
var decoder = new Lz4DecoderStream(inputStream);
|
||||
@ -97,9 +95,7 @@ namespace Unity_Studio
|
||||
case "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA": //.bytes
|
||||
case "UnityWeb":
|
||||
{
|
||||
byte[] lzmaBuffer = new byte[lzmaSize];
|
||||
b_Stream.Read(lzmaBuffer, 0, lzmaSize);
|
||||
|
||||
var lzmaBuffer = b_Stream.ReadBytes(lzmaSize);
|
||||
using (var lzmaStream = new EndianBinaryReader(SevenZipHelper.StreamDecompress(new MemoryStream(lzmaBuffer))))
|
||||
{
|
||||
getFiles(lzmaStream, 0);
|
||||
@ -131,16 +127,14 @@ namespace Unity_Studio
|
||||
int fileCount = f_Stream.ReadInt32();
|
||||
for (int i = 0; i < fileCount; i++)
|
||||
{
|
||||
MemoryAssetsFile memFile = new MemoryAssetsFile();
|
||||
var memFile = new MemoryAssetsFile();
|
||||
memFile.fileName = f_Stream.ReadStringToNull();
|
||||
int fileOffset = f_Stream.ReadInt32();
|
||||
fileOffset += offset;
|
||||
int fileSize = f_Stream.ReadInt32();
|
||||
long nextFile = f_Stream.Position;
|
||||
f_Stream.Position = fileOffset;
|
||||
|
||||
byte[] buffer = new byte[fileSize];
|
||||
f_Stream.Read(buffer, 0, fileSize);
|
||||
var buffer = f_Stream.ReadBytes(fileSize);
|
||||
memFile.memStream = new MemoryStream(buffer);
|
||||
MemoryAssetsFileList.Add(memFile);
|
||||
f_Stream.Position = nextFile;
|
||||
@ -252,8 +246,7 @@ namespace Unity_Studio
|
||||
var unknown = blocksInfo.ReadInt32();
|
||||
memFile.fileName = blocksInfo.ReadStringToNull();
|
||||
assetsData.Position = entryinfo_offset;
|
||||
var buffer = new byte[entryinfo_size];
|
||||
assetsData.Read(buffer, 0, (int)entryinfo_size);
|
||||
var buffer = assetsData.ReadBytes((int)entryinfo_size);
|
||||
memFile.memStream = new MemoryStream(buffer);
|
||||
MemoryAssetsFileList.Add(memFile);
|
||||
}
|
||||
|
@ -18,7 +18,9 @@ namespace Unity_Studio
|
||||
private byte[] a32 = new byte[4];
|
||||
private byte[] a64 = new byte[8];
|
||||
|
||||
public EndianBinaryReader(Stream stream, EndianType endian = EndianType.BigEndian) : base(stream) { this.endian = endian; }
|
||||
public EndianBinaryReader(Stream stream, EndianType endian = EndianType.BigEndian)
|
||||
: base(stream)
|
||||
{ this.endian = endian; }
|
||||
|
||||
public long Position
|
||||
{
|
||||
@ -121,28 +123,17 @@ namespace Unity_Studio
|
||||
|
||||
public void AlignStream(int alignment)
|
||||
{
|
||||
long pos = BaseStream.Position;
|
||||
//long padding = alignment - pos + (pos / alignment) * alignment;
|
||||
//if (padding != alignment) { base.BaseStream.Position += padding; }
|
||||
if ((pos % alignment) != 0) { BaseStream.Position += alignment - (pos % alignment); }
|
||||
var pos = BaseStream.Position;
|
||||
var mod = pos % alignment;
|
||||
if (mod != 0) { BaseStream.Position += alignment - mod; }
|
||||
}
|
||||
|
||||
public string ReadAlignedString(int length)
|
||||
{
|
||||
if (length > 0 && length < (BaseStream.Length - BaseStream.Position))//crude failsafe
|
||||
if (length > 0 && length < (BaseStream.Length - BaseStream.Position))
|
||||
{
|
||||
byte[] stringData = new byte[length];
|
||||
Read(stringData, 0, length);
|
||||
var result = Encoding.UTF8.GetString(stringData); //must verify strange characters in PS3
|
||||
|
||||
/*string result = "";
|
||||
char c;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
c = (char)base.ReadByte();
|
||||
result += c.ToString();
|
||||
}*/
|
||||
|
||||
var stringData = ReadBytes(length);
|
||||
var result = Encoding.UTF8.GetString(stringData);
|
||||
AlignStream(4);
|
||||
return result;
|
||||
}
|
||||
|
@ -38,17 +38,9 @@ namespace Unity_Studio
|
||||
|
||||
public static void LoadAssetsFile(string fileName)
|
||||
{
|
||||
//var loadedAssetsFile = assetsfileList.Find(aFile => aFile.filePath == fileName);
|
||||
//if (loadedAssetsFile == null)
|
||||
if (!assetsfileListHash.Contains(fileName))
|
||||
{
|
||||
//open file here and pass the stream to facilitate loading memory files
|
||||
//also by keeping the stream as a property of AssetsFile, it can be used later on to read assets
|
||||
AssetsFile assetsFile = new AssetsFile(fileName, new EndianBinaryReader(File.OpenRead(fileName)));
|
||||
//if (Path.GetFileName(fileName) == "mainData") { mainDataFile = assetsFile; }
|
||||
|
||||
//totalAssetCount += assetsFile.preloadTable.Count;
|
||||
|
||||
assetsfileList.Add(assetsFile);
|
||||
assetsfileListHash.Add(fileName);
|
||||
|
||||
@ -78,23 +70,8 @@ namespace Unity_Studio
|
||||
{
|
||||
string sharedFilePath = Path.GetDirectoryName(fileName) + "\\" + sharedFile.fileName;
|
||||
string sharedFileName = Path.GetFileName(sharedFile.fileName);
|
||||
|
||||
//var loadedSharedFile = assetsfileList.Find(aFile => aFile.filePath == sharedFilePath);
|
||||
/*var loadedSharedFile = assetsfileList.Find(aFile => aFile.filePath.EndsWith(Path.GetFileName(sharedFile.fileName)));
|
||||
if (loadedSharedFile != null) { sharedFile.Index = assetsfileList.IndexOf(loadedSharedFile); }
|
||||
else if (File.Exists(sharedFilePath))
|
||||
{
|
||||
//progressBar1.Maximum += 1;
|
||||
sharedFile.Index = assetsfileList.Count;
|
||||
LoadAssetsFile(sharedFilePath);
|
||||
}*/
|
||||
|
||||
//searching in unityFiles would preserve desired order, but...
|
||||
//var quedSharedFile = unityFiles.Find(uFile => String.Equals(Path.GetFileName(uFile), sharedFileName, StringComparison.OrdinalIgnoreCase));
|
||||
//if (quedSharedFile == null)
|
||||
if (!unityFilesHash.Contains(sharedFileName))
|
||||
{
|
||||
//if (!File.Exists(sharedFilePath)) { sharedFilePath = Path.GetDirectoryName(fileName) + "\\" + sharedFileName; }
|
||||
if (!File.Exists(sharedFilePath))
|
||||
{
|
||||
var findFiles = Directory.GetFiles(Path.GetDirectoryName(fileName), sharedFileName, SearchOption.AllDirectories);
|
||||
@ -107,7 +84,6 @@ namespace Unity_Studio
|
||||
sharedFile.Index = unityFiles.Count;
|
||||
unityFiles.Add(sharedFilePath);
|
||||
unityFilesHash.Add(sharedFileName);
|
||||
//progressBar1.Maximum++;
|
||||
value++;
|
||||
}
|
||||
}
|
||||
@ -166,16 +142,17 @@ namespace Unity_Studio
|
||||
{
|
||||
string destFile = Path.GetFileNameWithoutExtension(splitFile);
|
||||
string destPath = Path.GetDirectoryName(splitFile) + "\\";
|
||||
if (!File.Exists(destPath + destFile))
|
||||
var destFull = destPath + destFile;
|
||||
if (!File.Exists(destFull))
|
||||
{
|
||||
string[] splitParts = Directory.GetFiles(destPath, destFile + ".split*");
|
||||
using (var destStream = File.Create(destPath + destFile))
|
||||
using (var destStream = File.Create(destFull))
|
||||
{
|
||||
for (int i = 0; i < splitParts.Length; i++)
|
||||
{
|
||||
string splitPart = destPath + destFile + ".split" + i;
|
||||
string splitPart = destFull + ".split" + i;
|
||||
using (var sourceStream = File.OpenRead(splitPart))
|
||||
sourceStream.CopyTo(destStream); // You can pass the buffer size as second argument.
|
||||
sourceStream.CopyTo(destStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -339,20 +316,22 @@ namespace Unity_Studio
|
||||
}
|
||||
if (!exportable && displayAll)
|
||||
{
|
||||
if (asset.Text == "")
|
||||
{
|
||||
asset.Text = asset.TypeString + " #" + asset.uniqueID;
|
||||
}
|
||||
asset.extension = ".dat";
|
||||
asset.SubItems.AddRange(new[] { asset.TypeString, asset.Size.ToString() });
|
||||
exportable = true;
|
||||
}
|
||||
if (exportable)
|
||||
{
|
||||
if (asset.Text == "")
|
||||
{
|
||||
asset.Text = asset.TypeString + " #" + asset.uniqueID;
|
||||
}
|
||||
asset.SubItems.AddRange(new[] { asset.TypeString, asset.fullSize.ToString() });
|
||||
//处理同名文件
|
||||
if (!exportableAssetsHash.Add((asset.TypeString + asset.Text).ToUpper()))
|
||||
{
|
||||
asset.Text += " #" + asset.uniqueID;
|
||||
}
|
||||
//处理非法文件名
|
||||
asset.Text = FixFileName(asset.Text);
|
||||
assetsFile.exportableAssets.Add(asset);
|
||||
}
|
||||
@ -371,14 +350,12 @@ namespace Unity_Studio
|
||||
});
|
||||
}
|
||||
exportableAssets.AddRange(assetsFile.exportableAssets);
|
||||
//if (assetGroup.Items.Count > 0) { listView1.Groups.Add(assetGroup); }
|
||||
}
|
||||
|
||||
visibleAssets = exportableAssets;
|
||||
|
||||
//will only work if ListView is visible
|
||||
exportableAssetsHash.Clear();
|
||||
//assetListView.EndUpdate();
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -1122,7 +1099,7 @@ namespace Unity_Studio
|
||||
{
|
||||
//TODO check texture type and set path accordingly; eg. CubeMap, Texture3D
|
||||
string texPathName = Path.GetDirectoryName(FBXfile) + "\\Texture2D\\";
|
||||
ExportTexture(TexturePD, texPathName, false);
|
||||
ExportTexture2D(TexturePD, texPathName, false);
|
||||
texPathName = Path.GetFullPath(Path.Combine(texPathName, $"{TexturePD.Text}.png"));//必须是png文件
|
||||
ob.AppendFormat("\n\tTexture: 7{0}, \"Texture::{1}\", \"\" {{", TexturePD.uniqueID, TexturePD.Text);
|
||||
ob.Append("\n\t\tType: \"TextureVideoClip\"");
|
||||
@ -1606,14 +1583,7 @@ namespace Unity_Studio
|
||||
return new byte[3] { red, green, blue };
|
||||
}
|
||||
|
||||
public static void ExportRawFile(AssetPreloadData asset, string exportFilepath)
|
||||
{
|
||||
asset.sourceFile.a_Stream.Position = asset.Offset;
|
||||
var bytes = asset.sourceFile.a_Stream.ReadBytes(asset.Size);
|
||||
File.WriteAllBytes(exportFilepath, bytes);
|
||||
}
|
||||
|
||||
public static bool ExportTexture(AssetPreloadData asset, string exportPathName, bool flip)
|
||||
public static bool ExportTexture2D(AssetPreloadData asset, string exportPathName, bool flip)
|
||||
{
|
||||
var m_Texture2D = new Texture2D(asset, true);
|
||||
if (m_Texture2D.image_data == null)
|
||||
@ -1648,19 +1618,20 @@ namespace Unity_Studio
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool ExportAudioClip(AssetPreloadData asset, string exportFilename, string exportFileextension)
|
||||
public static bool ExportAudioClip(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
var m_AudioClip = new AudioClip(asset, true);
|
||||
if (m_AudioClip.m_AudioData == null)
|
||||
return false;
|
||||
var oldextension = exportFileextension;
|
||||
var oldextension = asset.extension;
|
||||
var exportFileExtension = asset.extension;
|
||||
var convertfsb = (bool)Properties.Settings.Default["convertfsb"];
|
||||
if (convertfsb && exportFileextension == ".fsb")
|
||||
if (convertfsb && exportFileExtension == ".fsb")
|
||||
{
|
||||
exportFileextension = ".wav";
|
||||
exportFileExtension = ".wav";
|
||||
}
|
||||
var exportFullname = exportFilename + exportFileextension;
|
||||
if (ExportFileExists(exportFullname))
|
||||
var exportFullName = exportPath + asset.Text + exportFileExtension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
if (convertfsb && oldextension == ".fsb")
|
||||
{
|
||||
@ -1713,7 +1684,7 @@ namespace Unity_Studio
|
||||
Encoding.UTF8.GetBytes("data").CopyTo(buffer, 36);
|
||||
BitConverter.GetBytes(len1).CopyTo(buffer, 40);
|
||||
Marshal.Copy(ptr1, buffer, 44, (int)len1);
|
||||
File.WriteAllBytes(exportFullname, buffer);
|
||||
File.WriteAllBytes(exportFullName, buffer);
|
||||
|
||||
result = subsound.unlock(ptr1, ptr2, len1, len2);
|
||||
if (result != FMOD.RESULT.OK) { return false; }
|
||||
@ -1724,38 +1695,63 @@ namespace Unity_Studio
|
||||
}
|
||||
else
|
||||
{
|
||||
File.WriteAllBytes(exportFullname, m_AudioClip.m_AudioData);
|
||||
File.WriteAllBytes(exportFullName, m_AudioClip.m_AudioData);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ExportMonoBehaviour(MonoBehaviour m_MonoBehaviour, string exportFilename)
|
||||
public static bool ExportShader(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
File.WriteAllText(exportFilename, m_MonoBehaviour.serializedText);
|
||||
var m_Shader = new Shader(asset, true);
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
File.WriteAllBytes(exportFullName, m_Shader.m_Script);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ExportShader(Shader m_Shader, string exportFilename)
|
||||
public static bool ExportTextAsset(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
File.WriteAllBytes(exportFilename, m_Shader.m_Script);
|
||||
var m_TextAsset = new TextAsset(asset, true);
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
File.WriteAllBytes(exportFullName, m_TextAsset.m_Script);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ExportText(TextAsset m_TextAsset, string exportFilename)
|
||||
public static bool ExportMonoBehaviour(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
File.WriteAllBytes(exportFilename, m_TextAsset.m_Script);
|
||||
var m_MonoBehaviour = new MonoBehaviour(asset, true);
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
File.WriteAllText(exportFullName, m_MonoBehaviour.serializedText);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ExportFont(unityFont m_Font, string exportFilename)
|
||||
public static bool ExportFont(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
var m_Font = new unityFont(asset, true);
|
||||
if (m_Font.m_FontData != null)
|
||||
{
|
||||
File.WriteAllBytes(exportFilename, m_Font.m_FontData);
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
File.WriteAllBytes(exportFullName, m_Font.m_FontData);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void ExportMesh(Mesh m_Mesh, string exportPath)
|
||||
public static bool ExportMesh(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
var m_Mesh = new Mesh(asset, true);
|
||||
if (m_Mesh.m_VertexCount <= 0)
|
||||
return;
|
||||
return false;
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("g " + m_Mesh.m_Name);
|
||||
#region Vertices
|
||||
@ -1812,15 +1808,33 @@ namespace Unity_Studio
|
||||
}
|
||||
#endregion
|
||||
|
||||
File.WriteAllText(exportPath, sb.ToString());
|
||||
File.WriteAllText(exportFullName, sb.ToString());
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void ExportVideo(VideoClip m_VideoClip, string exportFilename)
|
||||
public static bool ExportVideoClip(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
var m_VideoClip = new VideoClip(asset, true);
|
||||
if (m_VideoClip.m_VideoData != null)
|
||||
{
|
||||
File.WriteAllBytes(exportFilename, m_VideoClip.m_VideoData);
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
File.WriteAllBytes(exportFullName, m_VideoClip.m_VideoData);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool ExportRawFile(AssetPreloadData asset, string exportPath)
|
||||
{
|
||||
var exportFullName = exportPath + asset.Text + asset.extension;
|
||||
if (ExportFileExists(exportFullName))
|
||||
return false;
|
||||
asset.sourceFile.a_Stream.Position = asset.Offset;
|
||||
var bytes = asset.sourceFile.a_Stream.ReadBytes(asset.Size);
|
||||
File.WriteAllBytes(exportFullName, bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool ExportFileExists(string filename)
|
||||
|
Reference in New Issue
Block a user