Skip to content

Instantly share code, notes, and snippets.

@ProjectSky
Last active August 4, 2020 04:32
Show Gist options
  • Save ProjectSky/d725deb8d0e60ee334e925346dcf9234 to your computer and use it in GitHub Desktop.
Save ProjectSky/d725deb8d0e60ee334e925346dcf9234 to your computer and use it in GitHub Desktop.
export.lua
--
-- Created by NotePad++.
-- User: ProjectSky
-- Date: 2020/01/24
-- Time: 13:58
-- Export file save in %USERPROFILE%/Zomboid/Lua/ItemName_%s.txt
--
-- get current language code
local clang = tostring(Translator.getLanguage())
-- get all item list
local items = getAllItems()
-- simple counter
local count = 0
-- file name
local output = ("/Zomboid/lua/ItemName_%s.txt"):format(clang)
local output_miss = ("/Zomboid/lua/ItemName_%s_miss.txt"):format(clang)
local output_Items = ("/Zomboid/lua/Items_%s_miss.txt"):format(clang)
local _table_dp = {}
local _table = {}
local WriteFile = Keys.java_WriteFile
-- if string byte > 127 return false
local function isEng(str)
return (string.match(str,"[^%w%p%s]") == nil) end
-- sorf table return key
local function sortkey(tab, sortfun)
local keys = {}
for key in pairs(tab) do
table.insert(keys, key)
end
table.sort(keys, function(a, b)
return sortfun(tab[a], tab[b])
end)
return keys
end
local function export()
-- head
WriteFile(output, string.format("ItemName_%s = {" .. "\r\n", clang))
-- get key value write to ItemName_%s.txt file
for i = 0, items:size() - 1 do
local item = items:get(i)
-- wtf Blooo?
-- filter Blooo
if item:getDisplayName() ~= "Blooo" and not isEng(item:getDisplayName()) then
local result = (' ItemName_%s = "%s",'):format(instanceItem(item):getFullType(),item:getDisplayName())
WriteFile(output, result .. "\r\n")
end
end
-- foot
WriteFile(output, "}" .. "\r\n")
end
-- insert table
local function insert()
for i = 0, items:size() - 1 do
local item = items:get(i)
if item:getDisplayName() ~= "Blooo" and item:getDisplayName() ~= "" and item:getDisplayName() ~= instanceItem(item):getFullType() and isEng(item:getDisplayName()) then
_table[instanceItem(item):getFullType()] = item:getDisplayName()
local dpname = string.gsub(item:getDisplayName(), "%s", "_")
local dpname = string.gsub(dpname, "-", "_")
_table_dp[dpname] = item:getDisplayName()
end
end
end
-- export miss translate
local function exportmiss()
-- head
WriteFile(output_miss, string.format("ItemName_%s = {" .. "\r\n", clang))
local sortkeys = sortkey(_table, function(a, b) return a < b end)
for _, key in pairs(sortkeys) do
local result = (' ItemName_%s = "%s",'):format(key,_table[key])
WriteFile(output_miss, result .. "\r\n")
end
-- foot
WriteFile(output_miss, "}" .. "\r\n")
end
-- export miss translate
local function exportdisplayname()
-- head
WriteFile(output_Items, string.format("Items_%s = {" .. "\r\n", clang))
local sortkeys = sortkey(_table_dp, function(a, b) return a < b end)
for _, key in pairs(sortkeys) do
local result = (' DisplayName_%s = "%s",'):format(key,_table_dp[key])
WriteFile(output_Items, result .. "\r\n")
end
-- foot
WriteFile(output_Items, "}" .. "\r\n")
end
local function OnKeyPressed(key)
if key == 199 and count == 0 then -- press HOME
-- block duplicate press
count = count + 1
export()
insert()
exportmiss()
exportdisplayname()
end
end
Events.OnKeyPressed.Add(OnKeyPressed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment