增加Lua函数

This commit is contained in:
2025-09-02 22:45:41 +08:00
parent 6788128cc3
commit 25e7cff75d
14 changed files with 260 additions and 22 deletions
+2
View File
@@ -32,6 +32,8 @@ add_executable(sgutil
sgu_utils.h
sgu_lua_npk_file.h
sgu_lua_npk_file.cpp
sgu_lua_utils.h
sgu_lua_utils.cpp
)
target_include_directories(sgutil
+78
View File
@@ -0,0 +1,78 @@
--print npk file list to file
--[[
npkFileName(string): npk file path name(utf8)
listFileName(string): list output path name(utf8)
keyName(boolean): print key name
npkName(boolean): print npk file name
npkBase(boolean): print npk base dir name
fileSize(boolean): print file size
fileCRC(boolean): print file crc
fileOffset(boolean): print file offset
return(boolean): is success
--]]
local M={}
function M.dump_npk_file_list(npkFileName, listFileName, npkBase, npkName, keyName, fileOffset, fileSize, fileCRC)
local npk = createNPKFile();
local ret = npk:openPakFile(npkFileName)
if(not ret) then
return false
end
listOutputFile = io.open(listFileName, "w")
fileList = npk:generateFullInfoList();
counts = fileList:getFileCounts()
for i=0, counts-1, 1 do
local fileInfo = fileList:getFileItem(i)
outputLine=""
if(npkBase) then
if(#outputLine>0) then outputLine = outputLine .. "|" end
outputLine = outputLine .. fileInfo:npkBase();
end
if(npkName) then
if(#outputLine>0) then outputLine = outputLine .. "|" end
outputLine = outputLine .. fileInfo:npkName();
end
if(keyName) then
if(#outputLine>0) then outputLine = outputLine .. "|" end
outputLine = outputLine .. fileInfo:keyName();
end
if(fileOffset) then
if(#outputLine>0) then outputLine = outputLine .. "|" end
outputLine = outputLine .. tostring(fileInfo.fileOffset);
end
if(fileSize) then
if(#outputLine>0) then outputLine = outputLine .. "|" end
outputLine = outputLine .. tostring(fileInfo.fileSize);
end
if(fileCRC) then
if(#outputLine>0) then outputLine = outputLine .. "|" end
outputLine = outputLine .. tostring(fileInfo.fileCRC);
end
if(#outputLine>0) then
listOutputFile:write(outputLine .. "\n")
end
end
listOutputFile:close()
destroyFileList(fileList)
npk:closePakFile()
destroyNPKFile(npk)
return true
end
return M;
+48
View File
@@ -0,0 +1,48 @@
--Extracts all files from an NPK archive to a specified output directory.
--[[
npkFileName The path to the NPK archive file to be extracted.
outputPath The path to the output directory where files will be extracted.
return true if extraction succeeds; false otherwise.
--]]
local M={}
function M.extract_npk_files(npkFileName, outputPath)
local npk = createNPKFile();
local ret = npk:openPakFile(npkFileName)
if(not ret) then
return false
end
local counts = npk:getFileCounts()
for i=0, counts-1, 1 do
local baseInfo = npk:getFileNode(i)
local fileStream = npk:openFileStream(baseInfo:keyName())
print("keyName=" .. fileStream:name() .. "\n")
local diskFileName = outputPath .. "/" .. baseInfo:keyName()
ret = forceCreatePathForFile(diskFileName);
if(not ret) then
print("CreatePathForFile failed: " .. diskFileName)
npk:closeFileStream(fileStream)
break
end
ret = fileStream:writeToDiskFile(diskFileName)
if(not ret) then
print("dump file failed: " .. baseInfo:keyName())
print("LastErrorMsg=" .. getLastErrorMessage())
npk:closeFileStream(fileStream)
break
end
npk:closeFileStream(fileStream)
end
npk:closePakFile()
destroyNPKFile(npk)
return true
end
return M;
+9 -1
View File
@@ -11,12 +11,13 @@ extern "C"
#include "lua_tinker.h"
////////////////////////////////////////////////////////////////////////////////////
void LuaNPKFile::registerLuaApi(lua_State* L)
void registerNPKLuaLibs(lua_State* L)
{
lua_tinker::def(L, "createNPKFile", &LuaNPKFile::create);
lua_tinker::def(L, "destroyNPKFile", &LuaNPKFile::destroy);
lua_tinker::def(L, "createEmptyFileList", &LuaNPKFileList::createEmpty);
lua_tinker::def(L, "destroyFileList", &LuaNPKFileList::destroy);
lua_tinker::def(L, "getLastErrorMessage", &NPK::getLastErrorMessage);
lua_tinker::class_add<LuaNPKFile>(L, "NPKFile");
lua_tinker::class_def<LuaNPKFile>(L, "openPakFile", &LuaNPKFile::openPakFile);
@@ -52,6 +53,7 @@ void LuaNPKFile::registerLuaApi(lua_State* L)
lua_tinker::class_def<LuaNPKFileStream>(L, "tell", &LuaNPKFileStream::tell);
lua_tinker::class_def<LuaNPKFileStream>(L, "crc32", &LuaNPKFileStream::crc32);
lua_tinker::class_def<LuaNPKFileStream>(L, "isEOF", &LuaNPKFileStream::isEOF);
lua_tinker::class_def<LuaNPKFileStream>(L, "writeToDiskFile", &LuaNPKFileStream::writeToDiskFile);
}
LuaNPKFile* LuaNPKFile::create()
@@ -167,6 +169,12 @@ bool LuaNPKFileStream::isEOF(void) const
return m_pFileStream ? m_pFileStream->isEOF() : true;
}
bool LuaNPKFileStream::writeToDiskFile(const char* wszFileName)
{
std::wstring wstrLocalDiskFile = convertUtf8StringToWide(wszFileName);
return m_pFileStream ? m_pFileStream->writeToDiskFile(wstrLocalDiskFile.c_str()) : false;
}
/// /////////////////////////////////////////////////////////////////////////////////
LuaNPKFileList* LuaNPKFileList::createEmpty()
{
+5 -1
View File
@@ -27,6 +27,9 @@ public:
uint32_t crc32(void);
bool isEOF(void) const;
//write total file content to local disk file, wszFileName(utf8)
bool writeToDiskFile(const char* wszFileName);
public:
NPK::IFileStream* m_pFileStream = nullptr;
};
@@ -46,7 +49,6 @@ public:
class LuaNPKFile
{
public:
static void registerLuaApi(struct lua_State* L);
static LuaNPKFile* create();
static void destroy(LuaNPKFile* instance);
@@ -68,3 +70,5 @@ public:
LuaNPKFile();
~LuaNPKFile();
};
void registerNPKLuaLibs(struct lua_State* L);
+34
View File
@@ -0,0 +1,34 @@
#include "sgu_lua_utils.h"
#include "libNPK_Utils.h"
#include <strsafe.h>
#include <Shlwapi.h>
#include <algorithm>
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
#include "lua_tinker.h"
//utf8
bool luaForceCreatePathForFile(const char* szPath)
{
std::wstring strPath = convertUtf8StringToWide(szPath);
return forceCreatePathForFile(strPath.c_str());
}
//utf8
bool luaForceCreatePath(const char* szPath)
{
std::wstring strPath = convertUtf8StringToWide(szPath);
return forceCreatePath(strPath.c_str());
}
void registerUtilsLuaLibs(struct lua_State* L)
{
lua_tinker::def(L, "forceCreatePath", &luaForceCreatePath);
lua_tinker::def(L, "forceCreatePathForFile", &luaForceCreatePathForFile);
}
+3
View File
@@ -0,0 +1,3 @@
#pragma once
void registerUtilsLuaLibs(struct lua_State* L);
+2
View File
@@ -4,6 +4,7 @@
bool extractNPKFiles(const char* szNPKFileName, const char* szOutputDir)
{
#if 0
if (!szNPKFileName || !szOutputDir || szNPKFileName[0] == 0 || szOutputDir[0] == 0)
{
printf("Error: Invalid parameters. NPK file name and output directory must be specified.\n");
@@ -84,5 +85,6 @@ bool extractNPKFiles(const char* szNPKFileName, const char* szOutputDir)
}
}
NPK::closeNPKFile(npkFile);
#endif
return true;
}
+3 -1
View File
@@ -1,5 +1,6 @@
#include "sgu_mode_run_lua_file.h"
#include "sgu_lua_npk_file.h"
#include "sgu_lua_utils.h"
#include "libNPK.h"
@@ -22,7 +23,8 @@ bool run_lua_file(const char* szLuaFilename)
lua_tinker::def(L, "getLastErrorMessage", &NPK::getLastErrorMessage);
//register classes
LuaNPKFile::registerLuaApi(L);
registerNPKLuaLibs(L);
registerUtilsLuaLibs(L);
//run lua file
lua_tinker::dofile(L, szLuaFilename);