Skip to content

Commit

Permalink
实现功能
Browse files Browse the repository at this point in the history
  • Loading branch information
marinedzl committed Mar 3, 2020
1 parent 7a506d2 commit 9cf3284
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "RapidJSON"]
path = RapidJSON
url = https://github.com/Tencent/rapidjson.git
1 change: 1 addition & 0 deletions RapidJSON
Submodule RapidJSON added at b16cec
171 changes: 168 additions & 3 deletions ReplaceJsonString/ReplaceJsonString.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,174 @@
#include "pch.h"
#include <iostream>
#include <fstream>
#include <rapidjson/document.h>
#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/encodedstream.h> // AutoUTFInputStream
#include <rapidjson/prettywriter.h>

int main()
#define CHECK(x) {if(!(x)) assert(x); goto Exit0;}

using namespace rapidjson;

bool ReplaceString(Document& d);

int main(int argc, char** argv)
{
std::cout << "Hello World!\n";
UTFType fileUTFType;
bool fileHasBOM;
Document d; // Document is GenericDocument<UTF8<> >

// Read file
{
FILE* fp = nullptr;
fopen_s(&fp, argv[1], "rb"); // non-Windows use "r"

char buffer[65536];
FileReadStream bis(fp, buffer, sizeof(buffer));

AutoUTFInputStream<unsigned, FileReadStream> eis(bis); // wraps bis into eis

d.ParseStream<0, AutoUTF<unsigned> >(eis); // This parses any UTF file into UTF-8 in memory

fclose(fp);

if (d.HasParseError())
{
printf("parse json file error(%d)\n", d.GetParseError());
goto Exit0;
}

fileUTFType = eis.GetType();
fileHasBOM = eis.HasBOM();
}

// Replace string
{
if (!ReplaceString(d))
{
printf("replace string error\n");
goto Exit0;
}
}

// Write file
{
std::string filename = argv[1];
filename = filename.insert(filename.find_last_of('.'), "_replaced");

FILE* fp = nullptr;
fopen_s(&fp, filename.c_str(), "wb"); // non-Windows use "w"

char write_buffer[65536];
FileWriteStream os(fp, write_buffer, sizeof(write_buffer));

typedef AutoUTFOutputStream<unsigned, FileWriteStream> OutputStream;
OutputStream eos(os, fileUTFType, fileHasBOM);

PrettyWriter<OutputStream, UTF8<>, UTF16LE<> > writer(eos); // 记得要填入eos,官网的例子里漏掉了
d.Accept(writer);

fclose(fp);
}

Exit0:
system("pause");
return -1;
}

bool IsHeadMesh(const char* Name)
{
if (!strcmp(Name, "mod_hd_hf1_01"))
return true;

if (!strcmp(Name, "mod_hd_hf2_01"))
return true;

if (!strcmp(Name, "mod_hd_hf2_02"))
return true;

if (!strcmp(Name, "mod_hd_hm1_01"))
return true;

if (!strcmp(Name, "mod_hd_hm2_01"))
return true;

return false;
}

const char* MeshSlotString =
"{\
\"SlotPart\": \"eFaceSlot\",\
\"CustomSlotName\" : \"root\",\
\"OverrideTransform\" : false,\
\"Location\" :\
{\
\"X\": 0,\
\"Y\" : 0,\
\"Z\" : 0\
},\
\"Rotation\":\
{\
\"Pitch\": 0,\
\"Yaw\" : 0,\
\"Roll\" : 0\
},\
\"Scale\":\
{\
\"X\": 0,\
\"Y\" : 0,\
\"Z\" : 0\
},\
\"ChildSocket\": \"\"\
}";

Document MakeMeshSlotValue(Document& doc, Value& SkinMeshValue)
{
Document subDoc(&doc.GetAllocator());
subDoc.Parse(MeshSlotString);
if (subDoc.HasParseError())
{
printf("parse json file error(%d)\n", subDoc.GetParseError());
}
subDoc.AddMember("MeshPrefab", Value(StringRef(SkinMeshValue["SkinMeshPrefab"].GetString())), doc.GetAllocator());

return subDoc;
}

bool ReplaceString(Document& doc)
{
for (SizeType rootArrayIndex = 0; rootArrayIndex < doc.Size(); rootArrayIndex++)
{
auto& Row = doc[rootArrayIndex];

// Find SkinMesh
bool found = false;
Value::ValueIterator itr;
auto SkinMeshs = Row["SkinMeshs"].GetArray();
for (itr = SkinMeshs.Begin(); itr != SkinMeshs.End(); ++itr)
{
const char* PrefabName = (*itr)["SkinMeshPrefab"].GetString();
if (IsHeadMesh(PrefabName))
{
found = true;
break;
}
}

if (!found)
continue;

// Add MeshSlot
auto& MeshSlots = Row["MeshSlots"];
MeshSlots.GetArray().PushBack(MakeMeshSlotValue(doc, (*itr)), doc.GetAllocator());

// Remove SkinMesh
SkinMeshs.Erase(itr);

printf("replace row %s\n", Row["Name"].GetString());
}

return true;
Exit0:
return false;
}
4 changes: 4 additions & 0 deletions ReplaceJsonString/ReplaceJsonString.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)RapidJSON/include;</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -106,6 +107,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)RapidJSON/include;</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -123,6 +125,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)RapidJSON/include;</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -142,6 +145,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)RapidJSON/include;</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down

0 comments on commit 9cf3284

Please sign in to comment.