From eb4981808b96b6154678be40d7eaf325b8fa09f4 Mon Sep 17 00:00:00 2001 From: Perfare Date: Tue, 16 Jul 2019 05:32:54 +0800 Subject: [PATCH] Fixed coding errors --- AssetStudioFBX/AssetStudioFBX.cpp | 9 +++++-- AssetStudioFBX/AssetStudioFBX.h | 9 +++---- AssetStudioFBX/AssetStudioFBXExporter.cpp | 31 ++++++++--------------- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/AssetStudioFBX/AssetStudioFBX.cpp b/AssetStudioFBX/AssetStudioFBX.cpp index f48b774..2d40576 100644 --- a/AssetStudioFBX/AssetStudioFBX.cpp +++ b/AssetStudioFBX/AssetStudioFBX.cpp @@ -2,9 +2,14 @@ namespace AssetStudio { - char* Fbx::StringToCharArray(String^ s) + char* Fbx::StringToUTF8(String^ s) { - return (char*)(void*)Marshal::StringToHGlobalAnsi(s); + auto bytes = Text::Encoding::UTF8->GetBytes(s); + auto chars = new char[bytes->Length + 1]; + pin_ptr ptr = &bytes[0]; + memcpy(chars, ptr, bytes->Length); + chars[bytes->Length] = '\0'; + return chars; } void Fbx::Init(FbxManager** pSdkManager, FbxScene** pScene) diff --git a/AssetStudioFBX/AssetStudioFBX.h b/AssetStudioFBX/AssetStudioFBX.h index d73d3f6..23f2a66 100644 --- a/AssetStudioFBX/AssetStudioFBX.h +++ b/AssetStudioFBX/AssetStudioFBX.h @@ -10,19 +10,18 @@ using namespace System; using namespace System::Collections::Generic; using namespace System::IO; -using namespace System::Runtime::InteropServices; #define WITH_MARSHALLED_STRING(name,str,block)\ { \ char* name; \ try \ { \ - name = StringToCharArray(str); \ + name = StringToUTF8(str); \ block \ } \ finally \ { \ - Marshal::FreeHGlobal((IntPtr)name); \ + delete name; \ } \ } @@ -43,13 +42,13 @@ namespace AssetStudio { public: static Vector3 QuaternionToEuler(Quaternion q); static Quaternion EulerToQuaternion(Vector3 v); - static char* StringToCharArray(String^ s); + static char* StringToUTF8(String^ s); static void Init(FbxManager** pSdkManager, FbxScene** pScene); ref class Exporter { public: - static void Export(String^ path, IImported^ imported, bool eulerFilter, float filterPrecision, bool allFrames, bool allBones, bool skins, float boneSize, float scaleFactor, bool flatInbetween, int versionIndex, bool isAscii); + static void Export(String^ name, IImported^ imported, bool eulerFilter, float filterPrecision, bool allFrames, bool allBones, bool skins, float boneSize, float scaleFactor, bool flatInbetween, int versionIndex, bool isAscii); private: bool exportSkins; diff --git a/AssetStudioFBX/AssetStudioFBXExporter.cpp b/AssetStudioFBX/AssetStudioFBXExporter.cpp index 4b1b245..846d51a 100644 --- a/AssetStudioFBX/AssetStudioFBXExporter.cpp +++ b/AssetStudioFBX/AssetStudioFBXExporter.cpp @@ -12,9 +12,8 @@ namespace AssetStudio } String^ currentDir = Directory::GetCurrentDirectory(); Directory::SetCurrentDirectory(dir->FullName); - path = Path::GetFileName(path); - - Exporter^ exporter = gcnew Exporter(path, imported, allFrames, allBones, skins, boneSize, scaleFactor, versionIndex, isAscii); + auto name = Path::GetFileName(path); + Exporter^ exporter = gcnew Exporter(name, imported, allFrames, allBones, skins, boneSize, scaleFactor, versionIndex, isAscii); //TODO exporter->ExportMorphs(false, flatInbetween); exporter->ExportAnimations(eulerFilter, filterPrecision, flatInbetween); exporter->pExporter->Export(exporter->pScene); @@ -23,7 +22,7 @@ namespace AssetStudio Directory::SetCurrentDirectory(currentDir); } - Fbx::Exporter::Exporter(String^ path, IImported^ imported, bool allFrames, bool allBones, bool skins, float boneSize, float scaleFactor, int versionIndex, bool isAscii) + Fbx::Exporter::Exporter(String^ name, IImported^ imported, bool allFrames, bool allBones, bool skins, float boneSize, float scaleFactor, int versionIndex, bool isAscii) { this->imported = imported; exportSkins = skins; @@ -51,7 +50,7 @@ namespace AssetStudio FbxGlobalSettings& globalSettings = pScene->GetGlobalSettings(); globalSettings.SetSystemUnit(FbxSystemUnit(scaleFactor)); - cDest = StringToCharArray(path); + cDest = StringToUTF8(name); pExporter = FbxExporter::Create(pScene, ""); int pFileFormat = 0; @@ -155,7 +154,7 @@ namespace AssetStudio } if (cDest != NULL) { - Marshal::FreeHGlobal((IntPtr)cDest); + delete cDest; } } @@ -387,7 +386,7 @@ namespace AssetStudio char* pMatName = NULL; try { - pMatName = StringToCharArray(mat->Name); + pMatName = StringToUTF8(mat->Name); int foundMat = -1; for (int j = 0; j < pMaterials->GetCount(); j++) { @@ -467,7 +466,7 @@ namespace AssetStudio } finally { - Marshal::FreeHGlobal((IntPtr)pMatName); + delete pMatName; } } @@ -579,7 +578,7 @@ namespace AssetStudio char* pTexName = NULL; try { - pTexName = StringToCharArray(matTexName); + pTexName = StringToUTF8(matTexName); int foundTex = -1; for (int i = 0; i < pTextures->GetCount(); i++) { @@ -608,17 +607,7 @@ namespace AssetStudio pTex->SetRotation(0.0, 0.0); pTextures->Add(pTex); - String^ path = Path::GetDirectoryName(gcnew String(pExporter->GetFileName().Buffer())); - if (path == String::Empty) - { - path = "."; - } - FileInfo^ file = gcnew FileInfo(path + Path::DirectorySeparatorChar + Path::GetFileName(matTex->Name)); - DirectoryInfo^ dir = file->Directory; - if (!dir->Exists) - { - dir->Create(); - } + FileInfo^ file = gcnew FileInfo(matTex->Name); BinaryWriter^ writer = gcnew BinaryWriter(file->Create()); writer->Write(matTex->Data); writer->Close(); @@ -626,7 +615,7 @@ namespace AssetStudio } finally { - Marshal::FreeHGlobal((IntPtr)pTexName); + delete pTexName; } }