Skip to content

Commit

Permalink
Switch to dynamic log path.
Browse files Browse the repository at this point in the history
- Remove the fixed log path, base_path. Use a dynamic one to replace it.
- Only enable log output in Debug config to reduce user disk consumption.
  • Loading branch information
yyc12345 committed Mar 27, 2023
1 parent 9828d3d commit 05be6aa
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions BBDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ CKERROR CreateBBDecoderProto(CKBehaviorPrototype **pproto)

int BBDecoder(const CKBehaviorContext& behcontext)
{
InitVSDTempFolder(behcontext.Context); // yyc mark: init dynamic temp folder first.

interface_t parse_bb_test(CKBehavior *bb, CKFile *file);
void decorate(interface_t &data, CKBehavior *bb);
void generate_bb_test(interface_t &interface_data, CKBehavior *bb, CKFile *file);
Expand Down
3 changes: 0 additions & 3 deletions Decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include <utility>
#include <Windows.h>

//extern "C" {
// extern void __stdcall OutputDebugStringA(_In_opt_ const char* lpOutputString);
//};
void printfdbg(const char* fmt, ...)//print a format string to VS debug output
{
char s[1024];
Expand Down
4 changes: 3 additions & 1 deletion Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,18 @@ void generate_bb_test(interface_t &interface_data, CKBehavior *bb, CKFile *file)
if (name[i] == '.' || name[i] == '_') continue;
name[i] = '_';
}
sprintf(filename, base_path "/generator/generator_out_%s.log", name);
Generator generator = Generator(interface_data);
char *buffer = new char[1048576];
int length = generator.Generate(buffer);
#if defined(VSD_ENABLE_LOG) // yyc mark: only enable logger on Debug config
sprintf(filename, "%s/generator_out_%s.log", VSDTempFolderGenerator, name);
FILE *fout = fopen(filename, "wb");
for (int i = 0; i < length * 4; ++i)
{
fputc(buffer[i], fout);
}
fclose(fout);
#endif
extern void parse_string_test(CKBehavior *bb, char *buffer);
parse_string_test(bb, buffer);

Expand Down
10 changes: 8 additions & 2 deletions Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "precomp.h"
#include <map>
#include "interfaceData.h"
#include <iostream>
using namespace std;

class Parser
Expand Down Expand Up @@ -301,7 +302,12 @@ class Parser
}
void parse()
{
// yyc mark: only enable logger on Debug config
#if defined(VSD_ENABLE_LOG)
m_logger = fopen(m_filename, "w");
#else
m_logger = fopen("NUL", "w");
#endif
m_depth = 0;
char *p = m_data;
parse_chunk(p);
Expand Down Expand Up @@ -379,7 +385,7 @@ void parse_string_test(CKBehavior *bb,char *buffer)
}
map<int, int> bbTypeMap;
pre_scan(bb, bbTypeMap);
sprintf(filename, base_path "/generator/parser_out_%s.log", name);
sprintf(filename, "%s/parser_out_%s.log", VSDTempFolderGenerator, name);
Parser parser = Parser(buffer, filename, bbTypeMap);
parser.parse();
}
Expand All @@ -399,7 +405,7 @@ interface_t parse_bb_test(CKBehavior *bb, CKFile *file)
}
map<int, int> bbTypeMap;
pre_scan(bb, bbTypeMap);
sprintf(filename, base_path "/parser/parser_out_%s.log", name);
sprintf(filename, "%s/parser_out_%s.log", VSDTempFolderParser, name);
//bb->PreSave(file, 0);
CKStateChunk *chunk = bb->Save(file, 0);
int length = chunk->ConvertToBuffer(NULL);
Expand Down
41 changes: 41 additions & 0 deletions StdAfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,45 @@
// stdafx.obj will contain the pre-compiled type information

#include "precomp.h"
#include <cstring>
#include <filesystem>
#include <Windows.h>

char* VSDTempFolderGenerator = nullptr;
char* VSDTempFolderParser = nullptr;
void InitVSDTempFolder(CKContext* ctx) {
FreeVSDTempFolder(); // free first

auto pathmgr = ctx->GetPathManager();
assert(pathmgr != nullptr);

// WARNING: it seems that Virtools will not cleanup temp folder
// when crash or terminated by Visual Studio.
// Programmer may need manually clean it.
std::filesystem::path temp(pathmgr->GetVirtoolsTemporaryFolder().CStr());
std::string cache;
temp /= "VSD";

auto generator = temp / "generator";
cache = generator.string();
VSDTempFolderGenerator = new char[cache.size() + 1];
std::memcpy(VSDTempFolderGenerator, cache.c_str(), cache.size() + 1);
std::filesystem::create_directories(generator);

auto parser = temp / "parser";
cache = parser.string();
VSDTempFolderParser = new char[cache.size() + 1];
std::memcpy(VSDTempFolderParser, cache.c_str(), cache.size() + 1);
std::filesystem::create_directories(parser);

ctx->OutputToConsoleEx("VirtoolsScriptDeobfuscation: Log path is \"%s\"", temp.string().c_str());
OutputDebugStringA("Log path: ");
OutputDebugStringA(temp.string().c_str());
OutputDebugStringA("\n");
}
void FreeVSDTempFolder(void) {
if (VSDTempFolderGenerator != nullptr) delete[] VSDTempFolderGenerator;
if (VSDTempFolderParser != nullptr) delete[] VSDTempFolderParser;

VSDTempFolderGenerator = VSDTempFolderParser = nullptr;
}
2 changes: 2 additions & 0 deletions VirtoolsScriptDeobfuscation.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>precomp.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)$(ProjectName).pch</PrecompiledHeaderOutputFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>ck2.lib;vxmath.lib;%(AdditionalDependencies)</AdditionalDependencies>
Expand Down Expand Up @@ -103,6 +104,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>precomp.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)$(ProjectName).pch</PrecompiledHeaderOutputFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>ck2.lib;vxmath.lib;%(AdditionalDependencies)</AdditionalDependencies>
Expand Down
9 changes: 6 additions & 3 deletions precomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

#include "VxDefines.h"
#include "CKAll.h"
#define base_path "C:\\Users\\jjy\\Desktop\\test"

extern char* VSDTempFolderGenerator;
extern char* VSDTempFolderParser;
extern void InitVSDTempFolder(CKContext* ctx);
extern void FreeVSDTempFolder(void);

extern void printfdbg(const char* fmt,...);
//#undef min
//#undef max

0 comments on commit 05be6aa

Please sign in to comment.